您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java中如何使用SpringSecurity,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
shrio,SpringSecurity:認(rèn)證,授權(quán)(VIP1,vip2…)
功能權(quán)限
訪問(wèn)權(quán)限
菜單權(quán)限
攔截器,過(guò)濾器:大量的原生代碼,冗余
<!--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è)類(lèi):
WebSecurityConfigurerAdapter: 自定義Security策略
AuthenticationManagerBuilder:自定義認(rèn)證策略
@EnableWebSecurity: 開(kāi)啟WebSecurity模式 @Enablexxxx 開(kāi)啟某個(gè)功能
Spring Security的兩個(gè)主要目標(biāo)是“認(rèn)證”和“授權(quán)”(訪問(wèn)控制) .
“認(rèn)證”(Authentication)
“授權(quán)”(Authorization)
這個(gè)概念是通用的,而不是只在Spring Security中存在。
<!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
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)證的配置類(lèi),要先繼承WebSecurityConfigurerAdapter*/ @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { //定義訪問(wèn)規(guī)則:首頁(yè)每個(gè)人都可以訪問(wèn),但是功能也只有特定權(quán)限的人才能訪問(wè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"); //沒(méi)有權(quán)限默認(rèn)跳轉(zhuǎn)到登陸頁(yè)面 /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"); } }
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ù)庫(kù)中拿信息
源碼分析
沒(méi)有權(quán)限的話會(huì)自動(dòng)轉(zhuǎn)發(fā)到/login
//沒(méi)有權(quán)限默認(rèn)跳轉(zhuǎn)到登陸頁(yè)面 /login
http.formLogin();
//開(kāi)啟注銷(xiāo)
http.logout();
<!--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)用戶(hù)登錄后顯示用戶(hù)角色的信息
<!--thymeleof整合security--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
關(guān)于“java中如何使用SpringSecurity”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。