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();
}
添加回答
舉報