1 回答
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();
}
添加回答
举报