随着春天引导 1.5.6.RELEASE我能够发送HTTP状态代码401,而不是403在描述如果请求的uri没有认证如何让春天的安全响应未经授权(HTTP 401码),这样做:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//...
http.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("myHeader"));
//...
}
}
Run Code Online (Sandbox Code Playgroud)
使用org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint班级.
我刚刚升级到Spring Boot 2.0.0.RELEASE,发现不再有这样的类了(至少在那个包中).
问:Http401AuthenticationEntryPoint Spring Boot中
是否存在此类()?如果不是,那么在现有项目中保持相同行为以保持与依赖于此状态代码(401而不是403?)的其他实现保持一致的可能是一个很好的选择.
java spring spring-security http-status-code-401 spring-boot
我正在尝试在 Spring Boot 2 中的 WebSecurityConfig 配置方法中实现自定义 AuthenticationEntryPoint。
我见过很多这样的例子:
@Component
public class Http401UnauthorizedEntryPoint implements AuthenticationEntryPoint {
private final Logger log = LoggerFactory.getLogger(Http401UnauthorizedEntryPoint.class);
/**
* Always returns a 401 error code to the client.
*/
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException arg2) throws IOException,
ServletException {
log.debug("Pre-authenticated entry point called. Rejecting access");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied");
}
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到 403 而不是 401。
我怎样才能在 Spring boot 2 中做到这一点?