NutzCN Logo
问答 跨域在过滤器里设置了,但是根本没进过滤器
发布于 1966天前 作者 qq_297125b4 2137 次浏览 复制 上一个帖子 下一个帖子
标签:

请求没进过滤器。用postman是可以的,也看到设置的返回头了。但是用js请求就不行

@IocBean
public class MyCrossOriginFilter implements ActionFilter {
	
	private static final Log log = Logs.get();
	protected String origin;
	protected String methods;
	protected String headers;
	protected String credentials;
	
	public MyCrossOriginFilter() {
		this("*", "GET, POST, PUT, DELETE, OPTIONS, PATCH", "Origin, Content-Type, Accept, Authorization, X-Requested-With, deviceType, deviceCode, deviceVersion, deviceInfo, platformVersion, platformType, token", "true");
	}
	
	public MyCrossOriginFilter(String origin, String methods, String headers, String credentials) {
		this.origin = origin;
		this.methods = methods;
		this.headers = headers;
		this.credentials = credentials;
	}
	
	public View match(ActionContext ac) {
		log.info("1111111==================22222222222");
		HttpServletResponse resp = ac.getResponse();
		if (!Strings.isBlank(this.origin)) {
			resp.setHeader("Access-Control-Allow-Origin", this.origin);
		}
		
		if (!Strings.isBlank(this.methods)) {
			resp.setHeader("Access-Control-Allow-Methods", this.methods);
		}
		
		if (!Strings.isBlank(this.headers)) {
			resp.setHeader("Access-Control-Allow-Headers", this.headers);
//			resp.setHeader("Access-Control-Expose-Headers", this.headers);
		}
		
		if (!Strings.isBlank(this.credentials)) {
			resp.setHeader("Access-Control-Allow-Credentials", this.credentials);
		}
		
		if ("OPTIONS".equals(ac.getRequest().getMethod())) {
			if (log.isDebugEnabled()) {
				log.debugf("Feedback -- [%s] [%s] [%s] [%s]", new Object[]{this.origin, this.methods, this.headers, this.credentials});
			}

			return new VoidView();
		}

		return null;
	}
}
1 回复

是不是用了@Post

添加回复
请先登陆
回到顶部