Spring Boot中的SpringSecurity基礎(chǔ)教程
一 SpringSecurity簡介
- Web開發(fā)中,雖然安全屬于非共功能性需求,但也是應(yīng)用非常重要的一部分。
- 如果開發(fā)后期才考慮安全問題,就會有兩方面的弊處:
- 一方面,應(yīng)用存在嚴(yán)重的安全漏洞,無法滿足用戶需求,可能造成用戶隱私數(shù)據(jù)被攻擊者竊取
- 另一方面,應(yīng)用的基本架構(gòu)已經(jīng)確定,要修復(fù)安全漏洞,可能需要對系統(tǒng)的架構(gòu)做出比較重大的調(diào)整,會需要更多的開發(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)限是不同的。比如對一個(gè)文件來說,有的用戶只能進(jìn)行讀取,而有的用戶可以進(jìn)行修改。一般來說,系統(tǒng)會為不同的用戶分配不同的角色,而每個(gè)角色則對應(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),可以對應(yīng)用中的領(lǐng)域?qū)ο筮M(jìn)行細(xì)粒度的控制。
- Spring Security 是針對Spring項(xiàng)目的安全框架,也是Spring Boot底層安全模塊默認(rèn)的技術(shù)選型,可以實(shí)現(xiàn)強(qiáng)大的Web安全控制,對于安全控制,僅需要引入 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)證您的身份后,最終會授予您訪問資源(如信息,文件,數(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.定制請求的授權(quán)規(guī)則
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
//首頁所有人可以訪問,功能頁面只有對應(yīng)的人可以訪問
//請求授權(quán)的規(guī)則
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
}4.測試一下:發(fā)現(xiàn)除了首頁都進(jìn)不去了!因?yàn)槲覀兡壳皼]有登錄的角色,因?yàn)檎埱笮枰卿浀慕巧珦碛袑?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 {
//首頁所有人可以訪問,功能頁面只有對應(yī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跨站請求偽造,因?yàn)闀a(chǎn)生安全問題,我們可以將請求改為post表單提交,或者在spring security中關(guān)閉csrf功能;我們試試
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
//首頁所有人可以訪問,功能頁面只有對應(yī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--跨站請求偽造
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 {
//首頁所有人可以訪問,功能頁面只有對應(yī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--跨站請求偽造
http.csrf().disable();
//注銷 跳到首頁
http.logout().logoutSuccessUrl("/");
//開啟記住功能 cookie默認(rèn)保持兩周 自定義接收前端的參數(shù)
http.rememberMe().rememberMeParameter("remember");
}
原理:登錄成功后,將cookie發(fā)送給瀏覽器保存,以后登錄帶上這個(gè)cookie,只要通過檢查就可以免登錄了。如果點(diǎn)擊注銷,則會刪除這個(gè)cookie
8. 定制登錄頁面
在登錄頁配置后面指定.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 配置提交請求及方式,方式必須為post
//鏈?zhǔn)骄幊?
@Override
protected void configure(HttpSecurity http) throws Exception {
//首頁所有人可以訪問,功能頁面只有對應(yī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--跨站請求偽造
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"); // 登陸表單提交請求在登錄頁增加記住我的多選框
<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 {
//首頁所有人可以訪問,功能頁面只有對應(yī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--跨站請求偽造
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>注冊
</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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談java運(yùn)用注解實(shí)現(xiàn)對類中的方法檢測的工具
這篇文章主要介紹了淺談java運(yùn)用注解實(shí)現(xiàn)對類中的方法檢測的工具,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
RocketMQ設(shè)計(jì)之故障規(guī)避機(jī)制
這篇文章主要介紹了RocketMQ設(shè)計(jì)之故障規(guī)避機(jī)制,故障規(guī)避機(jī)制就是用來解決當(dāng)Broker出現(xiàn)故障,Producer不能及時(shí)感知而導(dǎo)致消息發(fā)送失敗的問題,下面詳細(xì)介紹需要的小伙伴可以參考一下2022-03-03
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之嘟嘟健身房管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)健身房管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
java開發(fā)中如何使用JVisualVM進(jìn)行性能分析
JVisualVM是由Sun提供的性能分析工具,如此強(qiáng)大的后盾怎能不強(qiáng)大?在Jdk6.0以后的版本中是自帶的,配置好環(huán)境變量然后在運(yùn)行中輸入“JVisualVm”或直接到Jdk的安裝目錄的Bin目錄下找到運(yùn)行程序即可運(yùn)行。如果是用Jdk1.5或以前版本的朋友就得要單獨(dú)安裝了2015-12-12
自己動(dòng)手實(shí)現(xiàn)mybatis動(dòng)態(tài)sql的方法
下面小編就為大家分享一篇自己動(dòng)手實(shí)現(xiàn)mybatis動(dòng)態(tài)sql的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
java web項(xiàng)目里ehcache.xml介紹
java web項(xiàng)目里ehcache.xml介紹,需要的朋友可以參考一下2013-03-03
spring聲明式事務(wù) @Transactional 不回滾的多種情況以及解決方案
本文主要介紹了spring聲明式事務(wù) @Transactional 不回滾的多種情況以及解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
JavaSwing GridLayout 網(wǎng)格布局的實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaSwing GridLayout 網(wǎng)格布局的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

