为了账号安全,请及时绑定邮箱和手机立即绑定

尝试使用 Spring Security 自定义失败登录

尝试使用 Spring Security 自定义失败登录

喵喵时光机 2023-08-09 17:13:08
我试图在 Spring Security 的失败登录响应中添加更多信息。这是我的配置:@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class SecurityConfig extends WebSecurityConfigurerAdapter {    private final CustomAuthenticationProvider authProvider;    private final CustomAuthEntryPoint customAuthEntryPoint;    public SecurityConfig(CustomAuthenticationProvider authProvider, CustomAuthEntryPoint customAuthEntryPoint) {        this.authProvider = authProvider;        this.customAuthEntryPoint = customAuthEntryPoint;    }    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        auth.authenticationProvider(authProvider);    }   @Override    protected void configure(HttpSecurity http) throws Exception {        http            .cors().and()            .logout().deleteCookies("SESSION").and()            .authorizeRequests()                .antMatchers("/actuator/**").permitAll()                .anyRequest().authenticated().and()                     .httpBasic().authenticationEntryPoint(customAuthEntryPoint).and()            .csrf().disable();    }}@Componentpublic class CustomAuthEntryPoint implements AuthenticationEntryPoint {    @Override    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {        System.out.println("entrypoint " + exception.getClass().getName());        response.getOutputStream().print(exception.getClass().getName());        response.sendError(401, exception.getClass().getName());    }}当我尝试登录时,它第一次进入自定义入口点,并出现良好的异常(BadCredentialsException),但接下来我认为 Spring 会尝试请求/error,然后它会返回到入口点InsufficientAuthenticationException(来自ExceptionTranslationFilter.sendStartAuthentication)。在响应中,我有一个 401,没有任何正文,甚至没有入口点中定义的异常类名。知道配置有什么问题吗?
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

你是对的,spring尝试重定向到“/error”,因为你在CustomAuthEntryPoint中调用了response.sendError并尝试再次进行身份验证,因此你得到另一个InsufficentAuthentication异常。解决此问题的一种方法是使用response.setStatus(401)而不是response.sendError方法。就像这样:


@Component

public class CustomAuthEntryPoint implements AuthenticationEntryPoint {

  @Override

  public void commence(HttpServletRequest request, HttpServletResponse response,

      AuthenticationException exception) throws IOException, ServletException {

    System.out.println("entrypoint " + exception.getClass().getName());

    response.getOutputStream().print(exception.getClass().getName());

    response.setStatus(401);

  }

}

或者,您可以从授权中排除“/error”url,但您必须小心不要泄漏该错误端点上的敏感信息:


@Override

    protected void configure(HttpSecurity http) throws Exception {

        http

            .cors().and()

            .logout().deleteCookies("SESSION").and()

            .authorizeRequests()

                .antMatchers("/actuator/**","/error").permitAll()

                .anyRequest().authenticated().and()         

            .httpBasic().authenticationEntryPoint(customAuthEntryPoint).and()

            .csrf().disable();

    }


查看完整回答
反对 回复 2023-08-09
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信