詳解springboot?springsecuroty中的注銷和權(quán)限控制問(wèn)題
上篇文章給大家介紹了springboot對(duì)接第三方微信授權(quán)及獲取用戶的頭像和昵稱等等
1 賬戶注銷
1.1 在SecurityConfig中加入開(kāi)啟注銷功能的代碼
src/main/java/com/lv/config/SecurityConfig.java
package com.lv.config;
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;
//AOP : 攔截器!
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授權(quán)
@Override
public void configure(HttpSecurity http) throws Exception {
//首頁(yè)所有人都可以訪問(wèn),功能頁(yè)只有對(duì)應(yīng)的有權(quán)限的人才能訪問(wèn)
//請(qǐng)求授權(quán)的規(guī)則~(鏈?zhǔn)骄幊?
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//沒(méi)有權(quán)限默認(rèn)會(huì)跳轉(zhuǎn)到登錄頁(yè),需要開(kāi)啟登錄頁(yè)面
http.formLogin();
//注銷,開(kāi)啟了注銷功能,跳到首頁(yè)
http.logout().logoutSuccessUrl("/");
//防止跨站工具, get,post
http.csrf().disable();//關(guān)閉csrf功能,注銷失敗可能的原因
}
//認(rèn)證,springboot 2.1.x 可以直接使用
//密碼編碼:PasswordEncoder
//在Spring Security 5.0+ 新增了很多加密方法~
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//這些數(shù)據(jù)正常應(yīng)該從數(shù)據(jù)庫(kù)中讀
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("lv").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}1.2 在index.html 添加注銷的按鈕
src/main/resources/templates/index.html
<!--登錄注銷-->
<div class="right menu">
<div>
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i>登錄
</a>
</div>
<div>
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i>注銷
</a>
</div>
</div>1.3 啟動(dòng)項(xiàng)目測(cè)試
訪問(wèn)登錄頁(yè)面,登錄 guest 賬戶,該賬戶可以訪問(wèn) level1的頁(yè)面

登錄成功后,點(diǎn)擊 level1的鏈接,成功跳轉(zhuǎn)到 level 頁(yè)面,然后點(diǎn)擊注銷按鈕

彈回到首頁(yè),再次點(diǎn)擊點(diǎn)擊 level1 頁(yè)面

跳轉(zhuǎn)到了登錄頁(yè)面

說(shuō)明賬戶注銷成功
2 權(quán)限控制
2.1 導(dǎo)入springsecurity和thymeleaf的整合依賴
pom.xml
<!-- springSecurity和thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>2.2 springboot版本降級(jí)
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>必須將springboot的版本降到2.0.9以下,否則 sec:authorize="isAuthenticated()" 不會(huì)生效.版本降低后,需要手動(dòng)導(dǎo)入junit依賴,否則測(cè)試類會(huì)報(bào)錯(cuò)
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>2.3 引入約束
在index.html的頭文件中添加springsecurity和thymeleaf的整合約束
src/main/resources/templates/index.html
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">2.4 修改頁(yè)面代碼
主要修改兩部分,一部分是登錄狀態(tài)下顯示用戶名,和注銷按鈕,未登錄顯示登錄按鈕 通過(guò) sec:authorize="isAuthenticated()" 實(shí)現(xiàn).另一部分是根據(jù)登錄用戶的權(quán)限顯示不同的頁(yè)面菜單,通過(guò) sec:authorize="hasRole('vip1')" 實(shí)現(xiàn).
src/main/resources/templates/index.html
<!DOCTYPE html>
<html 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>首頁(yè)</title>
<!--semantic-ui-->
<link 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}">首頁(yè)</a>
<!--登錄注銷-->
<div class="right menu">
<!--如果未登錄:顯示登錄按鈕-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i>登錄
</a>
</div>
<!--如果已登錄:顯示用戶名和注銷按鈕-->
<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>
</div>
</div>
<div class="ui segment" style="text-align: center">
<h3>Spring Security Study by 秦疆</h3>
</div>
<div>
<br>
<div class="ui three column stackable grid">
<!--菜單根據(jù)用戶的角色動(dòng)態(tài)實(shí)現(xiàn)-->
<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>2.5 重啟程序測(cè)試
未登錄頁(yè)面

登錄 lv 用戶的頁(yè)面

登錄 geust 用戶的頁(yè)面

登錄 root 用戶的頁(yè)面

頁(yè)面顯示都不同,權(quán)限控制成功實(shí)現(xiàn)
到此這篇關(guān)于springboot-springsecuroty 注銷和權(quán)限控制的文章就介紹到這了,更多相關(guān)springboot注銷和權(quán)限控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷功能
- SpringBoot--- SpringSecurity進(jìn)行注銷權(quán)限控制的配置方法
- Vue+springboot批量刪除功能實(shí)現(xiàn)代碼
- springboot和vue前后端交互的實(shí)現(xiàn)示例
- SpringBoot3結(jié)合Vue3實(shí)現(xiàn)用戶登錄功能
- 基于SpringBoot和Vue3的博客平臺(tái)的用戶注冊(cè)與登錄功能實(shí)現(xiàn)
- SpringBoot和Vue.js實(shí)現(xiàn)的前后端分離的用戶權(quán)限管理系統(tǒng)
- 詳解SpringBoot項(xiàng)目整合Vue做一個(gè)完整的用戶注冊(cè)功能
- Vue結(jié)合Springboot實(shí)現(xiàn)用戶列表單頁(yè)面(前后端分離)
- vue+springboot用戶注銷功能實(shí)現(xiàn)代碼
相關(guān)文章
基于java語(yǔ)言實(shí)現(xiàn)快遞系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于java語(yǔ)言實(shí)現(xiàn)快遞系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
MyBatisPlus中事務(wù)處理的實(shí)現(xiàn)
本文主要介紹了MyBatisPlus中事務(wù)處理的實(shí)現(xiàn),包括事務(wù)的開(kāi)啟、提交、回滾等操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表
這篇文章主要為大家詳細(xì)介紹了Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Java WebService 簡(jiǎn)單實(shí)例(附實(shí)例代碼)
本篇文章主要介紹了Java WebService 簡(jiǎn)單實(shí)例(附實(shí)例代碼), Web Service 是一種新的web應(yīng)用程序分支,他們是自包含、自描述、模塊化的應(yīng)用,可以發(fā)布、定位、通過(guò)web調(diào)用。有興趣的可以了解一下2017-01-01
SpringBoot如何整合redis實(shí)現(xiàn)過(guò)期key監(jiān)聽(tīng)事件
這篇文章主要介紹了SpringBoot如何整合redis實(shí)現(xiàn)過(guò)期key監(jiān)聽(tīng)事件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

