websocket cors and gradle
Posted 白衣胜雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websocket cors and gradle相关的知识,希望对你有一定的参考价值。
gradle need to use bootRepackage which will package all the dependencies, not to use jar task which will only package your main class code
websocket use two levels of cors.
1. http level:
package org.sang;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
@CrossOrigin
@Component
public class SimpleCORSFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
System.out.println("hello");
HttpServletRequest reqs = (HttpServletRequest)req;
response.setHeader("Access-Control-Allow-Origin", reqs.getHeader("Origin"));
System.out.println(reqs.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
response.setHeader("Access-Control-Expose-Headers", "Location");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
}
2 stomplevel:
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/endpointSang").setAllowedOrigins("*").withSockJS();
}
以上是关于websocket cors and gradle的主要内容,如果未能解决你的问题,请参考以下文章
Spring Security 和 Websocket 的 CORS 问题