SpringBoot解决前端的跨域问题

  1.我的项目是以jar包的形式部署在阿里云服务器上的,首先你得配置安全组规则开放指定端口,默认是8080,也可以通过yml或者properties文件自己指定,然后配置解决跨域的过滤器:

@Component
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;

        HttpServletRequest reqs = (HttpServletRequest) req;

        // response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
        
    }

    @Override
    public void destroy() {
    }
}



 

2.接着maven clean  maven package通过xftp把jar包放到服务器的合适目录下

3.然后java -jar xxx.jar 启动 如果想一直启动则用nohup java -jar xxx.jar回车-->exit回车即可。

   注意:如果想关闭一直启动的jar包,则要杀死进程,删除并不管用,

   1)根据端口查看这个进程的pid:netstat -lnp|grep 8080  #8080请换为你的apache需要的端口

   2)查看进程的详细信息:ps pid

   3)  杀掉进程:kill -9 pid  (-9表示强迫进程立即停止)

   

你可能感兴趣的:(bug心得)