We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
全局配置允许跨域主要的是需要正确处理[OPTIONS]。 在写完OptionsInterceptor之后,竟然发现根本进不了这个Interceptor。 完成看了源码之后才发现,系统会先行查找是否有对应的方法。如果没有,直接就会报错。根本不会掉拦截器。 所以在方法上还需要明确支持OPTION。 上代码:
方法写法,这里不可以写PostMapping,必须用RequestMapping,并明确支持OPTIONS。 @RequestMapping(method={RequestMethod.OPTIONS,RequestMethod.POST}, path = "/login") public UserResponse login(@RequestBody UserRequest userRequestModel) {
@RequestMapping(method={RequestMethod.OPTIONS,RequestMethod.POST}, path = "/login") public UserResponse login(@RequestBody UserRequest userRequestModel) {
此时拦截器才会生效。 @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); allow(response,HttpMethod.POST,HttpMethod.PUT, HttpMethod.OPTIONS); if (method == OPTIONS) { Logger.w("OptionsInterceptor onIntercept: allow "); return true; } return false; } }
@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); allow(response,HttpMethod.POST,HttpMethod.PUT, HttpMethod.OPTIONS); if (method == OPTIONS) { Logger.w("OptionsInterceptor onIntercept: allow "); return true; } return false; } }
The text was updated successfully, but these errors were encountered:
正在开发更简单的方式,后续可以通过注解来解决这个问题,请在这里跟进:
#305
Sorry, something went wrong.
No branches or pull requests
全局配置允许跨域主要的是需要正确处理[OPTIONS]。
在写完OptionsInterceptor之后,竟然发现根本进不了这个Interceptor。
完成看了源码之后才发现,系统会先行查找是否有对应的方法。如果没有,直接就会报错。根本不会掉拦截器。
所以在方法上还需要明确支持OPTION。
上代码:
方法写法,这里不可以写PostMapping,必须用RequestMapping,并明确支持OPTIONS。
@RequestMapping(method={RequestMethod.OPTIONS,RequestMethod.POST}, path = "/login") public UserResponse login(@RequestBody UserRequest userRequestModel) {
此时拦截器才会生效。
@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); allow(response,HttpMethod.POST,HttpMethod.PUT, HttpMethod.OPTIONS); if (method == OPTIONS) { Logger.w("OptionsInterceptor onIntercept: allow "); return true; } return false; } }
The text was updated successfully, but these errors were encountered: