Spring Boot中的SpringSecurity基礎(chǔ)教程
一 SpringSecurity簡(jiǎn)介
- Web開發(fā)中,雖然安全屬于非共功能性需求,但也是應(yīng)用非常重要的一部分。
- 如果開發(fā)后期才考慮安全問題,就會(huì)有兩方面的弊處:
- 一方面,應(yīng)用存在嚴(yán)重的安全漏洞,無法滿足用戶需求,可能造成用戶隱私數(shù)據(jù)被攻擊者竊取
- 另一方面,應(yīng)用的基本架構(gòu)已經(jīng)確定,要修復(fù)安全漏洞,可能需要對(duì)系統(tǒng)的架構(gòu)做出比較重大的調(diào)整,會(huì)需要更多的開發(fā)時(shí)間,影響應(yīng)用的發(fā)布進(jìn)程
- 結(jié)論:從應(yīng)用開發(fā)的第一天就要把安全相關(guān)的因素考慮進(jìn)來,并持續(xù)在整個(gè)應(yīng)用的開發(fā)過程中
- Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架。它實(shí)際上是保護(hù)基于spring的應(yīng)用程序的標(biāo)準(zhǔn)Spring Security是一個(gè)框架,側(cè)重于為Java應(yīng)用程序提供身份驗(yàn)證和授權(quán)。
- Spring安全性的真正強(qiáng)大之處在于它可以輕松地?cái)U(kuò)展以滿足定制需求
- Spring 是一個(gè)非常流行和成功的 Java 應(yīng)用開發(fā)框架。Spring Security 基于 Spring 框架,提供了一套Web 應(yīng)用安全性的完整解決方案。一般來說,Web 應(yīng)用的安全性包括用戶認(rèn)證(Authentication)和用戶授權(quán)(Authorization)兩個(gè)部分。
- 用戶認(rèn)證指的是驗(yàn)證某個(gè)用戶是否為系統(tǒng)中的合法主體,也就是說用戶能否訪問該系統(tǒng)。用戶認(rèn)證一般要求用戶提供用戶名和密碼。系統(tǒng)通過校驗(yàn)用戶名和密碼來完成認(rèn)證過程。
- 用戶授權(quán)指的是驗(yàn)證某個(gè)用戶是否有權(quán)限執(zhí)行某個(gè)操作。在一個(gè)系統(tǒng)中,不同用戶所具有的權(quán)限是不同的。比如對(duì)一個(gè)文件來說,有的用戶只能進(jìn)行讀取,而有的用戶可以進(jìn)行修改。一般來說,系統(tǒng)會(huì)為不同的用戶分配不同的角色,而每個(gè)角色則對(duì)應(yīng)一系列的權(quán)限。
- 在用戶認(rèn)證方面,SpringSecurity 框架支持主流的認(rèn)證方式,包括==HTTP 基本認(rèn)證、HTTP 表單驗(yàn)證、HTTP 摘要認(rèn)證、OpenID和LDAP ==等。
- 在用戶授權(quán)方面,Spring Security 提供了基于角色的訪問控制和訪問控制列表(Access Control List,ACL),可以對(duì)應(yīng)用中的領(lǐng)域?qū)ο筮M(jìn)行細(xì)粒度的控制。
- 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模式
- Spring Security的兩個(gè)主要目標(biāo)是 “認(rèn)證” 和 “授權(quán)”(訪問控制)
- “認(rèn)證”(Authentication)
- 身份驗(yàn)證是關(guān)于驗(yàn)證您的憑據(jù),如用戶名/用戶ID和密碼,以驗(yàn)證您的身份。
- 身份驗(yàn)證通常通過用戶名和密碼完成,有時(shí)與身份驗(yàn)證因素結(jié)合使用。
- “授權(quán)” (Authorization)
- 授權(quán)發(fā)生在系統(tǒng)成功驗(yàn)證您的身份后,最終會(huì)授予您訪問資源(如信息,文件,數(shù)據(jù)庫,資金,位置,幾乎任何內(nèi)容)的完全權(quán)限。
二 實(shí)戰(zhàn)演示
0. 環(huán)境 介紹
- jdk 1.8
- Spring Boot 2.0.9.RELEASE
1. 新建一個(gè)初始的springboot項(xiàng)目

2. 導(dǎo)入thymeleaf依賴
<!--thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>3. 導(dǎo)入靜態(tài)資源

4. 編寫controller跳轉(zhuǎn)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 緣友一世
* date 2022/9/11-21:56
*/
@Controller
public class RouterController {
@RequestMapping({"/","/index"})
public String index() {
return "index";
}
@RequestMapping("/toLogin")
public String toLogin() {
return "views/login";
}
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id) {
return "views/level1/"+id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id) {
return "views/level2/"+id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") int id) {
return "views/level3/"+id;
}
}
5. 認(rèn)證和授權(quán)
- Spring Security 增加上認(rèn)證和授權(quán)的功能
引入Spring Security模塊
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>2.編寫 Spring Security 配置類
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author 緣友一世
* date 2022/9/11-22:13
*/
// 開啟WebSecurity模式
@EnableWebSecurity //AOP 攔截器
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
}
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author 緣友一世
* date 2022/9/11-22:13
*/
// 開啟WebSecurity模式
@EnableWebSecurity //AOP 攔截器
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
}
3.定制請(qǐng)求的授權(quán)規(guī)則
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
//首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問
//請(qǐng)求授權(quán)的規(guī)則
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
}4.測(cè)試一下:發(fā)現(xiàn)除了首頁都進(jìn)不去了!因?yàn)槲覀兡壳皼]有登錄的角色,因?yàn)檎?qǐng)求需要登錄的角色擁有對(duì)應(yīng)的權(quán)限才可以
5.在 configure() 方法中加入以下配置,開啟自動(dòng)配置的登錄功能
//沒有權(quán)限默認(rèn)回到登錄頁面,需要開啟登錄的頁面 http.formLogin();
6.定義認(rèn)證規(guī)則,重寫 configure(AuthenticationManagerBuilder auth) 方法
- 要將前端傳過來的密碼進(jìn)行某種方式加密,否則就無法登錄

