Spring Security的持久化用戶(hù)和授權(quán)實(shí)現(xiàn)方式
使用JdbcUserDetailsManager(UserDetailsService另一種實(shí)現(xiàn))實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀取用戶(hù)
1.引入jdbc和相關(guān)數(shù)據(jù)庫(kù)驅(qū)動(dòng)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
2.創(chuàng)建數(shù)據(jù)庫(kù)表
--用戶(hù)表 CREATE TABLE users( username VARCHAR(50) NOT NULL PRIMARY KEY --用戶(hù)名, password VARCHAR(500) NOT NULL --密碼, enabled BOOLEAN NOT NULL --有效性 ); --權(quán)限表 CREATE TABLE authorities( username VARCHAR(50) NOT NULL --用戶(hù)名, authority VARCHAR(50) NOT NULL --權(quán)限, constraint fk FOREIGN KEY(username) REFERENCES users(username) ); CREATE unique index ix_auth_username ON authorities (username, authority);
3.配置數(shù)據(jù)庫(kù)連接(application.yml)
spring: datasource: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/security username:postgres password: postgres
4.修改SecurityConfig配置
@Configuration public class SecurityConfig { //配置Security過(guò)濾鏈 @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { //配置哪些接口需要認(rèn)證(.anyRequest().authenticated()代表任何請(qǐng)求都需認(rèn)證) http.authorizeHttpRequests(authorize -> { authorize.anyRequest().authenticated(); }); //配置post表單請(qǐng)求/login接口 http.formLogin(Customizer.withDefaults()); //csrf攻擊:開(kāi)發(fā)環(huán)境可不配方便調(diào)試,上線(xiàn)環(huán)境需配置,否則會(huì)遭csrf攻擊 http.csrf(AbstractHttpConfigurer::disable); //返回Security過(guò)濾鏈對(duì)象 return http.build(); } @Bean //配置JdbcUserDetailsManager實(shí)現(xiàn)數(shù)據(jù)庫(kù)存儲(chǔ)用戶(hù) public UserDetailsService userDetailsService(DataSource dataSource) { return new JdbcUserDetailsManager(dataSource); } }
實(shí)現(xiàn)Spring Security授權(quán)功能
1.創(chuàng)建接口
@RestController public class HelloController{ @RequestMapping("/hello") public String hello() { return "Hello Security"; } @RequestMapping("/hello1") public String hello1() { return "Hello Security1"; } }
2.配置數(shù)據(jù)庫(kù)賬號(hào)和權(quán)限(DbUser用戶(hù)擁有hello和hello1權(quán)限、DbUser1只擁有hello1權(quán)限)
3.修改SecurityConfig配置
@Configuration public class SecurityConfig { //配置Security過(guò)濾鏈 @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { //配置哪些接口需要認(rèn)證(.anyRequest().authenticated()代表任何請(qǐng)求都需認(rèn)證) http.authorizeHttpRequests(authorize -> { authorize.requestMatchers("/hello").hasAuthority("hello"); authorize.requestMatchers("/hello1").hasAuthority("hello1"); authorize.anyRequest().authenticated(); }); //配置post表單請(qǐng)求/login接口 http.formLogin(Customizer.withDefaults()); //csrf攻擊:開(kāi)發(fā)環(huán)境可不配方便調(diào)試,上線(xiàn)環(huán)境需配置,否則會(huì)遭csrf攻擊 http.csrf(AbstractHttpConfigurer::disable); //返回Security過(guò)濾鏈對(duì)象 return http.build(); } @Bean //配置JdbcUserDetailsManager實(shí)現(xiàn)數(shù)據(jù)庫(kù)存儲(chǔ)用戶(hù) public UserDetailsService userDetailsService(DataSource dataSource) { return new JdbcUserDetailsManager(dataSource); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)
這篇文章主要介紹了Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)的相關(guān)資料,需要的朋友可以參考下2017-02-02java 多線(xiàn)程與并發(fā)之volatile詳解分析
volatile這個(gè)關(guān)鍵字可能很多朋友都聽(tīng)說(shuō)過(guò),或許也都用過(guò)。在Java 5之前,它是一個(gè)備受爭(zhēng)議的關(guān)鍵字,因?yàn)樵诔绦蛑惺褂盟鶗?huì)導(dǎo)致出人意料的結(jié)果。在Java 5之后,volatile關(guān)鍵字才得以重獲生機(jī)2021-11-11Spring數(shù)據(jù)庫(kù)連接池url參數(shù)踩坑及解決
這篇文章主要介紹了Spring數(shù)據(jù)庫(kù)連接池url參數(shù)踩坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09解決springboot利用ConfigurationProperties注解配置數(shù)據(jù)源無(wú)法讀取配置信息問(wèn)題
今天在學(xué)習(xí)springboot利用ConfigurationProperties注解配置數(shù)據(jù)源的使用遇到一個(gè)問(wèn)題無(wú)法讀取配置信息,發(fā)現(xiàn)全部為null,糾結(jié)是哪里出了問(wèn)題呢,今天一番思考,問(wèn)題根源找到,下面把我的解決方案分享到腳本之家平臺(tái),感興趣的朋友一起看看吧2021-05-05Spring IoC容器常見(jiàn)獲取Bean的方式匯總示例解析
這篇文章主要為大家介紹了Spring IoC容器常見(jiàn)獲取Bean的方式匯總示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java實(shí)現(xiàn)小型圖書(shū)館管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)小型圖書(shū)館管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Java變量命名規(guī)則詳解及常見(jiàn)命名錯(cuò)誤(建議收藏)
這篇文章主要介紹了Java中變量命名的規(guī)則及最佳實(shí)踐,包括有效字符、大小寫(xiě)敏感性、不能使用保留字、駝峰命名法、描述性命名、特定類(lèi)型的命名習(xí)慣、避免潛在問(wèn)題、常見(jiàn)命名錯(cuò)誤及如何避免等內(nèi)容,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02