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

我试图在 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();

    }

}

@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.sendError(401, exception.getClass().getName());

    }

}

当我尝试登录时,它第一次进入自定义入口点,并出现良好的异常(BadCredentialsException),但接下来我认为 Spring 会尝试请求/error,然后它会返回到入口点InsufficientAuthenticationException(来自ExceptionTranslationFilter.sendStartAuthentication)。


在响应中,我有一个 401,没有任何正文,甚至没有入口点中定义的异常类名。


知道配置有什么问题吗?


喵喵时光机
浏览 107回答 1
1回答

慕运维8079593

你是对的,spring尝试重定向到“/error”,因为你在CustomAuthEntryPoint中调用了response.sendError并尝试再次进行身份验证,因此你得到另一个InsufficentAuthentication异常。解决此问题的一种方法是使用response.setStatus(401)而不是response.sendError方法。就像这样:@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.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();    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java