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")); // 跨域时会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态 if (request.getMethod().equals(RequestMethod.OPTIONS.name())) { response.setStatus(HttpStatus.OK.value()); return false; } IgnoreAuth annotation; if (handler instanceof HandlerMethod) { annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class); } else { return true; } //从header中获取token String token = request.getHeader(LOGIN_TOKEN_KEY);
时间: 2023-06-15 10:06:38 浏览: 128
request_key_auth.rar__auth.requestToken
这段代码是一个 Java Spring 框架中的拦截器,用于处理在请求中传递的 Token 信息。它首先设置了跨域请求的相关响应头信息,然后判断请求是否为 OPTIONS 请求,如果是则返回正常状态。接着,它通过 @IgnoreAuth 注解判断该请求是否需要进行身份验证,如果不需要,则直接返回 true。最后,它从请求头中获取 Token 信息。这段代码的作用是确保请求合法且有权限访问对应的资源。
阅读全文