码迷,mamicode.com
首页 > Web开发 > 详细

websocket cors and gradle

时间:2017-11-01 18:59:08      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:ini   tom   headers   ebs   levels   res   websocket   rri   .com   

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

标签:ini   tom   headers   ebs   levels   res   websocket   rri   .com   

原文地址:http://www.cnblogs.com/goldengallo/p/7767801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!