1

@Component
public class BootOAuth2AuthExceptionEntryPoint extends OAuth2AuthenticationEntryPoint {

private Logger logger = LoggerFactory.getLogger(BootOAuth2AuthExceptionEntryPoint.class);

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
    logger.debug("BootOAuth2AuthExceptionEntryPoint -> {}",e);
    if (e.getCause() instanceof InvalidTokenException) {
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JsonUtil.toJsonString(Result.createWithErrorMessage("token无效", Constants.INVALID_TOKEN)));
    }
}

}
在 OAuth2ResourceConfig配置

@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources.resourceId("resourceId"); //重点,设置资源id
    resources.authenticationEntryPoint(bootOAuth2AuthExceptionEntryPoint).accessDeniedHandler(loginOAuth2AccessDeniedHandler);
}

exception
69 声望1 粉丝