java SpringSecurity使用詳解
SpringSecurity
shrio,SpringSecurity:認(rèn)證,授權(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是針對(duì)Spring項(xiàng)目的安全框架,也是Spring Boot底層安全模塊默認(rèn)的技術(shù)選型,他可以實(shí)現(xiàn)強(qiáng)大的Web安全控制,對(duì)于安全控制,我們僅需要引入Spring-boot-starter-security模塊,進(jìn)行少量的配置,即可實(shí)現(xiàn)強(qiáng)大的安全管理!
記住幾個(gè)類:
- WebSecurityConfigurerAdapter: 自定義Security策略
- AuthenticationManagerBuilder:自定義認(rèn)證策略
- @EnableWebSecurity: 開啟WebSecurity模式 @Enablexxxx 開啟某個(gè)功能
Spring Security的兩個(gè)主要目標(biāo)是“認(rèn)證”和“授權(quán)”(訪問控制) .
“認(rèn)證”(Authentication)
“授權(quán)”(Authorization)
這個(gè)概念是通用的,而不是只在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)限驗(yàn)證的配置類,要先繼承WebSecurityConfigurerAdapter*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//定義訪問規(guī)則:首頁每個(gè)人都可以訪問,但是功能也只有特定權(quán)限的人才能訪問 鏈?zhǔ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)限默認(rèn)跳轉(zhuǎn)到登陸頁面 /login
http.formLogin();
}
//認(rèn)證
/* 密碼編碼: BCryptPasswordEncoder()
不編碼會(huì)報(bào)下面的錯(cuò)誤
* 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"都會(huì)去到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;
}
}
當(dāng)然也可以在數(shù)據(jù)庫中拿信息

源碼分析
沒有權(quán)限的話會(huì)自動(dòng)轉(zhuǎn)發(fā)到/login
//沒有權(quán)限默認(rè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實(shí)現(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)進(jìn)行判斷顯示該有的信息

4、根據(jù)源碼寫表單name屬性


5、實(shí)現(xiàn)有什么權(quán)限顯示什么樣的信息

6、注銷logout-404

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- Java SpringBoot安全框架整合Spring Security詳解
- Spring Security攔截器引起Java CORS跨域失敗的問題及解決
- Java開發(fā)之spring security實(shí)現(xiàn)基于MongoDB的認(rèn)證功能
- java中Spring Security的實(shí)例詳解
- Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
- java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇)
- Java 中使用Spring Security的實(shí)例詳解
相關(guān)文章
JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解
這篇文章主要介紹了JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
SpringBoot利用Validation包實(shí)現(xiàn)高效參數(shù)校驗(yàn)
如果不進(jìn)行校驗(yàn)就直接使用這些數(shù)據(jù),可能會(huì)導(dǎo)致各種問題,那么SpringBoot如何利用Validation包實(shí)現(xiàn)高效參數(shù)校驗(yàn)?zāi)?下面讓我們一起來探討這個(gè)重要的話題吧2025-04-04
IDEA 自定義方法注解模板的實(shí)現(xiàn)方法
這篇文章主要介紹了IDEA 自定義方法注解模板的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
mybatis中的mapper.xml使用循環(huán)語句
這篇文章主要介紹了mybatis中的mapper.xml使用循環(huán)語句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java使用雙異步實(shí)現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫
在開發(fā)中,我們經(jīng)常會(huì)遇到這樣的需求,將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫中,這篇文章主要來和大家講講Java如何使用雙異步實(shí)現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,感興趣的可以了解下2024-01-01
springboot 在idea中實(shí)現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實(shí)現(xiàn)熱部署的方法,實(shí)現(xiàn)了熱部署,在每一次作了修改之后,都會(huì)自動(dòng)的重啟,非常節(jié)約時(shí)間,感興趣的小伙伴們可以參考一下2018-10-10
springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決
這篇文章主要介紹了springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

