@Component public class AuthorizationInterceptor implements HandlerInterceptor { public static final String LOGIN_TOKEN_KEY = "Token"; @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //支持跨域请求 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; }解析代码
时间: 2023-06-26 20:07:17 浏览: 87
Intellij IDEA如何去掉@Autowired 注入警告的方法
这是一个基于Spring框架的拦截器,用于拦截HTTP请求并进行身份验证。具体来说,在每个请求到达处理程序之前,该拦截器会检查请求头中是否包含一个名为"Token"的字段。如果包含该字段,则将其传递给TokenService进行身份验证。如果验证成功,则该请求将被允许进入处理程序;否则,该请求将被拒绝。
此外,该拦截器还处理了跨域请求的相关设置,允许指定的HTTP头字段,以及支持从处理程序中排除身份验证的注释。
阅读全文