Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor 不起作用 - 跨域 #315

Closed
xuncai-tech opened this issue Oct 15, 2020 · 2 comments
Closed

Interceptor 不起作用 - 跨域 #315

xuncai-tech opened this issue Oct 15, 2020 · 2 comments

Comments

@xuncai-tech
Copy link

已下是写了一个全局的跨域拦截器。但是onIntercept方法不会被调用。
不太清楚是什么原因。

@interceptor
public class OptionsInterceptor implements HandlerInterceptor {
public static void allow(HttpResponse response, HttpMethod... methods) {
String methodHeader = TextUtils.join(",", methods);
response.setHeader("Access-Control-Allow-Origin", "");
response.setHeader("Access-Control-Allow-Methods", methodHeader);
response.setHeader("Access-Control-Allow-Headers", "
");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Allow", methodHeader);
}
/**
* Intercept the execution of a handler.
*
* @param request current request.
* @param response current response.
* @param handler the corresponding handler of the current request.
* @return true if the interceptor has processed the request and responded.
*/
@OverRide
public boolean onIntercept(@nonnull HttpRequest request, @nonnull HttpResponse response, @nonnull RequestHandler handler) throws Exception {

    HttpMethod method = request.getMethod();
    Logger.w("OptionsInterceptor onIntercept: "+method);
    if (method == OPTIONS) {
        allow(response,HttpMethod.PUT, HttpMethod.OPTIONS);
        Logger.w("OptionsInterceptor onIntercept: allow ");
        return true;
    }
    return false;
}

}

@yanzhenjie
Copy link
Owner

#305

@xuncai-tech
Copy link
Author

这个问题主要是因为 系统会先发现OPTIONS不被支持,直接就会报错。此时系统还未调用拦截器,所有拦截器无法生效。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants