溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring security中怎么自定義登錄頁面

發(fā)布時間:2021-06-18 15:22:18 來源:億速云 閱讀:280 作者:Leah 欄目:大數(shù)據(jù)

這篇文章將為大家詳細講解有關Spring security中怎么自定義登錄頁面,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

實現(xiàn)步驟

1. 復制上一示例的源碼

重命名包名 case2 為 case3

重命名 Case2Application.java 為 Case3Application.java

2. 在 WebSecurityConfig 中配置登錄頁

在 config(HttpSecurity http) 方法中對 formLogin 選項進行配置。需要包含以下設置:

  • 放行自定義登錄頁 url,例如: /login.html;

  • 設置登錄跳轉(zhuǎn)頁 url, 例如: /login.html;

  • 禁用 csrf 保護。如不添加此項,則需要每次從 cookie 中獲取 csrftoken 的值并隨表單一同提交到服務端。

完整代碼如下:

package net.txt100.learn.springsecurity.base.case3.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Title: WebSecurityConfig
 * Package: net.txt100.learn.springsecurity.base.case3.config
 * Creation date: 2019-08-11
 * Description:
 *
 * @author <a href="zgjt_tongl@thunis.com">Tonglei</a>
 * @since 1.0
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        // 配置密碼的保護策略,spring security 默認使用 bcrypt 加密算法。
        // 此處只要顯式聲明 BCryptPasswordEncoder Bean 即可
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        UsernamePasswordAuthenticationFilter up;

        http
            .csrf().disable() // 關閉 CSRF 保護功能,否則不支持 Post 請求
            .authorizeRequests() // 針對 HttpServletRequest 進行安全配置
                .antMatchers("/login.html").permitAll() // login.html 頁面無需登錄即可訪問
                .anyRequest().authenticated() // 對所有 Request 均需安全認證
            .and().formLogin()
                .loginPage("/login.html") // 每當需要登錄時瀏覽器跳轉(zhuǎn)到 login.html 頁面
                .loginProcessingUrl("/login") // 自定義登錄提交地址,默認地址是 /login, 默認處理器是 UsernamePasswordAuthenticationFilter
//                 .usernameParameter("/phone_number") // 自定義登錄用戶ID參數(shù),默認是 username
//                 .passwordParameter("/check_code") // 自定義登錄口令參數(shù),默認是 password
            .and().httpBasic(); // 定義如何驗證用戶,此項代表彈出瀏覽器認證窗口
    }
}

3. 創(chuàng)建 login.html 頁面

新建目錄 src/main/webapp,并在該目錄下創(chuàng)建文件 login.html。需要至少包含:

  • form 標簽,并且 action 賦值為 WebSecurityConfig 中 loginProcessingUrl 配置地址 (默認: "/login");

  • 用戶名參數(shù),與 WebSecurityConfig 中 usernameParameter 配置一致(默認:"username");

  • 密碼參數(shù),與 WebSecurityConfig 中 passwordParameter 配置一致(默認:"password")。

關于Spring security中怎么自定義登錄頁面就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI