spring boot做前后分离的跨域问题

最近在用spring boot 做数据接口供客户端调用时发现存在跨域问题,虽然解决问题有很多。比如用nginx处理等等。但是更方便的是服务端设置一下跨域名。

一、前提

1. 使用spring boot + maven 构建的项目

二、设置

1. 写一个过滤器(filter)

实现Filter接口,然后设置一下响应头。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package info.xiaomo.core.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 把今天最好的表现当作明天最新的起点..~
 * いま 最高の表現 として 明日最新の始発..~
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * @author: xiaomo
 * @github: https://github.com/houko
 * @email: hupengbest@163.com
 * @QQ_NO: 83387856
 * @Date: 2016/4/1516:25
 * @Description:
 * @Copyright(©) 2015 by xiaomo.
 **/
@Component
public class CORSFilter implements Filter {


    /**
     * 初始化
     *
     * @param filterConfig filterConfig
     * @throws ServletException ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * CORS 过滤器
     *
     * @param req   rq
     * @param res   res
     * @param chain chin
     * @throws IOException      IOException
     * @throws ServletException ServletException
     */
    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-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, If-Modified-Since");
        chain.doFilter(req, res);
    }


    /**
     * 销毁对象
     */
    public void destroy() {
    }
}

2. 在启动器中加入过滤器

实质就是加了一个@bean,然后在启动的时候通过过滤器把所有的响应头设置一下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package info.xiaomo.website;

import info.xiaomo.core.filter.CORSFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 把今天最好的表现当作明天最新的起点..~
 * いま 最高の表現 として 明日最新の始発..~
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * @author: xiaomo
 * @github: https://github.com/houko
 * @email: hupengbest@163.com
 * @QQ_NO: 83387856
 * @Date: 2016/4/1 15:38
 * @Description: 后台管理启动器
 * @Copyright(©) 2015 by xiaomo.
 **/
@Configuration
@EnableAutoConfiguration
@ComponentScan("info.xiaomo")
@EntityScan("info.xiaomo.*.model")
@EnableTransactionManagement
@EnableJpaRepositories("info.xiaomo.*.dao")
@RestController
public class XiaomoMain {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(XiaomoMain.class, args);
    }

    @Bean
    public CORSFilter corsFilter() {
        return new CORSFilter();
    }

    @RequestMapping("/")
    String index() {
        return "Hello World! this is website index";
    }
}

这样一来,客户端就不会存在跨域问题了。

署名 - 非商业性使用 - 禁止演绎 4.0