天天看點

Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼

文章目錄

  • 一 SpringSecurity簡介
  • 二 實戰示範
    • 0. 環境 介紹
    • 1. 建立一個初始的springboot項目
    • 2. 導入thymeleaf依賴
    • 3. 導入靜态資源
    • 4. 編寫controller跳轉
    • 5. 認證和授權
    • 6. 權限控制和登出
    • 7. 記住登入
    • 8. 定制登入頁面
  • 三 完整代碼
    • 3.1 pom配置檔案
    • 3.2 RouterController.java
    • 3.3 SecurityConfig.java
    • 3.4 login.html
    • 3.5 index.html
    • 3.6 效果展示

一 SpringSecurity簡介

  • Web開發中,雖然安全屬于非共功能性需求,但也是應用非常重要的一部分。
  • 如果開發後期才考慮安全問題,就會有兩方面的弊處:
    • 一方面,應用存在嚴重的安全漏洞,無法滿足使用者需求,可能造成使用者隐私資料被攻擊者竊取
    • 另一方面,應用的基本架構已經确定,要修複安全漏洞,可能需要對系統的架構做出比較重大的調整,會需要更多的開發時間,影響應用的釋出程序
  • 結論:從應用開發的第一天就要把安全相關的因素考慮進來,并持續在整個應用的開發過程中
  • Spring Security是一個功能強大且高度可定制的身份驗證和通路控制架構。它實際上是保護基于spring的應用程式的标準
  • Spring Security是一個架構,側重于為Java應用程式提供身份驗證和授權。
  • Spring安全性的真正強大之處在于它可以輕松地擴充以滿足定制需求
  • Spring 是一個非常流行和成功的 Java 應用開發架構。Spring Security 基于 Spring 架構,提供了一套Web 應用安全性的完整解決方案。一般來說,Web 應用的安全性包括使用者認證(Authentication)和使用者授權(Authorization)兩個部分。
    • 使用者認證指的是驗證某個使用者是否為系統中的合法主體,也就是說使用者能否通路該系統。使用者認證一般要求使用者提供使用者名和密碼。系統通過校驗使用者名和密碼來完成認證過程。
    • 使用者授權指的是驗證某個使用者是否有權限執行某個操作。在一個系統中,不同使用者所具有的權限是不同的。比如對一個檔案來說,有的使用者隻能進行讀取,而有的使用者可以進行修改。一般來說,系統會為不同的使用者配置設定不同的角色,而每個角色則對應一系列的權限。
    • 在使用者認證方面,SpringSecurity 架構支援主流的認證方式,包括==HTTP 基本認證、HTTP 表單驗證、HTTP 摘要認證、OpenID和LDAP ==等。
    • 在使用者授權方面,Spring Security 提供了基于角色的通路控制和通路控制清單(Access Control List,ACL),可以對應用中的領域對象進行細粒度的控制。
  • Spring Security 是針對Spring項目的安全架構,也是Spring Boot底層安全子產品預設的技術選型,可以實作強大的Web安全控制,對于安全控制,僅需要引入 spring-boot-starter-security 子產品,進行少量的配置,即可實作強大的安全管理!
  • 幾個重要的類
    • WebSecurityConfigurerAdapter: 自定義Security政策
    • AuthenticationManagerBuilder:自定義認證政策
    • @EnableWebSecurity:開啟WebSecurity模式
  • Spring Security的兩個主要目标是 “認證” 和 “授權”(通路控制)
  • “認證”(Authentication)
    • 身份驗證是關于驗證您的憑據,如使用者名/使用者ID和密碼,以驗證您的身份。
    • 身份驗證通常通過使用者名和密碼完成,有時與身份驗證因素結合使用。
  • “授權” (Authorization)
    • 授權發生在系統成功驗證您的身份後,最終會授予您通路資源(如資訊,檔案,資料庫,資金,位置,幾乎任何内容)的完全權限。

二 實戰示範

0. 環境 介紹

  • jdk 1.8
  • Spring Boot 2.0.9.RELEASE

1. 建立一個初始的springboot項目

Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼

2. 導入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. 導入靜态資源

Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼

4. 編寫controller跳轉

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. 認證和授權

  • Spring Security 增加上認證和授權的功能
  1. 引入Spring Security子產品
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
           
  1. 編寫 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 {
    //鍊式程式設計
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       
    }
           
  1. 定制請求的授權規則
//鍊式程式設計
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首頁所有人可以通路,功能頁面隻有對應的人可以通路
        //請求授權的規則
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
    }
           
  1. 測試一下:發現除了首頁都進不去了!因為我們目前沒有登入的角色,因為請求需要登入的角色擁有對應的權限才可以
  2. 在 configure() 方法中加入以下配置,開啟自動配置的登入功能
//沒有權限預設回到登入頁面,需要開啟登入的頁面
http.formLogin();
           
  1. 定義認證規則,重寫 configure(AuthenticationManagerBuilder auth) 方法
    • 要将前端傳過來的密碼進行某種方式加密,否則就無法登入
      Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼
/**
     * 要将前端傳過來的密碼進行某種方式加密,否則就無法登入
     * @param auth
     * @throws Exception
     */
    @Override //認證 密碼編碼 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. 權限控制和登出

  1. 開啟自動配置的登出的功能
    @EnableWebSecurity //AOP 攔截器
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        //鍊式程式設計
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //首頁所有人可以通路,功能頁面隻有對應的人可以通路
            //請求授權的規則
            http.authorizeRequests().antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
            //登出 跳到首頁
            http.logout().logoutSuccessUrl("/");
       
               
  2. 在前端,增加一個登出的按鈕, index.html 導航欄中
    <a class="item" th:href="@{/logout}">
    	<i class="address card icon"></i> 登出
    </a>
               
  3. 不同身份的使用者,顯示不同内容
    • 使用者沒有登入的時候,導航欄上隻顯示登入按鈕,使用者登入之後,導航欄可以顯示登入的使用者資訊及登出按鈕
    • sec:authorize=“isAuthenticated()”:是否認證登入! 來顯示不同的頁面
    • 導入依賴
    <!--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>
               
    • 在前端頁面導入命名空間
    <html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
               
    • 修改導航欄,增加認證判斷
    <!--index.html-->
    <!--登入登出-->
                <div class="right menu">
                    <!--如果未登入-->
                    <div sec:authorize="!isAuthenticated()">
                        <a class="item" th:href="@{/toLogin}">
                            <i class="address card icon"></i> 登入
                        </a>
                    </div>
    
                    <!--如果登陸了:使用者名、登出-->
                    <!--當登入、使用者名、退出同時出現,是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}">
                            <i class="sign-out icon"></i> 登出
                        </a>
                    </div>
                </div>
               
  • 如果登出404,就是因為它預設防止csrf跨站請求僞造,因為會産生安全問題,我們可以将請求改為post表單送出,或者在spring security中關閉csrf功能;我們試試
//鍊式程式設計
	    @Override
	    protected void configure(HttpSecurity http) throws Exception {
	        //首頁所有人可以通路,功能頁面隻有對應的人可以通路
	        //請求授權的規則
	        http.authorizeRequests().antMatchers("/").permitAll()
	                .antMatchers("/level1/**").hasRole("vip1")
	                .antMatchers("/level2/**").hasRole("vip2")
	                .antMatchers("/level3/**").hasRole("vip3");
	        //沒有權限預設回到登入頁面,需要開啟登入的頁面
	        http.formLogin().loginProcessingUrl("/login");
	
	        //防止網站攻擊 csrf--跨站請求僞造
	        http.csrf().disable();
	        //登出 跳到首頁
	        http.logout().logoutSuccessUrl("/");
	    }
           
  1. 角色功能塊
    <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}"><i class="bullhorn icon"></i> Level-1-1</a></div>
                                <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
                                <div><a th:href="@{/level1/3}"><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}"><i class="bullhorn icon"></i> Level-2-1</a></div>
                                <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
                                <div><a th:href="@{/level2/3}"><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}"><i class="bullhorn icon"></i> Level-3-1</a></div>
                                <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
                                <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
                            </div>
                        </div>
                    </div>
                </div>
    
            </div>
        </div>
               