/**
* 要將前端傳過來的密碼進(jìn)行某種方式加密,否則就無法登錄
* @param auth
* @throws Exception
*/
@Override //認(rèn)證 密碼編碼 PassWordEncoder
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("yang").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}6. 權(quán)限控制和注銷
開啟自動(dòng)配置的注銷的功能
@EnableWebSecurity //AOP 攔截器
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
//首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問
//請(qǐng)求授權(quán)的規(guī)則
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//注銷 跳到首頁
http.logout().logoutSuccessUrl("/");在前端,增加一個(gè)注銷的按鈕, index.html 導(dǎo)航欄中
<a class="item" th:href="@{/logout}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<i class="address card icon"></i> 注銷
</a>不同身份的用戶,顯示不同內(nèi)容
- 用戶沒有登錄的時(shí)候,導(dǎo)航欄上只顯示登錄按鈕,用戶登錄之后,導(dǎo)航欄可以顯示登錄的用戶信息及注銷按鈕
- sec:authorize=“isAuthenticated()”:是否認(rèn)證登錄! 來顯示不同的頁面
- 導(dǎo)入依賴
<!--thymeleaf-security整合包-->
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>在前端頁面導(dǎo)入命名空間
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
修改導(dǎo)航欄,增加認(rèn)證判斷
<!--index.html-->
<!--登錄注銷-->
<div class="right menu">
<!--如果未登錄-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}" rel="external nofollow" rel="external nofollow" >
<i class="address card icon"></i> 登錄
</a>
</div>
<!--如果登陸了:用戶名、注銷-->
<!--當(dāng)?shù)卿?、用戶名、退出同時(shí)出現(xiàn),是spring版本太高了,降至2.0.9/7 就能正常了-->
<div sec:authorize="isAuthenticated()">
<a class="item" >
用戶名:<span sec:authentication="name"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<i class="sign-out icon"></i> 注銷
</a>
</div>
</div>如果注銷404,就是因?yàn)樗J(rèn)防止csrf跨站請(qǐng)求偽造,因?yàn)闀?huì)產(chǎn)生安全問題,我們可以將請(qǐng)求改為post表單提交,或者在spring security中關(guān)閉csrf功能;我們?cè)囋?/blockquote>//鏈?zhǔn)骄幊? @Override protected void configure(HttpSecurity http) throws Exception { //首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問 //請(qǐng)求授權(quán)的規(guī)則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認(rèn)回到登錄頁面,需要開啟登錄的頁面 http.formLogin().loginProcessingUrl("/login"); //防止網(wǎng)站攻擊 csrf--跨站請(qǐng)求偽造 http.csrf().disable(); //注銷 跳到首頁 http.logout().logoutSuccessUrl("/"); }角色功能塊
<div> <br> <div class="ui three column stackable grid"> <div class="column" sec:authorize="hasRole('vip1')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 1</h5> <hr> <div><a th:href="@{/level1/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-1</a></div> <div><a th:href="@{/level1/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-2</a></div> <div><a th:href="@{/level1/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip2')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 2</h5> <hr> <div><a th:href="@{/level2/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-1</a></div> <div><a th:href="@{/level2/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-2</a></div> <div><a th:href="@{/level2/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip3')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 3</h5> <hr> <div><a th:href="@{/level3/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-1</a></div> <div><a th:href="@{/level3/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-2</a></div> <div><a th:href="@{/level3/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-3</a></div> </div> </div> </div> </div> </div> </div>7. 記住登錄
開啟記住我功能
//鏈?zhǔn)骄幊? @Override protected void configure(HttpSecurity http) throws Exception { //首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問 //請(qǐng)求授權(quán)的規(guī)則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認(rèn)回到登錄頁面,需要開啟登錄的頁面 http.formLogin().loginProcessingUrl("/login");// 登陸表單提交請(qǐng)求 //防止網(wǎng)站攻擊 csrf--跨站請(qǐng)求偽造 http.csrf().disable(); //注銷 跳到首頁 http.logout().logoutSuccessUrl("/"); //開啟記住功能 cookie默認(rèn)保持兩周 自定義接收前端的參數(shù) http.rememberMe().rememberMeParameter("remember"); }
原理:登錄成功后,將cookie發(fā)送給瀏覽器保存,以后登錄帶上這個(gè)cookie,只要通過檢查就可以免登錄了。如果點(diǎn)擊注銷,則會(huì)刪除這個(gè)cookie8. 定制登錄頁面
在登錄頁配置后面指定.loginPage
<form th:action="@{/login}" method="post"> <div class="field"> <label>Username</label> <div class="ui left icon input"> <input type="text" placeholder="Username" name="username"> <i class="user icon"></i> </div> </div> <div class="field"> <label>Password</label> <div class="ui left icon input"> <input type="password" name="password"> <i class="lock icon"></i> </div> </div> <div class="field"> <input type="checkbox" name="remember"> 記住我 </div> <input type="submit" class="ui blue submit button"/> </form>login.html 配置提交請(qǐng)求及方式,方式必須為post
//鏈?zhǔn)骄幊? @Override protected void configure(HttpSecurity http) throws Exception { //首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問 //請(qǐng)求授權(quán)的規(guī)則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認(rèn)回到登錄頁面,需要開啟登錄的頁面 http.formLogin().loginPage("/toLogin"); //防止網(wǎng)站攻擊 csrf--跨站請(qǐng)求偽造 http.csrf().disable(); //注銷 跳到首頁 http.logout().logoutSuccessUrl("/"); //開啟記住功能 cookie默認(rèn)保持兩周 自定義接收前端的參數(shù) http.rememberMe().rememberMeParameter("remember"); }配置接收登錄的用戶名和密碼的參數(shù)!
http.formLogin() .usernameParameter("username") .passwordParameter("password") .loginPage("/toLogin") .loginProcessingUrl("/login"); // 登陸表單提交請(qǐng)求在登錄頁增加記住我的多選框
<input type="checkbox" name="remember"> 記住我后端驗(yàn)證處理
//定制記住我的參數(shù)! http.rememberMe().rememberMeParameter("remember");三 完整代碼
3.1 pom配置文件
<dependencies> <!--thymeleaf-security整合包--> <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.4.RELEASE</version> </dependency> <!--thymeleaf--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>3.2 RouterController.java
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * @author 緣友一世 * date 2022/9/11-21:56 */ @Controller public class RouterController { @RequestMapping({"/","/index"}) public String index() { return "index"; } @RequestMapping("/toLogin") public String toLogin() { return "views/login"; } @RequestMapping("/level1/{id}") public String level1(@PathVariable("id") int id) { return "views/level1/"+id; } @RequestMapping("/level2/{id}") public String level2(@PathVariable("id") int id) { return "views/level2/"+id; } @RequestMapping("/level3/{id}") public String level3(@PathVariable("id") int id) { return "views/level3/"+id; } }3.3 SecurityConfig.java
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.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** * @author 緣友一世 * date 2022/9/11-22:13 */ @EnableWebSecurity //AOP 攔截器 public class SecurityConfig extends WebSecurityConfigurerAdapter { //鏈?zhǔn)骄幊? @Override protected void configure(HttpSecurity http) throws Exception { //首頁所有人可以訪問,功能頁面只有對(duì)應(yīng)的人可以訪問 //請(qǐng)求授權(quán)的規(guī)則 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //沒有權(quán)限默認(rèn)回到登錄頁面,需要開啟登錄的頁面 http.formLogin().usernameParameter("username") .passwordParameter("password") .loginPage("/toLogin") .loginProcessingUrl("/login"); //防止網(wǎng)站攻擊 csrf--跨站請(qǐng)求偽造 http.csrf().disable(); //注銷 跳到首頁 http.logout().logoutSuccessUrl("/"); //開啟記住功能 cookie默認(rèn)保持兩周 自定義接收前端的參數(shù) http.rememberMe().rememberMeParameter("remember"); } /** * 要將前端傳過來的密碼進(jìn)行某種方式加密,否則就無法登錄 * @param auth * @throws Exception */ @Override //認(rèn)證 密碼編碼 PassWordEncoder protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("yang").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }3.4 login.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>登錄</title> <!--semantic-ui--> <link rel="external nofollow" rel="external nofollow" rel="stylesheet"> </head> <body> <!--主容器--> <div class="ui container"> <div class="ui segment"> <div style="text-align: center"> <h1 class="header">登錄</h1> </div> <div class="ui placeholder segment"> <div class="ui column very relaxed stackable grid"> <div class="column"> <div class="ui form"> <form th:action="@{/login}" method="post"> <div class="field"> <label>Username</label> <div class="ui left icon input"> <input type="text" placeholder="Username" name="username"> <i class="user icon"></i> </div> </div> <div class="field"> <label>Password</label> <div class="ui left icon input"> <input type="password" name="password"> <i class="lock icon"></i> </div> </div> <div class="field"> <input type="checkbox" name="remember"> 記住我 </div> <input type="submit" class="ui blue submit button"/> </form> </div> </div> </div> </div> <div style="text-align: center"> <div class="ui label"> </i>注冊(cè) </div> <br><br> <small>blog.kuangstudy.com</small> </div> <div class="ui segment" style="text-align: center"> <h3>Spring Security Study</h3> </div> </div> </div> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script> <script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body> </html>3.5 index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>首頁</title> <!--semantic-ui--> <link rel="external nofollow" rel="external nofollow" rel="stylesheet"> <link th:href="@{/qinjiang/css/qinstyle.css}" rel="external nofollow" rel="stylesheet"> </head> <body> <!--主容器--> <div class="ui container"> <div class="ui segment" id="index-header-nav" th:fragment="nav-menu"> <div class="ui secondary menu"> <a class="item" th:href="@{/index}" rel="external nofollow" >首頁</a> <!--登錄注銷--> <div class="right menu"> <!--如果未登錄--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/toLogin}" rel="external nofollow" rel="external nofollow" > <i class="address card icon"></i> 登錄 </a> </div> <!--如果登陸了:用戶名、注銷--> <!--當(dāng)?shù)卿洝⒂脩裘?、退出同時(shí)出現(xiàn),是spring版本太高了,降至2.0.9/7 就能正常了--> <div sec:authorize="isAuthenticated()"> <a class="item" > 用戶名:<span sec:authentication="name"></span> </a> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <i class="sign-out icon"></i> 注銷 </a> </div> <!--已登錄 <a th:href="@{/usr/toUserCenter}" rel="external nofollow" > <i class="address card icon"></i> admin </a> --> </div> </div> </div> <div class="ui segment" style="text-align: center"> <h3>Spring Security Study</h3> </div> <div> <br> <div class="ui three column stackable grid"> <div class="column" sec:authorize="hasRole('vip1')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 1</h5> <hr> <div><a th:href="@{/level1/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-1</a></div> <div><a th:href="@{/level1/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-2</a></div> <div><a th:href="@{/level1/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-1-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip2')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 2</h5> <hr> <div><a th:href="@{/level2/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-1</a></div> <div><a th:href="@{/level2/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-2</a></div> <div><a th:href="@{/level2/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-2-3</a></div> </div> </div> </div> </div> <div class="column" sec:authorize="hasRole('vip3')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 3</h5> <hr> <div><a th:href="@{/level3/1}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-1</a></div> <div><a th:href="@{/level3/2}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-2</a></div> <div><a th:href="@{/level3/3}" rel="external nofollow" rel="external nofollow" ><i class="bullhorn icon"></i> Level-3-3</a></div> </div> </div> </div> </div> </div> </div> </div> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script> <script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body> </html>3.6 效果展示
到此這篇關(guān)于Spring Boot中的SpringSecurity學(xué)習(xí)的文章就介紹到這了,更多相關(guān)Spring Boot之SpringSecurity內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Hazelcast Caching 使用和配置詳解
這篇文章主要介紹了Spring Boot Hazelcast Caching 使用和配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
@insert mybatis踩坑記錄,實(shí)體接收前端傳遞的參數(shù)
這篇文章主要介紹了@insert mybatis踩坑記錄,實(shí)體接收前端傳遞的參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
SpringBoot Shiro授權(quán)實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringBoot Shiro授權(quán)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
idea創(chuàng)建SpringBoot自動(dòng)創(chuàng)建Lombok無效果的問題解決方案
這篇文章主要介紹了idea創(chuàng)建SpringBoot自動(dòng)創(chuàng)建Lombok無效果的問題解決方案,感興趣的朋友跟隨小編一起看看吧2024-12-12
Spring?Boot?@Autowired?@Resource屬性賦值時(shí)機(jī)探究
這篇文章主要為大家介紹了Spring?Boot?@Autowired?@Resource屬性賦值時(shí)機(jī),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
idea中Maven鏡像源詳細(xì)配置步驟記錄(對(duì)所有項(xiàng)目)
Maven是一個(gè)能使我們的java程序開發(fā)節(jié)省時(shí)間和精力,是開發(fā)變得相對(duì)簡(jiǎn)單,還能使開發(fā)規(guī)范化的工具,下面這篇文章主要給大家介紹了關(guān)于idea中Maven鏡像源詳細(xì)配置(對(duì)所有項(xiàng)目)的相關(guān)資料,需要的朋友可以參考下2023-05-05
關(guān)于springboot加載yml配置文件的no字段自動(dòng)轉(zhuǎn)義問題
這篇文章主要介紹了關(guān)于springboot加載yml配置文件的no字段自動(dòng)轉(zhuǎn)義問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot整合 xxl-job的項(xiàng)目實(shí)踐
XL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),用于解決分布式系統(tǒng)中的任務(wù)調(diào)度和管理問題,它包括調(diào)度中心、執(zhí)行器和Web管理控制臺(tái),本文就來介紹一下springboot整合 xxl-job的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-09-09



