我试图在 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,没有任何正文,甚至没有入口点中定义的异常类名。
知道配置有什么问题吗?
慕运维8079593
相关分类