SpringBoot 整合 SpringSecurity 之起源篇(零)

SpringBoot 整合 SpringSecurity 之起源篇(零)html

本篇为SpringSecurity的第一篇,主要来介绍下什么是SpringSecurity,以及在springboot中如何使用它java

<!-- more -->git

I. 基本知识点

官方文档: https://docs.spring.io/spring-security/site/docs/5.2.2.BUILD-SNAPSHOT/reference/htmlsingle/#community-helpgithub

下面是官方介绍web

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.spring

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements安全

用国语,简单抽象的说一下它的定义springboot

  • 很🐂的认证和访问权限校验框架

那么具体能干吗?cookie

  • 用户登陆认证:用户名+密码登陆,肯定用户身份
  • 用户访问鉴权(常见的ACL访问控制列表,RBAC角色访问控制):断定是否有权限访问某个资源
  • 安全保护(CSRF跨站点攻击,Session Fixation会话固定攻击...)

II. 初体验

接下来咱们看一下再springboot中如何使用springsecurityapp

1. 配置

首先得是spring boot项目,而后添加上security的依赖便可,相对完整的pom配置以下(注意咱们使用的springboot版本为2.2.1.RELEASE)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

2. 实例demo

上面配置完以后,啥都不须要干,项目已经接入了spring security;项目中的服务都须要登陆以后才能访问

// 程序启动类
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

// rest 服务
@RestController
public class IndexRest {

    @GetMapping(path = {"/", "/index"})
    public String index() {
        return "hello this is index!";
    }

    @GetMapping(path = "hello")
    public String hello(String name) {
        return "welcome " + name;
    }
}

当咱们须要访问首页时,会发现直接302重定向到登陆页面了,以下图

spring security默认给咱们生成了一个用户名为user,密码为控制台中输出的一行日志如Using generated security password: aa410186-5c04-4282-b217-507ffb1f61eb

登陆以后会重定向回咱们以前访问的url,经过抓包能够看到,登陆成功以后,会设置请求方的cookie,后续的请求携带cookie来代表用户身份

3. 基本配置

上面虽然演示了一个hello world的初体验项目,可是这个默认的用户名/密码有点鬼畜,默认的配置主要来自于org.springframework.boot.autoconfigure.security.SecurityProperties.User,下面是截图(因此前面的用户名为user)

接下来咱们须要配置为对人类友好的方式,在项目的配置文件application.yml中,指定登陆的用户名/密码

spring:
  security:
    user:
      name: yihuihui
      password: 123456

重启测试项目,使用新的用户名/密码(yihuihui/123456)就能够登陆成功了;

4. 用户身份获取

上面虽然是一个简单的case,但还有一点不得不提一下,在个人接口中,虽然知道你登陆了,但怎么知道你是谁呢?

咱们能够直接经过HttpServletRequest#getRemoteUser()的方法来获取登陆用户; 或者经过SecurityContextHolder.getContext().getAuthentication().getPrincipal()来获取受权信息

咱们来写一个通用方法

public String getUser() {
    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteUser();
}

// or
public Object getUser() {
    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

而后稍微改一下咱们的服务接口

@GetMapping(path = {"/", "/index"})
public String index() {
    return "hello this is index! welcome " + getUser();
}

再次访问以后,结果以下

5. 小结

本文主要是spring security系列的起源篇,第一节介绍了下什么是SpringSecurity,有什么特色

  • spring security是一个很🐂🍺的认证(能够简单理解为登陆验证)和鉴权(可简单理解为访问控制)框架
  • 三大特色:登陆 + 鉴权 + 安全防御

第二节介绍了一个简单入门的HelloWorld实例

  • springboot项目,添加依赖 spring-boot-starter-security; 全部的http接口访问都须要登陆,默认提供一个用户名为user,密码为控制台输出的UUID字符串
  • 经过spring.security.user.namespring.security.user.password来指定用户名密码
  • 经过HttpServletRequest#getRemoteUser()获取登陆用户

那么问题来了,什么系统可能只有一个用户呢?要多用户怎么办?不一样的用户不一样的权限怎么办?某些接口全部人均可以访问又怎么办?

II. 其余

0. 项目

1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛

一灰灰blog