7. 記住登入

  1. 開啟記住我功能
    //鍊式程式設計
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //首頁所有人可以通路,功能頁面隻有對應的人可以通路
            //請求授權的規則
            http.authorizeRequests().antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
            //沒有權限預設回到登入頁面,需要開啟登入的頁面
            http.formLogin().loginProcessingUrl("/login");// 登陸表單送出請求
    
            //防止網站攻擊 csrf--跨站請求僞造
            http.csrf().disable();
            //登出 跳到首頁
            http.logout().logoutSuccessUrl("/");
            //開啟記住功能 cookie預設保持兩周 自定義接收前端的參數
            http.rememberMe().rememberMeParameter("remember");
        }
               
Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼
  • 原理:登入成功後,将cookie發送給浏覽器儲存,以後登入帶上這個cookie,隻要通過檢查就可以免登入了。如果點選登出,則會删除這個cookie

8. 定制登入頁面

  1. 在登入頁配置後面指定.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>
               
  2. login.html 配置送出請求及方式,方式必須為post
    //鍊式程式設計
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //首頁所有人可以通路,功能頁面隻有對應的人可以通路
            //請求授權的規則
            http.authorizeRequests().antMatchers("/").permitAll()
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
            //沒有權限預設回到登入頁面,需要開啟登入的頁面
            http.formLogin().loginPage("/toLogin");
    
            //防止網站攻擊 csrf--跨站請求僞造
            http.csrf().disable();
            //登出 跳到首頁
            http.logout().logoutSuccessUrl("/");
            //開啟記住功能 cookie預設保持兩周 自定義接收前端的參數
            http.rememberMe().rememberMeParameter("remember");
        }
               
  3. 配置接收登入的使用者名和密碼的參數!
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
.loginProcessingUrl("/login"); // 登陸表單送出請求
           
  1. 在登入頁增加記住我的多選框
<input type="checkbox" name="remember"> 記住我
           
  1. 後端驗證處理
//定制記住我的參數!
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 {
    //鍊式程式設計
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首頁所有人可以通路,功能頁面隻有對應的人可以通路
        //請求授權的規則
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //沒有權限預設回到登入頁面,需要開啟登入的頁面
        http.formLogin().usernameParameter("username")
                        .passwordParameter("password")
                        .loginPage("/toLogin")
                		.loginProcessingUrl("/login");

        //防止網站攻擊 csrf--跨站請求僞造
        http.csrf().disable();
        //登出 跳到首頁
        http.logout().logoutSuccessUrl("/");
        //開啟記住功能 cookie預設保持兩周 自定義接收前端的參數
        http.rememberMe().rememberMeParameter("remember");
    }
	
	/**
     * 要将前端傳過來的密碼進行某種方式加密,否則就無法登入
     * @param auth
     * @throws Exception
     */
    @Override //認證 密碼編碼 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 href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" 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 href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
    <link th:href="@{/qinjiang/css/qinstyle.css}" 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}">首頁</a>

            <!--登入登出-->
            <div class="right menu">
                <!--如果未登入-->
                <div sec:authorize="!isAuthenticated()">
                    <a class="item" th:href="@{/toLogin}">
                        <i class="address card icon"></i> 登入
                    </a>
                </div>

                <!--如果登陸了:使用者名、登出-->
                <!--當登入、使用者名、退出同時出現,是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}">
                        <i class="sign-out icon"></i> 登出
                    </a>
                </div>
                <!--已登入
                <a th:href="@{/usr/toUserCenter}" target="_blank" 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}"><i class="bullhorn icon"></i> Level-1-1</a></div>
                            <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
                            <div><a th:href="@{/level1/3}"><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}"><i class="bullhorn icon"></i> Level-2-1</a></div>
                            <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
                            <div><a th:href="@{/level2/3}"><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}"><i class="bullhorn icon"></i> Level-3-1</a></div>
                            <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
                            <div><a th:href="@{/level3/3}"><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 效果展示

Spring Boot之SpringSecurity學習一 SpringSecurity簡介二 實戰示範三 完整代碼

繼續閱讀