springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例
spring security框架使用
環(huán)境
jdk版本:jdk 17
依賴
<!-- spring boot 版本 3.2.0 -->
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</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-thymeleaf</artifactId>
</dependency>
登錄認(rèn)證
依賴
spring-boot-starter-security后,服務(wù)啟動(dòng)時(shí),控制臺(tái)日志會(huì)輸出自動(dòng)生成的密碼日志信息如下
Using generated security password: 2d9c8697-8ebd-4be7-afea-a6bc922a7d5b This generated password is for development use only. Your security configuration must be updated before running your application in production.
默認(rèn)的用戶名為:
user
依賴
spring-boot-starter-security后,調(diào)用接口時(shí)就會(huì)進(jìn)行登錄認(rèn)證,會(huì)跳轉(zhuǎn)到默認(rèn)的登錄頁(yè)面:/login,該頁(yè)面是框架自帶的- 頁(yè)面加載較慢,樣式加載不成功,但不影響功能使用。原因:會(huì)加載一些無(wú)法訪問的css數(shù)據(jù)
- 輸入默認(rèn)的用戶名和密碼,進(jìn)行登錄認(rèn)證,登錄認(rèn)證成功即可訪問接口數(shù)據(jù)
- 該頁(yè)面可自定義
默認(rèn)的登出頁(yè)面:/logout,該頁(yè)面是框架自帶的
- 頁(yè)面加載較慢,樣式加載不成功
- 該頁(yè)面有一個(gè)確認(rèn)登出按鈕,點(diǎn)擊后即可退出登錄,調(diào)用接口時(shí)會(huì)重新進(jìn)行登錄認(rèn)證
- 該頁(yè)面可自定義
spring security自定義登錄頁(yè)面
使用瀏覽器自帶的登錄頁(yè)面
添加一個(gè)配置類WebSecurityConfig.java,后續(xù)內(nèi)容也會(huì)在此類中進(jìn)行配置
package xin.yangshuai.springsecurity03.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
// @EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 開啟授權(quán)保護(hù)
http.authorizeRequests(new Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry>() {
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry) {
expressionInterceptUrlRegistry
// 對(duì)所有請(qǐng)求開啟授權(quán)保護(hù)
.anyRequest()
// 已經(jīng)認(rèn)證的請(qǐng)求會(huì)被自動(dòng)授權(quán)
.authenticated();
}
});
// 使用基本授權(quán)方式(瀏覽器自帶的登錄頁(yè)面,無(wú)默認(rèn)登出頁(yè))
http.httpBasic(Customizer.withDefaults());
return http.build();
}
}
http.authorizeRequests授權(quán)配置- 此方式由瀏覽器彈出默認(rèn)登錄頁(yè)面進(jìn)行登錄認(rèn)證
@EnableWebSecurity此處不用開啟,依賴時(shí)已經(jīng)自動(dòng)開啟
自定義登錄頁(yè)面
調(diào)整配置類WebSecurityConfig.java
package xin.yangshuai.springsecurity03.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
// @EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// 開啟授權(quán)保護(hù)
http.authorizeRequests(new Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry>() {
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry) {
expressionInterceptUrlRegistry
// 對(duì)所有請(qǐng)求開啟授權(quán)保護(hù)
.anyRequest()
// 已經(jīng)認(rèn)證的請(qǐng)求會(huì)被自動(dòng)授權(quán)
.authenticated();
}
});
// 自定義登錄頁(yè)面
http.formLogin(new Customizer<FormLoginConfigurer<HttpSecurity>>() {
@Override
public void customize(FormLoginConfigurer<HttpSecurity> httpSecurityFormLoginConfigurer) {
// 自定義登錄頁(yè),并且設(shè)置無(wú)需授權(quán)允許訪問
httpSecurityFormLoginConfigurer.loginPage("/login").permitAll();
// 配置自定義表單的用戶名參數(shù),默認(rèn)值:username
httpSecurityFormLoginConfigurer.usernameParameter("myusername");
// 配置自定義表單的密碼參數(shù),默認(rèn)值:password
httpSecurityFormLoginConfigurer.passwordParameter("mypassword");
// 校驗(yàn)失敗時(shí)跳轉(zhuǎn)的地址,默認(rèn)值:/login?error
httpSecurityFormLoginConfigurer.failureUrl("/login?error");
}
});
return http.build();
}
}
loginPage("/login")表示未授權(quán)時(shí),會(huì)跳轉(zhuǎn)到GET /login接口,因此需要對(duì)GET /login接口響應(yīng)自定義的登錄頁(yè)面usernameParameter("myusername")表示可以自定義提交表單參數(shù),在登錄頁(yè)面,請(qǐng)求登錄認(rèn)證接口默認(rèn)是POST /login,用戶名、密碼的參數(shù)名可以自定義failureUrl("/login?error")表示認(rèn)證失敗,會(huì)跳轉(zhuǎn)到GET /login?error接口,可以攜帶參數(shù)用來(lái)信息展示,實(shí)際上也是跳轉(zhuǎn)到了登錄頁(yè)面,只是攜帶了參數(shù)
配置登錄頁(yè)面接口
package xin.yangshuai.springsecurity03.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("login")
public String login(){
return "login";
}
}
配置登錄頁(yè)面
根據(jù)thymeleaf模板引擎,頁(yè)面位置:src/main/resources/templates/login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>
<h1>登錄</h1>
<div th:if="${param.error}">用戶名或密碼錯(cuò)誤</div>
<!--
使用動(dòng)態(tài)參數(shù),th:action="@{/login}"
開啟防止csrf攻擊時(shí),會(huì)自動(dòng)生成_csrf隱藏字段
-->
<form th:action="@{/login}" method="post">
<div>
<input type="text" name="myusername" placeholder="用戶名">
</div>
<div>
<input type="password" name="mypassword" placeholder="密碼">
</div>
<input type="submit" value="登錄">
</form>
</body>
</html>
- 提交表單會(huì)默認(rèn)加上
_csrf參數(shù),防止csrf攻擊,這個(gè)與配置有關(guān) - 表單參數(shù)
myusername、mypassword要與配置保持一致 - 默認(rèn)賬號(hào)是
user,密碼在項(xiàng)目啟動(dòng)時(shí),會(huì)輸出到控制臺(tái)上 - 認(rèn)證失敗后,會(huì)跳回到登錄頁(yè),并且攜帶
error參數(shù),可以根據(jù)是否存在error參數(shù)來(lái)控制信息顯示
到此這篇關(guān)于springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springsecurity自定義登錄頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解析SpringSecurity自定義登錄驗(yàn)證成功與失敗的結(jié)果處理問題
- SpringSecurity動(dòng)態(tài)加載用戶角色權(quán)限實(shí)現(xiàn)登錄及鑒權(quán)功能
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
- SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn)
- SpringSecurity 默認(rèn)表單登錄頁(yè)展示流程源碼
- springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- SpringSecurity多表多端賬戶登錄的實(shí)現(xiàn)
- SpringBoot如何整合Springsecurity實(shí)現(xiàn)數(shù)據(jù)庫(kù)登錄及權(quán)限控制
- SpringSecurity6.x多種登錄方式配置小結(jié)
- SpringSecurity表單配置之登錄成功及頁(yè)面跳轉(zhuǎn)原理解析
相關(guān)文章
Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能,涉及java日期時(shí)間及JFrame框架圖形界面操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02
Java實(shí)現(xiàn)批量發(fā)送帶附件的郵件代碼
大家好,本篇文章主要講的是Java實(shí)現(xiàn)批量發(fā)送帶附件的郵件代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
logback整合rabbitmq實(shí)現(xiàn)消息記錄日志的配置
這篇文章主要介紹了logback整合rabbitmq實(shí)現(xiàn)消息記錄日志的配置,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
maven+阿里云創(chuàng)建國(guó)內(nèi)鏡像的中央倉(cāng)庫(kù)(親測(cè)可用)
本篇文章主要介紹了maven+阿里云創(chuàng)建國(guó)內(nèi)鏡像的中央倉(cāng)庫(kù)(親測(cè)可用),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Spring?Boot集成Redisson實(shí)現(xiàn)延遲隊(duì)列
本文詳細(xì)介紹了電商支付場(chǎng)景中利用Redisson的RDelayedQueue實(shí)現(xiàn)訂單的延遲關(guān)閉功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(條件隊(duì)列)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Spring攔截器HandlerInterceptor接口代碼解析
這篇文章主要介紹了Spring攔截器HandlerInterceptor接口代碼解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
淺談hibernate急迫加載問題(多重外鍵關(guān)聯(lián))
這篇文章主要介紹了淺談hibernate急迫加載問題(多重外鍵關(guān)聯(lián)),具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12

