
jetbrick.web.mvc.router.SimpleCORSRequestProcessor Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
*
* Author: Guoqiang Chen
* Email: [email protected]
* WebURL: https://github.com/subchen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jetbrick.web.mvc.router;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jetbrick.web.mvc.CORSRequestProcessor;
/**
* 支持 CORS request 的简单实现.
*/
public final class SimpleCORSRequestProcessor implements CORSRequestProcessor {
private String allowOrigin = "*";
private String allowHeaders = null;
private String allowMethods = null;
private String maxAge = "3600";
private boolean allowCredentials = false;
public void setAllowOrigin(String allowOrigin) {
this.allowOrigin = allowOrigin;
}
public void setAllowMethods(String allowMethods) {
this.allowMethods = allowMethods;
}
public void setAllowHeaders(String allowHeaders) {
this.allowHeaders = allowHeaders;
}
public void setMaxAge(String maxAge) {
this.maxAge = maxAge;
}
public void setAllowCredentials(boolean allowCredentials) {
this.allowCredentials = allowCredentials;
}
@Override
public void setHeaders(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", allowOrigin);
if (allowMethods == null) {
allowMethods = request.getHeader("Access-Control-Request-Method");
}
response.setHeader("Access-Control-Allow-Methods", allowMethods);
if (allowHeaders == null) {
allowHeaders = request.getHeader("Access-Control-Request-Headers");
}
response.setHeader("Access-Control-Max-Age", maxAge);
response.setHeader("Access-Control-Allow-Headers", allowHeaders);
if (allowCredentials) {
response.setHeader("Access-Control-Allow-Credentials", "true");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy