Spring Security中的表單登錄

前言

本文將介紹,在spring security中最基本的form表單登錄,以及一些相關的配置。

自定義表單登錄

如果我們不進行配置的話,spring security默認的登錄頁和登錄接口都是/login,只不過一個是get請求一個是post請求而已。

get http://localhost:8080/login.html 訪問頁面
post http://localhost:8080/login.html 提交form數據

我們也可以把它們單獨定義出來如下圖中:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //這個是用來忽略一些url地址,對其不進行校驗,通常用在一些靜態文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")//登錄頁面地址(如果不指定loginProcessingUrl,它就代表登錄頁面地址和登錄接口地址都是/login.html)
                .loginProcessingUrl("/loginTest")//用於指定登錄接口地址(對應的頁面form表單中的action對應的值也要修改)
                .permitAll()//相關接口不要被攔截
                .and()//相當於一個標籤結束符號,表示上一段配置,之後開啓新的配置
                .csrf().disable(); //關閉csrf
    }
}

對應的頁面form表單中的action對應的值也要修改

 <form action="/loginTest.html" method="post">

參數配置

如果沒有對登錄參數進行配置的話,默認的情況下form表單中username和password這兩個不能變

		<form action="/loginTest" method="post">
                <label for="name">用戶名</label>
                <input type="text" name="username" id="name">
                <label for="pass">密碼</label>
                <input type="password" name="password" id="pass">
        </form>

如果想要變更的話我們也許需要在配置文件中進行如下配置:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定義用戶名屬性值
                .passwordParameter("mima")//自定義密碼的屬性是
                .permitAll()//相關接口不要被攔截
                .and()//相當於一個標籤結束符號,表示上一段配置,之後開啓新的配置
                .csrf().disable(); //關閉csrf
    }

前端頁面對應的也相應的修改即可

  <form action="/loginTest" method="post">
                <label for="name">用戶名</label>
                <input type="text" name="yonghuming" id="name">
                <label for="pass">密碼</label>
                <input type="password" name="mima" id="pass">
        </form>

登錄後回調方法

成功回調
成功回調的方式有兩種:

defaultSuccessUrl
successForwardUrl

上代碼:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定義用戶名屬性值
                .passwordParameter("mima")//自定義密碼的屬性是
                .successForwardUrl("/index")//設置成功登錄跳轉的頁面,和下面的defaultSuccessUrl作用是相同的,兩則在使用的時候選擇其中一個就可以了
                .defaultSuccessUrl("/index",true)
                .permitAll()//相關接口不要被攔截
                .and()//相當於一個標籤結束符號,表示上一段配置,之後開啓新的配置
                .csrf().disable(); //關閉csrf
    }

失敗回調

failureForwardUrl
failureUrl

上代碼:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定義用戶名屬性值
                .passwordParameter("mima")//自定義密碼的屬性是
                .successForwardUrl("/index")//設置成功登錄跳轉的頁面,和下面的defaultSuccessUrl作用是相同的,兩則在使用的時候選擇其中一個就可以了
                //.defaultSuccessUrl("/index",true)
                //.failureUrl("loginfail")//登錄失敗跳轉頁面(重定向)同樣和下面的兩則選擇一個使用即可
                .failureForwardUrl("loginfail")//登錄失敗跳轉頁面(服務端跳轉)
                .permitAll()//相關接口不要被攔截
                .and()//相當於一個標籤結束符號,表示上一段配置,之後開啓新的配置
                .csrf().disable(); //關閉csrf
    }

登出

logout
logoutRequestMatcher
logoutSuccessUrl
deleteCookies

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定義用戶名屬性值
                .passwordParameter("mima")//自定義密碼的屬性是
                .successForwardUrl("/index")//設置成功登錄跳轉的頁面,和下面的defaultSuccessUrl作用是相同的,兩則在使用的時候選擇其中一個就可以了
                //.defaultSuccessUrl("/index",true)
                //.failureUrl("loginfail")//登錄失敗跳轉頁面(重定向)同樣和下面的兩則選擇一個使用即可
                .failureForwardUrl("/loginfail")//登錄失敗跳轉頁面(服務端跳轉)
                .and()
                //.logout() //默認的註銷url是/logout
                //.logoutUrl("/exit") //可以指定登出的url
                .logoutRequestMatcher(new AntPathRequestMatcher("/exist","post"))  //不僅可以指定登出url還可以指定請求方式
                .logoutSuccessUrl("/index")//登出成功後跳轉的頁面
                .deleteCookies() //清楚cookie
                .permitAll()//相關接口不要被攔截
                .and()//相當於一個標籤結束符號,表示上一段配置,之後開啓新的配置
                .csrf().disable(); //關閉csrf
    }

總結

本文介紹了簡單的表單登錄,以及登錄成功後的一些回調方法的使用和配置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章