spring security 自定义登录页面(loginPage和loginProcessingUrl) + 关闭csrf token

相关文章:
Spring Security默认登录页面原理
spring security 自定义登录页面

1. 自定义登录页面

核心是通过配置,覆盖原有默认的登陆页面:

//不写这个注解配置不生效
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.formLogin()
            .loginPage("/demo-login.html")
            .loginProcessingUrl("/demo-login");
    }
}

注意:loginPage和loginProcessingUrl参数必须同时成对出现,否则会有问题

这时,需要你新增一个demo-login.html页面

此外,为了避免重定向死循环现象:
在这里插入图片描述
这时候发现浏览器提示了重定向的次数过多错误,这是为啥呢?我们来分析一下我们在浏览器地址栏里面输入我们接口地址按下回车之后都发生了啥。

访问/hello接口,因为我们刚才配置了所有请求都需要身份认证,现在我们没有登录那自然就要跳转到登录页登录,跳转/demo-login.html。可是/demo-login.html也需要身份认证啊,那就再次跳转/demo-login.html,这样重复往返就死循环下去了。

问题讲清楚之后,那解决方案就很明确了,把/demo-login.html和/demo-login(form表单action属性的值)配置为不需要认证,放行。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.formLogin()
            .loginPage("/demo-login.html")
            .loginProcessingUrl("/demo-login")
            .and()
            .authorizeRequests()
            .antMatchers("/demo-login.html", "/demo-login").permitAll() //对于登陆页面,要让所有人免密登陆,否则,会进不去登陆页面,产生重定向死循环
            .anyRequest().authenticated();
    }
}

2. 关闭csrf token

默认情况下开启csrf token,会导致我们上面运行仍然失败:

我们发现报错了,显示无效的csrf token。这是因为spring security默认开启了csrf防护,只要在请求中带上csrf token就可以了。这个token在返回登录页面的时候spring security已经帮我们准备好了,只是我们没有使用模板引擎,所以取到token比较困难,这里我们把csrf关闭吧,大家可以自己摸索一下如何取到这个token,带上就完事了。重写一下配置。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.formLogin()
            .loginPage("/demo-login.html")
            .loginProcessingUrl("/demo-login")
            .and()
            .authorizeRequests()
            .antMatchers("/demo-login.html", "/demo-login").permitAll()
            .anyRequest().authenticated()
            .and()
            .csrf().disable(); //先关闭,将来在说
    }
}

参考

spring security 入门教程 自定义登录页面

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Spring Security自定义短信验证码登录,需要遵循以下步骤: 1. 配置短信验证码的过滤器 在Spring Security的配置类中,添加一个短信验证码过滤器,用于拦截短信验证码登录请求,并校验验证码是否正确。可以参考以下代码: ``` @Bean public SmsCodeFilter smsCodeFilter() throws Exception { SmsCodeFilter smsCodeFilter = new SmsCodeFilter(); smsCodeFilter.setAuthenticationManager(authenticationManagerBean()); smsCodeFilter.setAuthenticationFailureHandler(authenticationFailureHandler()); return smsCodeFilter; } ``` 2. 实现短信验证码的校验逻辑 创建一个实现了`AuthenticationProvider`接口的短信验证码认证提供者,并在其中实现短信验证码的校验逻辑。可以参考以下代码: ``` @Component public class SmsCodeAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication; String mobile = (String) authenticationToken.getPrincipal(); String smsCode = (String) authenticationToken.getCredentials(); // 校验短信验证码 if (smsCodeIsValid(mobile, smsCode)) { // 构造认证通过的令牌 SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(mobile); authenticationResult.setDetails(authenticationToken.getDetails()); return authenticationResult; } else { throw new BadCredentialsException("短信验证码不正确"); } } private boolean smsCodeIsValid(String mobile, String smsCode) { // 根据手机号和短信验证码进行校验 // ... return true; } @Override public boolean supports(Class<?> authentication) { return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication); } } ``` 3. 配置AuthenticationManager 在Spring Security的配置类中,配置`AuthenticationManager`,并将自定义的短信验证码认证提供者加入到其中。可以参考以下代码: ``` @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(smsCodeAuthenticationProvider()); } ``` 4. 配置登录接口 在Spring Security的配置类中,配置短信验证码登录的登录接口。可以参考以下代码: ``` @Override protected void configure(HttpSecurity http) throws Exception { http.addFilterBefore(smsCodeFilter(), UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/sms-login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .and() .csrf().disable(); } ``` 5. 发送短信验证码 在登录接口中,添加发送短信验证码的逻辑。可以参考以下代码: ``` @PostMapping("/sms-code") @ResponseBody public String sendSmsCode(@RequestParam String mobile) { // 发送短信验证码 // ... return "success"; } ``` 以上就是Spring Security自定义短信验证码登录的实现步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值