@Component
public class BootOAuth2AuthExceptionEntryPoint extends OAuth2AuthenticationEntryPoint {private Logger logger = LoggerFactory.getLogger(BootOAuth2AuthExceptionEntryPoint.class);@Overridepublic 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配置@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("resourceId"); //重点,设置资源id resources.authenticationEntryPoint(bootOAuth2AuthExceptionEntryPoint).accessDeniedHandler(loginOAuth2AccessDeniedHandler);}