一、創建項目並導入依賴
<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>
二、相關配置和代碼
查看這篇博客有相關介紹https://www.cnblogs.com/fernfei/p/12185186.html
1)創建一個MultiSecurityConfig配置類
注:在MultiSecurityConfig類上加上@configuration注解
2)在方法上注入,好讓多個httpsecurity共用一個賬戶密碼
@Bean和@Component都是把類注冊成spring組件,那為什么要有兩個?
如果你想要將第三方庫中的組件裝配到你的應用中,在這種情況下,是沒有辦法在它的類上添加@Component注解的,因此就不能使用自動化裝配的方案了,但是我們可以使用@Bean
3)在MultiSecurityConfig配置類中創建兩個內部類
分別是AdminSecurityConfig和OtherSecurityConfig內部類
3.1)在AdminSecurityConfig類上加@order(1)表示該類優先級高於OtherSecurityConfig
注:@order()中的數字越小,優先級越高
截圖里面的具體代碼可以翻我上面鏈接的另一個文章
我解釋一下我理解我注釋那部分,為什么不那樣寫
如果使用在AdminSecurityConfig中http.authorizeRequest(),會和OtherSecurityConfig中的http.authorizeRequest()重復,從而導致springboot以為這是兩個授權的請求路徑導致無法訪問登陸處理接口
剩下的配置都和單個HttpSecurity一模一樣可以直接訪問這篇博客https://www.cnblogs.com/fernfei/p/12185186.html