01 | Spring Security 架构及核心类
Spring Security 中的过滤器链
Spring Security 中采用的是管道-过滤器(Pipe-Filter)架构模式,这些过滤器链,构成了 Spring Security 的核心。如下图所示:

项目一旦启动,过滤器链将会实现自动配置,如下图所示:

UsernamePasswordAuthenticationFilter 用来检查输入的用户名和密码,代码如下:
public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response)
throws AuthenticationException {
if (postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException(
"Authentication method not supported: " + request.getMethod());
}
String username = obtainUsername(request);
String password = obtainPassword(request);
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest =
new UsernamePasswordAuthenticationToken(username, password);
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
…
}
BasicAuthenticationFilter 用来认证用户的身份。
FilterSecurityInterceptor 用来判定该请求是否能够访问目标 HTTP 端点。
Spring Security 中的核心类

SecurityContextHolder 存储了应用的安全上下文对象 SecurityContext,包含系统请求中最近使用的认证信息。
一个 HTTP 请求到达系统后,将通过一系列的 Filter 完成用户认证,然后具体的工作交由 AuthenticationManager 完成,AuthenticationManager 成功验证后会返回填充好的 Authentication 实例。
AuthenticationManager 是一个接口,其实现类 ProviderManager 会进一步依赖 AuthenticationProvider 接口完成具体的认证工作。
在 Spring Security 中存在一大批 AuthenticationProvider 接口的实现类,分别完成各种认证操作。在执行具体的认证工作时,Spring Security 势必会使用用户详细信息,UserDetailsService 服务就是用来对用户详细信息实现管理。
02 | 基于 Spring Security 构建用户认证体系
在 Spring Boot 中整合 Spring Security 框架首先需要引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
只要我们在代码工程中添加了上述依赖,包含在该工程中的所有 HTTP 端点都将被保护起来。
在引入 spring-boot-starter-security 依赖之后,Spring Security 会默认创建一个用户名为“user”的账号。当我们访问 AccountController 的 “accounts/{accountId}” 端点时,弹出如下界面:

同时,控制台日志打印如下:
Using generated security password: 17bbf7c4-456a-48f5-a12e-a680066c8f80
因此,访问该接口需要设置如下信息:

每次启动应用时,通过 Spring Security 自动生成的密码都会有所变化。如果我们想设置登录账号和密码,可以在 application.yml 中配置如下:
spring:
security:
user:
name: springcss
password: springcss_password
配置 Spring Security
初始化用户信息所依赖的配置类是 WebSecurityConfigurer 接口,在日常开发中,我们往往是使用 WebSecurityConfigurerAdapter 类并覆写其中的 configure(AuthenticationManagerBuilder auth) 的方法完成配置工作。
使用 AuthenticationManagerBuilder 类创建一个 AuthenticationManager 就能够轻松实现基于内存、LADP 和 JDBC 的验证。初始化所使用的用户信息只需要指定用户名(Username)、密码(Password)和角色(Role)这三项数据即可。
使用基于内存的用户信息存储方案
|