SpringSecurity自定義登錄頁面跳轉時訪問頁面302


 1 問題原因

登錄時報302

 

2 SecurityConfig 配置  關閉 csrf 跨站請求偽造 還是報錯

package com.mangoubiubiu.security.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;

/**
 * SecurityConfig 配置類
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
            //表單登錄
            http.formLogin()
                    .loginProcessingUrl("/login")
                    .loginPage("/login.html")
                    .successForwardUrl("/main");
            //所有請求都必須被認證(登錄)
            http.authorizeRequests()
                    //放行登錄頁面
                    .antMatchers("/login.html").permitAll()
                    //所有請求都必須被認證(登錄)
                    .anyRequest().authenticated();

            //關閉 csrf 跨站請求偽造
            http.csrf().disable();
    }

    @Bean
    public PasswordEncoder pw(){
        return new BCryptPasswordEncoder();
    }

}

以上代碼還是報錯

3 究極踩坑點

看看form表單 用戶名提交的key 

 

 

 SpringSecurity 默認的 key 是 username 和 password 可以在 UsernamePasswordAuthenticationFilter 這個類里面看

4 解決方案

只需要 將 form表單提交也 的 key 和 SpringSecurity 默認的key 保持一致即可

成功登錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM