首页 新闻 会员 周边

oauth2 重写token验证错误返回数据模型

0
悬赏园豆:10 [已关闭问题] 关闭于 2018-01-15 10:17

1. oauth2原始错误返回类型

JSON格式为
{
"error": "invalid_token",
"error_description": "Invalid access token: 123123123"
}

2. 想要在返回的数据类型中加上一个字段

3.源码中

源码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的注入类不起作用。

pkyou的主页 pkyou | 初学一级 | 园豆:98
提问于:2018-01-10 09:34
< >
分享
所有回答(2)
0

请问下后面问题解决了吗?我也遇到了一样的问题

心若向阳&何畏悲伤 | 园豆:202 (菜鸟二级) | 2018-05-18 16:27

重写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);
    }
支持(1) 反对(0) pkyou | 园豆:98 (初学一级) | 2018-06-01 15:58
0

刚开始考虑重写过滤器来解决,发现没办法在重写没有用。你这后面的解决方法是什么

N丶周建 | 园豆:204 (菜鸟二级) | 2018-12-27 16:38

重写拦截器preHandle方法

支持(4) 反对(0) pkyou | 园豆:98 (初学一级) | 2018-12-27 16:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册