public class MyHandlerTokenException implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
if(authException instanceof InsufficientAuthenticationException) {
Map<String, Object> map = new HashMap<String, Object>();
Throwable cause = authException.getCause();
map.put("errorCode", ""401);//401
map.put("data", "");
if(cause != null){
map.put("code", "invalid_token");
}else{
map.put("msg", authException.getMessage());
}
response.setContentType("application/json");
try {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getOutputStream(), map);
} catch (Exception e) {
throw new ServletException();
}
}
}
}
2
覆写ResourceServerConfigurerAdapter类中的方法
public void configure(ResourceServerSecurityConfigurer resources){
resources.authenticationEntryPoint(new MyHandlerTokenException?());
}
|