|
|
|
@ -1,27 +1,26 @@
|
|
|
|
|
# springSecurity 流程补充
|
|
|
|
|
# SpringSecurity 流程补充
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
1. 基于 spring-boot-dependencies:2.7.7
|
|
|
|
|
2. 首先需要了解 [springboot2.7 升级](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes)
|
|
|
|
|
`Changes to Auto-configuration` 以后使用 `autoconfigure` 进行自动注入
|
|
|
|
|
3.
|
|
|
|
|
|
|
|
|
|
代码地址[io.github.poo0054](https://github.com/poo0054/security-study/blob/master/starter/src/main/java/io/github/poo0054/security/StarterApplication.java)
|
|
|
|
|
3. 代码地址 [io.github.poo0054](https://github.com/poo0054/security-study/blob/master/starter/src/main/java/io/github/poo0054/security/StarterApplication.java)
|
|
|
|
|
|
|
|
|
|
## 启动
|
|
|
|
|
|
|
|
|
|
我们每次添加` <artifactId>spring-boot-starter-security</artifactId>`
|
|
|
|
|
,启动的时候启动日志会有一条类似
|
|
|
|
|
`Using generated springSecurity password: 1db8eb87-e2ee-4c72-88e7-9b85268c4430
|
|
|
|
|
我们每次添加 `<artifactId>spring-boot-starter-security</artifactId>`,启动的时候会有一条类似的日志:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Using generated springSecurity password: 1db8eb87-e2ee-4c72-88e7-9b85268c4430
|
|
|
|
|
|
|
|
|
|
This generated password is for development use only. Your springSecurity configuration must be updated before running your
|
|
|
|
|
application in production.`
|
|
|
|
|
application in production.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
的日志。找到`UserDetailsServiceAutoConfiguration#InMemoryUserDetailsManager`类,它是 springboot 自动装配的。
|
|
|
|
|
找到 `UserDetailsServiceAutoConfiguration#InMemoryUserDetailsManager` 类,它是 springboot 自动装配的。
|
|
|
|
|
|
|
|
|
|
下面这些都是 springboot 自动装配类,在`spring-boot-autoconfigure-2.7.7.jar`>META-INF>spring>
|
|
|
|
|
org.springframework.boot.autoconfigure.AutoConfiguration.imports 中. 这些类就是 springSecurity 的全部了.
|
|
|
|
|
下面这些都是 springboot 自动装配类,在 `spring-boot-autoconfigure-2.7.7.jar` > META-INF > spring > org.springframework.boot.autoconfigure.AutoConfiguration.imports 中。这些类就是 Spring Security 的全部了。
|
|
|
|
|
|
|
|
|
|
```imports
|
|
|
|
|
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
|
|
|
|
@ -70,8 +69,6 @@ public class SecurityAutoConfiguration {
|
|
|
|
|
这个是 springSecurity 的核心配置类`SecurityProperties`,里面能配置
|
|
|
|
|
`filter`: 过滤,`user` : 用户信息
|
|
|
|
|
|
|
|
|
|
`这个有个问题,filter是属于tomcat的,springSecurity 中使用什么方式让filter变的有序的`
|
|
|
|
|
|
|
|
|
|
### @Import({ SpringBootWebSecurityConfiguration.class, SecurityDataConfiguration.class })
|
|
|
|
|
|
|
|
|
|
这里导入了 2 个类 `SpringBootWebSecurityConfiguration`和`SecurityDataConfiguration`,`SecurityDataConfiguration`是 Spring
|
|
|
|
@ -193,6 +190,7 @@ UserDetailsManager 的非持久化实现,支持内存映射。
|
|
|
|
|
## SecurityFilterAutoConfiguration
|
|
|
|
|
|
|
|
|
|
SpringSecurity 的过滤器
|
|
|
|
|
|
|
|
|
|
自动配置。与 SpringBootWebSecurityConfiguration 分开配置,以确保在存在用户提供的 WebSecurityConfiguration 时,过滤器的顺序仍然被配置。
|
|
|
|
|
|
|
|
|
|
### DelegatingFilterProxyRegistrationBean
|
|
|
|
@ -571,7 +569,7 @@ FilterOrderRegistration(){
|
|
|
|
|
put(CorsFilter.class, order.next());
|
|
|
|
|
put(CsrfFilter.class, order.next());
|
|
|
|
|
put(LogoutFilter.class, order.next());
|
|
|
|
|
// .....
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
springSecurity 事先使用这个类把预加载的类全部排序好,然后每次 add 一个新的 filter 就会使用这个里面的序号。如果我们有自定义的类,也要提前加载到里面去,不然就会
|
|
|
|
@ -909,8 +907,7 @@ public Object invoke(MethodInvocation mi)throws Throwable{
|
|
|
|
|
Object result;
|
|
|
|
|
try {
|
|
|
|
|
result = mi.proceed();
|
|
|
|
|
}
|
|
|
|
|
finally{
|
|
|
|
|
} finally {
|
|
|
|
|
super.finallyInvocation(token);
|
|
|
|
|
}
|
|
|
|
|
return super.afterInvocation(token, result);
|
|
|
|
@ -1098,6 +1095,7 @@ public void init(B http)throws Exception{
|
|
|
|
|
```java
|
|
|
|
|
/**
|
|
|
|
|
* Updates the default values for authentication.
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected final void updateAuthenticationDefaults() {
|
|
|
|
@ -1195,8 +1193,7 @@ public void configure(H http){
|
|
|
|
|
FilterChain chain, RuntimeException exception) throws IOException, ServletException {
|
|
|
|
|
if (exception instanceof AuthenticationException) {
|
|
|
|
|
handleAuthenticationException(request, response, chain, (AuthenticationException) exception);
|
|
|
|
|
}
|
|
|
|
|
else if(exception instanceof AccessDeniedException){
|
|
|
|
|
} else if (exception instanceof AccessDeniedException) {
|
|
|
|
|
handleAccessDeniedException(request, response, chain, (AccessDeniedException) exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|