在前面的章節(jié)中,我們沿用了Spring Security默認(rèn)的安全機(jī)制:僅有一個(gè)用戶,僅有一種角色。在實(shí)際開發(fā)中,這自然是無法滿足需求的。本章將更加深入地對Spring Security迚行配置,且初步使用授權(quán)機(jī)制。
3.1 默認(rèn)數(shù)據(jù)庫模型的認(rèn)證與授權(quán)
3.1.1、資源準(zhǔn)備
首先,在controller包下新建三個(gè)控制器,如圖所示。
其次,分別建立一些測試路由。
package com.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/admin/api")
public class AdminController {
@GetMapping("/hello")
public String hello(){
return "hello,admin";
}
}
package com.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/app/api")
public class AppController {
@GetMapping("/hello")
public String hello(){
return "hello,app";
}
}
package com.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user/api")
public class UserController {
@GetMapping("/hello")
public String hello(){
return "hello,user";
}
}
假設(shè)在/admin/api/下的內(nèi)容是系統(tǒng)后臺(tái)管理相關(guān)的API,在/app/api 下的內(nèi)容是面向客戶端公開訪問的API,在/user/api/下的內(nèi)容是用戶操作自身數(shù)據(jù)相關(guān)的API;顯然,/admin/api必須擁有管理員權(quán)限才能進(jìn)行操作,而/user/api必須在用戶登錄后才能進(jìn)行操作。文章來源:http://www.zghlxwxcb.cn/news/detail-743379.html
3.1.2、資源授權(quán)的配置
為了能正常訪問前面的路由,我們需要進(jìn)一步地配置Spring Security。文章來源地址http://www.zghlxwxcb.cn/news/detail-743379.html
package com.boot.config;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.util.StringUtils;
import java.util.List;
import static org.springframework.security.config.Customizer.withDefaults;
//@EnableWebSecurity:開啟SpringSecurity 之后會(huì)默認(rèn)注冊大量的過濾器servlet filter
//過濾器鏈【責(zé)任鏈模式】SecurityFilterChain
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
//authorizeHttpRequests:針對http請求進(jìn)行授權(quán)配置
//login登錄頁面需要匿名訪問
//permitAll:具有所有權(quán)限 也就可以匿名可以訪問
//anyRequest:任何請求 所有請求
//authenticated:認(rèn)證【登錄】
http.authorizeHttpRequests(authorizeHttpRequests->
authorizeHttpRequests
//********************************角色****************************************
//.requestMatchers("/admin/api").hasRole("admin") //必須有admin角色才能訪問到
//.requestMatchers("/user/api").hasAnyRole("admin","user") // /user/api:admin、user都是可以訪問
//********************************權(quán)限****************************************
.requestMatchers("/admin/api").hasAuthority("admin:api") //必須有admin:api權(quán)限才能訪問到
.requestMatchers("/user/api").hasAnyAuthority("admin:api","user:api") //有admin:api、user:api權(quán)限能訪問到
//********************************匹配模式****************************************
.requestMatchers("/admin/api/?").hasAuthority("admin:api") //必須有admin:api權(quán)限才能訪問到
.requestMatchers("/user/api/my/*").hasAuthority("admin:api") //必須有admin:api權(quán)限才能訪問到
.requestMatchers("/admin/api/a/b/**").hasAuthority("admin:api") //必須有admin:api權(quán)限才能訪問到
.requestMatchers("/app/api").permitAll() //匿名可以訪問
.requestMatchers("/login").permitAll()
.anyRequest().authenticated()
);
//現(xiàn)在我們借助異常處理配置一個(gè)未授權(quán)頁面:【實(shí)際上是不合理的 我們應(yīng)該捕獲異常信息 通過異常類型來判斷是什么異?!? http.exceptionHandling(e->e.accessDeniedPage("/noAuth"));
//http:后面可以一直點(diǎn) 但是太多內(nèi)容之后不美
到了這里,關(guān)于【Spring Security】Spring Security 認(rèn)證與授權(quán)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!