java SpringSecurity使用詳解
SpringSecurity
shrio,SpringSecurity:認證,授權(quán)(VIP1,vip2…)
- 功能權(quán)限
- 訪問權(quán)限
- 菜單權(quán)限
- 攔截器,過濾器:大量的原生代碼,冗余
1、pom.xml
<!--Thymeleaf--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
簡介
Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術(shù)選型,他可以實現(xiàn)強大的Web安全控制,對于安全控制,我們僅需要引入Spring-boot-starter-security模塊,進行少量的配置,即可實現(xiàn)強大的安全管理!
記住幾個類:
- WebSecurityConfigurerAdapter: 自定義Security策略
- AuthenticationManagerBuilder:自定義認證策略
- @EnableWebSecurity: 開啟WebSecurity模式 @Enablexxxx 開啟某個功能
Spring Security的兩個主要目標是“認證”和“授權(quán)”(訪問控制) .
“認證”(Authentication)
“授權(quán)”(Authorization)
這個概念是通用的,而不是只在Spring Security中存在。
1、pom.xml
<!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、Security的controller
package com.kuang.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /*權(quán)限驗證的配置類,要先繼承WebSecurityConfigurerAdapter*/ @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //定義訪問規(guī)則:首頁每個人都可以訪問,但是功能也只有特定權(quán)限的人才能訪問 鏈式編程 //授權(quán) @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認跳轉(zhuǎn)到登陸頁面 /login http.formLogin(); } //認證 /* 密碼編碼: BCryptPasswordEncoder() 不編碼會報下面的錯誤 * java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" * */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("qin").password(new BCryptPasswordEncoder().encode("111")).roles("vip1","vip2","vip3") .and() .withUser("aaa").password(new BCryptPasswordEncoder().encode("111")).roles("vip1"); } }
3、路徑轉(zhuǎn)發(fā)的controller
package com.kuang.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Controller public class PathController { @GetMapping({"/","index"}) //"/""index"都會去到index.html public String Toindex(){ return "index"; } @GetMapping("/toLogin") public String Tologin(){ return "views/login"; } @GetMapping("/level1/{id}") //@PathVariable獲得url的占位符里面的值 public String ToView(@PathVariable("id")int id){ return "views/level1/"+id; } @GetMapping("/level2/{id}") public String ToView2(@PathVariable("id")int id){ return "views/level2/"+id; } @GetMapping("/level3/{id}") public String ToView3(@PathVariable("id")int id){ return "views/level3/"+id; } }
當然也可以在數(shù)據(jù)庫中拿信息
源碼分析
沒有權(quán)限的話會自動轉(zhuǎn)發(fā)到/login
//沒有權(quán)限默認跳轉(zhuǎn)到登陸頁面 /login
http.formLogin();
//開啟注銷
http.logout();
注銷及權(quán)限控制
<!--thymeleof整合security--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> xmlns:sec=http://www.thymeleaf.org/extras/spring-security
用spring-security實現(xiàn)用戶登錄后顯示用戶角色的信息
1、導(dǎo)入依賴thymeleof整合security
<!--thymeleof整合security--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency>
2、html命名空間
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
3、根據(jù)用戶的登錄狀態(tài)進行判斷顯示該有的信息
4、根據(jù)源碼寫表單name屬性
5、實現(xiàn)有什么權(quán)限顯示什么樣的信息
6、注銷logout-404
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能的示例代碼
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下2024-04-04dm.jdbc.driver.DMException網(wǎng)絡(luò)通信異常的解決過程
最近一個項目里面出現(xiàn)了一個比較詭異的問題,給大家分享下,這篇文章主要給大家介紹了關(guān)于dm.jdbc.driver.DMException網(wǎng)絡(luò)通信異常的解決過程,需要的朋友可以參考下2023-02-02JavaSwing GridLayout 網(wǎng)格布局的實現(xiàn)代碼
這篇文章主要介紹了JavaSwing GridLayout 網(wǎng)格布局的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-12-12Java中 ? extends T 和 ? super&nb
本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-05-05