JSON格式为
{
"error": "invalid_token",
"error_description": "Invalid access token: 123123123"
}
源码org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter
拦截器中doFilter方法拦截到请求,如果token验证错误,会执行catch方法:
catch (OAuth2Exception failed) {
SecurityContextHolder.clearContext();
if (debug) {
logger.debug("Authentication request failed: " + failed);
}
eventPublisher.publishAuthenticationFailure(new BadCredentialsException(failed.getMessage(), failed),
new PreAuthenticatedAuthenticationToken("access-token", "N/A"));
authenticationEntryPoint.commence(request, response,
new InsufficientAuthenticationException(failed.getMessage(), failed));
return;
}
其中语句
authenticationEntryPoint.commence(request, response,
new InsufficientAuthenticationException(failed.getMessage(), failed));
是接收token验证错误,但是实例authenticationEntryPoint
是在类中new出来的
private AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
想要重写OAuth2AuthenticationEntryPoint
类该怎么实现呢?
在配置文件中改bean的注入类不起作用。
请问下后面问题解决了吗?我也遇到了一样的问题
重写response
private void reWriteResponse(HttpServletResponse response,String json) throws IOException{
response.setContentType("application/json;charset=utf-8");
PrintWriter writer;
writer = response.getWriter();
writer.write(json);
}
刚开始考虑重写过滤器来解决,发现没办法在重写没有用。你这后面的解决方法是什么
重写拦截器preHandle
方法