SpringBoot Security安裝配置及Thymeleaf整合
功能:解決web站點(diǎn)的登錄,權(quán)限驗(yàn)證,授權(quán)等功能
優(yōu)點(diǎn):在不影響站點(diǎn)業(yè)務(wù)代碼,可以權(quán)限的授權(quán)與驗(yàn)證橫切到業(yè)務(wù)中
1、要添加的依賴
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--security 和 thymeleaf 整合包-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、Security 下授權(quán)與驗(yàn)證的簡(jiǎn)單配置(Security下有登錄,注銷,記住我等功能,可以快速集成到自己的login頁(yè)上)
Tis:如果template頁(yè)中使用了 Frame頁(yè),默認(rèn)是不能訪問的,需要添加 http.headers().frameOptions().sameOrigin();
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授權(quán)
@Override
protected void configure(HttpSecurity http) throws Exception {
//請(qǐng)求授權(quán)的規(guī)則
http.authorizeRequests()
//.antMatchers("/tologin").permitAll() //登錄頁(yè)所有人都可以訪問
//.antMatchers("/admin/**").hasRole("admin1")
.antMatchers("/admin/list").hasRole("admin1")
.antMatchers("/admin/role").hasRole("admin1")
.antMatchers("/admin/cate").hasRole("admin2")
.antMatchers("/admin/rule").hasRole("admin2");
// 項(xiàng)目里面使用了springSecurity spring Security下,X-Frame-Options默認(rèn)為DENY,非spring Security環(huán)境下,X-Frame-Options的默認(rèn)大多也是DENY,這種情況下,瀏覽器拒絕當(dāng)前頁(yè)面加載任何Frame頁(yè)面
http.headers().frameOptions().sameOrigin();
//登錄頁(yè)(Security默認(rèn)有一個(gè)登錄頁(yè))
http.formLogin().permitAll()
.loginPage("/tologin") //指定自定義的登錄頁(yè)地址
.successForwardUrl("/admin/index") //登錄成功跳轉(zhuǎn)地址
.usernameParameter("username").passwordParameter("password");//匹配自定義登錄頁(yè)的name元素名稱
// 開啟注銷功能,跳轉(zhuǎn)到登錄頁(yè)
http.csrf().disable(); //退出失敗可能能的原因
http.logout().logoutSuccessUrl("/tologin");
//開啟記住我功能,cookie 默認(rèn)保存14天
http.rememberMe()
.rememberMeParameter("remember");//匹配自定義登錄頁(yè)的name元素名稱
}
//認(rèn)證
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())//密碼加密方式(有些版本的Security必須要指定)
.withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3")
.and()
.withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1")
.and()
.withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2");
}
}
3、Security 和 Thymeleaf 頁(yè)面整合(添加依賴:thymeleaf-extras-springsecurity)
<!--加入約束-->
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<!--
sec:authorize="isAuthenticated()" 用戶是否登錄
sec:authorize="hasAnyRole('admin1')" 是否具有某個(gè)角色
sec:authentication="name" 當(dāng)前登錄用戶
sec:authentication="principal.authorities" 當(dāng)前用戶全部角色
-->
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好 您的身份是
<span sec:authentication="principal.authorities"></span>
</h2>
</div>
<li sec:authorize="hasRole('admin2')">
<a onclick="xadmin.add_tab('權(quán)限管理','admin/rule')">
<cite>權(quán)限管理</cite>
</a>
</li>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
將本地JAR文件手動(dòng)添加到Maven本地倉(cāng)庫(kù)的實(shí)現(xiàn)過程
在Java開發(fā)中,使用Maven作為項(xiàng)目管理工具已經(jīng)成為了主流的選擇,Maven提供了強(qiáng)大的依賴管理功能,可以輕松地下載和管理項(xiàng)目所需的庫(kù)和工具,在某些情況下,你可能會(huì)需要將本地下載的JAR文件手動(dòng)添加到Maven的本地倉(cāng)庫(kù)中,這篇博客將詳細(xì)介紹如何實(shí)現(xiàn)這一過程2024-10-10
MyBatis-Plus?updateById更新不了空字符串或null的解決方法
本文主要介紹了MyBatis-Plus?updateById更新不了空字符串或null的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
面試官:詳細(xì)談?wù)凧ava對(duì)象的4種引用方式
這篇文章主要給大家介紹了java面試官常會(huì)問到的,關(guān)于Java對(duì)象的4種引用方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
java執(zhí)行shell并獲取shell輸出日志方式
這篇文章主要介紹了java執(zhí)行shell并獲取shell輸出日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Springboot 整合RabbitMq(用心看完這一篇就夠了)
這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
深入淺析java web log4j 配置及在web項(xiàng)目中配置Log4j的技巧
這篇文章主要介紹了2015-11-11
java網(wǎng)上圖書商城(4)購(gòu)物車模塊1
這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上圖書商城,購(gòu)物車模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
基于Java利用static實(shí)現(xiàn)單例模式
這篇文章主要介紹了基于Java利用static實(shí)現(xiàn)單例模式,當(dāng)在多個(gè)線程同時(shí)觸發(fā)類的初始化過程的時(shí)候static不會(huì)被多次執(zhí)行,下面我們一起進(jìn)入文章看看具體要的原因2022-01-01

