Skip to content

Commit c9944cd

Browse files
committed
支持跨域
1 parent 4e406e3 commit c9944cd

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

admin/src/main/java/info/xiaomo/admin/AdminMain.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package info.xiaomo.admin;
22

3+
import info.xiaomo.core.filter.CORSFilter;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
56
import org.springframework.boot.orm.jpa.EntityScan;
7+
import org.springframework.context.annotation.Bean;
68
import org.springframework.context.annotation.ComponentScan;
79
import org.springframework.context.annotation.Configuration;
810
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@@ -37,6 +39,11 @@ public static void main(String[] args) throws Exception {
3739
SpringApplication.run(AdminMain.class, args);
3840
}
3941

42+
@Bean
43+
public CORSFilter corsFilter() {
44+
return new CORSFilter();
45+
}
46+
4047
@RequestMapping("/")
4148
String index() {
4249
return "Hello World! this is admin index";

admin/src/main/java/info/xiaomo/admin/controller/BlogController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import info.xiaomo.core.controller.BaseController;
44
import info.xiaomo.core.model.BlogModel;
55
import info.xiaomo.core.service.BlogService;
6-
import info.xiaomo.core.untils.MarkDownUtil;
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.data.domain.Page;
98
import org.springframework.data.domain.PageRequest;
@@ -83,7 +82,7 @@ public HashMap<String, Object> findByTitle(@RequestParam String title) {
8382
* @return
8483
*/
8584
@RequestMapping("findAll")
86-
public HashMap<String, Object> findAll(@RequestParam(value = "start",defaultValue = "1") int start, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
85+
public HashMap<String, Object> findAll(@RequestParam(value = "start", defaultValue = "1") int start, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
8786
Page<BlogModel> all = service.findAll(new PageRequest(start - 1, pageSize));
8887
result.put(code, success);
8988
result.put(blogs, all);
@@ -121,7 +120,7 @@ public HashMap<String, Object> add(
121120
blogModel.setStatus(0);
122121
blogModel.setBlogType(blogType);
123122
blogModel.setUpdateTime(new Date());
124-
blogModel.setTagId(tagIds);
123+
blogModel.setTagIds(tagIds);
125124
blogModel = service.addBlog(blogModel);
126125
result.put(code, success);
127126
result.put(blog, blogModel);
@@ -158,7 +157,7 @@ public HashMap<String, Object> update(
158157
blogModel.setContent(content);
159158
blogModel.setAuthor(nickName);
160159
blogModel.setSummary(summary);
161-
blogModel.setTagId(tagIds);
160+
blogModel.setTagIds(tagIds);
162161
blogModel.setBlogType(blogType);
163162
blogModel = service.updateBlog(blogModel);
164163
result.put(code, success);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package info.xiaomo.core.filter;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import javax.servlet.*;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.IOException;
8+
9+
/**
10+
* 把今天最好的表现当作明天最新的起点..~
11+
* いま 最高の表現 として 明日最新の始発..~
12+
* Today the best performance as tomorrow newest starter!
13+
* Created by IntelliJ IDEA.
14+
*
15+
* @author: xiaomo
16+
* @github: https://github.com/qq83387856
17+
* @email: hupengbest@163.com
18+
* @QQ_NO: 83387856
19+
* @Date: 2016/4/1516:25
20+
* @Description:
21+
* @Copyright(©) 2015 by xiaomo.
22+
**/
23+
@Component
24+
public class CORSFilter implements Filter {
25+
@Override
26+
public void init(FilterConfig filterConfig) throws ServletException {
27+
28+
}
29+
30+
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
31+
HttpServletResponse response = (HttpServletResponse) res;
32+
response.setHeader("Access-Control-Allow-Origin", "*");
33+
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
34+
response.setHeader("Access-Control-Max-Age", "3600");
35+
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
36+
chain.doFilter(req, res);
37+
}
38+
39+
public void destroy() {
40+
}
41+
}

web/src/main/java/info/xiaomo/web/WebMain.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package info.xiaomo.web;
22

3+
import info.xiaomo.core.filter.CORSFilter;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
56
import org.springframework.boot.orm.jpa.EntityScan;
7+
import org.springframework.context.annotation.Bean;
68
import org.springframework.context.annotation.ComponentScan;
79
import org.springframework.context.annotation.Configuration;
810
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@@ -34,4 +36,9 @@ public static void main(String[] args) throws Exception {
3436
SpringApplication.run(WebMain.class, args);
3537
}
3638

39+
@Bean
40+
public CORSFilter corsFilter() {
41+
return new CORSFilter();
42+
}
43+
3744
}

0 commit comments

Comments
 (0)