Spring Security的持久化用戶和授權(quán)實現(xiàn)方式
使用JdbcUserDetailsManager(UserDetailsService另一種實現(xiàn))實現(xiàn)數(shù)據(jù)庫讀取用戶
1.引入jdbc和相關(guān)數(shù)據(jù)庫驅(qū)動
<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ù)庫表
--用戶表 CREATE TABLE users( username VARCHAR(50) NOT NULL PRIMARY KEY --用戶名, password VARCHAR(500) NOT NULL --密碼, enabled BOOLEAN NOT NULL --有效性 ); --權(quán)限表 CREATE TABLE authorities( username VARCHAR(50) NOT NULL --用戶名, 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ù)庫連接(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過濾鏈 @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { //配置哪些接口需要認(rèn)證(.anyRequest().authenticated()代表任何請求都需認(rèn)證) http.authorizeHttpRequests(authorize -> { authorize.anyRequest().authenticated(); }); //配置post表單請求/login接口 http.formLogin(Customizer.withDefaults()); //csrf攻擊:開發(fā)環(huán)境可不配方便調(diào)試,上線環(huán)境需配置,否則會遭csrf攻擊 http.csrf(AbstractHttpConfigurer::disable); //返回Security過濾鏈對象 return http.build(); } @Bean //配置JdbcUserDetailsManager實現(xiàn)數(shù)據(jù)庫存儲用戶 public UserDetailsService userDetailsService(DataSource dataSource) { return new JdbcUserDetailsManager(dataSource); } }
實現(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ù)庫賬號和權(quán)限(DbUser用戶擁有hello和hello1權(quán)限、DbUser1只擁有hello1權(quán)限)
3.修改SecurityConfig配置
@Configuration public class SecurityConfig { //配置Security過濾鏈 @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { //配置哪些接口需要認(rèn)證(.anyRequest().authenticated()代表任何請求都需認(rèn)證) http.authorizeHttpRequests(authorize -> { authorize.requestMatchers("/hello").hasAuthority("hello"); authorize.requestMatchers("/hello1").hasAuthority("hello1"); authorize.anyRequest().authenticated(); }); //配置post表單請求/login接口 http.formLogin(Customizer.withDefaults()); //csrf攻擊:開發(fā)環(huán)境可不配方便調(diào)試,上線環(huán)境需配置,否則會遭csrf攻擊 http.csrf(AbstractHttpConfigurer::disable); //返回Security過濾鏈對象 return http.build(); } @Bean //配置JdbcUserDetailsManager實現(xiàn)數(shù)據(jù)庫存儲用戶 public UserDetailsService userDetailsService(DataSource dataSource) { return new JdbcUserDetailsManager(dataSource); } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)
這篇文章主要介紹了Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)的相關(guān)資料,需要的朋友可以參考下2017-02-02Spring數(shù)據(jù)庫連接池url參數(shù)踩坑及解決
這篇文章主要介紹了Spring數(shù)據(jù)庫連接池url參數(shù)踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09解決springboot利用ConfigurationProperties注解配置數(shù)據(jù)源無法讀取配置信息問題
今天在學(xué)習(xí)springboot利用ConfigurationProperties注解配置數(shù)據(jù)源的使用遇到一個問題無法讀取配置信息,發(fā)現(xiàn)全部為null,糾結(jié)是哪里出了問題呢,今天一番思考,問題根源找到,下面把我的解決方案分享到腳本之家平臺,感興趣的朋友一起看看吧2021-05-05Java變量命名規(guī)則詳解及常見命名錯誤(建議收藏)
這篇文章主要介紹了Java中變量命名的規(guī)則及最佳實踐,包括有效字符、大小寫敏感性、不能使用保留字、駝峰命名法、描述性命名、特定類型的命名習(xí)慣、避免潛在問題、常見命名錯誤及如何避免等內(nèi)容,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02