-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMain.java
More file actions
93 lines (82 loc) · 3.75 KB
/
Copy pathMain.java
File metadata and controls
93 lines (82 loc) · 3.75 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.javabaas.server;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.javabaas.server.common.interceptor.AdminInterceptor;
import com.javabaas.server.common.interceptor.AuthInterceptor;
import com.javabaas.server.common.interceptor.HeaderInterceptor;
import com.javabaas.server.common.interceptor.MasterInterceptor;
import com.javabaas.server.object.entity.BaasList;
import com.javabaas.server.object.entity.BaasObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import java.util.Map;
@SpringBootApplication
public class Main implements WebMvcConfigurer {
@Autowired
private HeaderInterceptor contentTypeInterceptor;
@Autowired
private AuthInterceptor authInterceptor;
@Autowired
private AdminInterceptor adminInterceptor;
@Autowired
private MasterInterceptor masterInterceptor;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//请求头拦截器,检查Content-Type、platform等
registry.addInterceptor(contentTypeInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/file/callback",
"/api/file/notify/**", "/customer/**");
//授权拦截器
registry.addInterceptor(authInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/file/callback",
"/api/file/notify/**", "/api/admin/**", "/customer/**");
//超级权限
registry.addInterceptor(adminInterceptor).addPathPatterns("/api/admin/**");
//管理权限
registry.addInterceptor(masterInterceptor).addPathPatterns("/api/master/**");
}
@Bean
public RestTemplate getRest() {
return new RestTemplate();
}
@Bean(name = "baasMapper")
public ObjectMapper baasMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
SimpleModule module = new SimpleModule();
module.addAbstractTypeMapping(Map.class, BaasObject.class);
module.addAbstractTypeMapping(List.class, BaasList.class);
return mapper.registerModule(module);
}
@Bean(name = "objectMapper")
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
/**
* 修改默认json适配器
*/
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
}