最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例

 更新時(shí)間:2025年09月23日 09:40:38   作者:shuair  
本文主要介紹了springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)證

  1. 依賴spring-boot-starter-security后,服務(wù)啟動(dòng)時(shí),控制臺(tái)日志會(huì)輸出自動(dòng)生成的密碼

    1. 日志信息如下

      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.
      
    2. 默認(rèn)的用戶名為:user

  2. 依賴spring-boot-starter-security后,調(diào)用接口時(shí)就會(huì)進(jìn)行登錄認(rèn)證,會(huì)跳轉(zhuǎn)到默認(rèn)的登錄頁(yè)面:/login,該頁(yè)面是框架自帶的

    1. 頁(yè)面加載較慢,樣式加載不成功,但不影響功能使用。原因:會(huì)加載一些無(wú)法訪問的css數(shù)據(jù)
    2. 輸入默認(rèn)的用戶名和密碼,進(jìn)行登錄認(rèn)證,登錄認(rèn)證成功即可訪問接口數(shù)據(jù)
    3. 該頁(yè)面可自定義
  3. 默認(rèn)的登出頁(yè)面:/logout,該頁(yè)面是框架自帶的

    1. 頁(yè)面加載較慢,樣式加載不成功
    2. 該頁(yè)面有一個(gè)確認(rèn)登出按鈕,點(diǎn)擊后即可退出登錄,調(diào)用接口時(shí)會(huì)重新進(jìn)行登錄認(rèn)證
    3. 該頁(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ù)myusernamemypassword要與配置保持一致
  • 默認(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

廉江市| 平利县| 通城县| 平塘县| 四子王旗| 江西省| 江津市| 长子县| 富川| 杨浦区| 石狮市| 拜泉县| 怀远县| 遵化市| 秭归县| 金华市| 农安县| 玉屏| 香港 | 柳林县| 汉中市| 盐边县| 高邮市| 香格里拉县| 宣汉县| 临江市| 广德县| 湘潭县| 嵩明县| 务川| 鞍山市| 古蔺县| 焉耆| 紫阳县| 综艺| 婺源县| 曲松县| 鄂温| 南宁市| 商丘市| 鲁甸县|