我们正在使用Spring Restful,oAuth,MVC服务器端,如果服务器和Web应用程序在同一台服务器上运行,它的工作正常。
我们在不同的域中运行服务器和Web应用程序,我们面临无效的会话错误。
当我们使帖子调用浏览器发出OPTIONS请求并且立即呼叫失败时。
我们已启用CORS过滤器,以便OPTIONS调用返回200,但它使当前会话无效,因此下一个调用失败。
我们没有发送OPTION请求,所以直到拦截器仍然无法进行无效会话。
我们尝试过以下解决方案:
我试图避免在OPTIONS请求上创建新会话,请让我们如何避免在Spring或Tomcat中。
我们还需要为混合移动应用程序提供此解决方案(因为它不支持cookie)。
在web.xml中:
<filter>
<filter-name>CORSFilter</filter-name>
<filter-class>com.tip.uiux.service.framework.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在CORSFilter.java中
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("X-Frame-Options", "DENY");
response.setHeader("Access-Control-Allow-Methods","POST, GET, HEAD, OPTIONS");
response.setHeader("Access-Control-Allow-Headers",
"Custom-Access-Token, Origin, Accept, Session-Alias, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
//LOGGER.info("Request Filtered for Cross Origin Resource Sharing using CORSFilter with custom headers");
chain.doFilter(req, res);
}
答案 0 :(得分:1)
您还应该在响应中添加相关标头。请在您的应用程序中使用以下CORS过滤器。
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
@Autowired
Environment environment;
public CorsFilter() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
if (Arrays.stream(this.environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("development")))) {
response.setHeader("Access-Control-Allow-Origin", "http://dev.yourdomain.com");
} else if (Arrays.stream(this.environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("production")))) {
response.setHeader("Access-Control-Allow-Origin", "https://www.yourdomain.com");
} else {
response.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
}
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, Authorization, X-Requested-With, Content-Type, Accept, Key");
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
请注意,过滤器还会向标头添加一些域信息(对于开发和生产环境,默认为localhost)。如果您想使用这些部分,可以为这些部分提供自己的域名。
希望这会有所帮助