Update SpringSecurity流程补充.md

pull/142/head
Yang Libin 2 years ago committed by GitHub
parent ec936747c3
commit 017c3b3e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

Loading…
Cancel
Save