1 package cn.home1.oss.lib.webmvc.starter;
2
3 import static org.apache.commons.lang3.StringUtils.isNotBlank;
4
5 import cn.home1.oss.boot.autoconfigure.AppProperties;
6 import cn.home1.oss.lib.webmvc.api.DomainResolver;
7 import cn.home1.oss.lib.webmvc.api.RequestResolver;
8 import cn.home1.oss.lib.webmvc.internal.DefaultDomainResolver;
9 import cn.home1.oss.lib.webmvc.internal.DefaultHttpEntityMethodProcessor;
10 import cn.home1.oss.lib.webmvc.internal.DefaultRequestResolver;
11
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
14 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
15 import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
16 import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
17 import org.springframework.boot.autoconfigure.web.ServerProperties;
18 import org.springframework.boot.autoconfigure.web.ServerProperties.Session;
19 import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
20 import org.springframework.boot.context.properties.EnableConfigurationProperties;
21 import org.springframework.context.annotation.Bean;
22 import org.springframework.context.annotation.ComponentScan;
23 import org.springframework.context.annotation.Configuration;
24 import org.springframework.context.annotation.Import;
25 import org.springframework.core.env.Environment;
26 import org.springframework.web.servlet.DispatcherServlet;
27 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
28
29 import javax.servlet.Servlet;
30
31
32
33
34
35 @ConditionalOnClass({Servlet.class, DispatcherServlet.class})
36 @ConditionalOnWebApplication
37 @AutoConfigureBefore({ErrorMvcAutoConfiguration.class, WebMvcAutoConfiguration.class})
38 @Configuration
39 @ComponentScan(basePackages = {"cn.home1.oss.lib.webmvc.starter"})
40 @EnableConfigurationProperties(value = {AppProperties.class})
41 @Import({GsonConfiguration.class, Jackson2Configuration.class})
42 public class WebApplicationAutoConfiguration extends WebMvcConfigurerAdapter {
43
44 @Autowired
45 private ServerProperties serverProperties;
46
47 @Autowired
48 private AppProperties appProperties;
49
50 @Autowired(required = false)
51 private Environment environment;
52
53 @Autowired(required = false)
54 private com.fasterxml.jackson.databind.ObjectMapper objectMapper;
55
56 @Bean
57 public DomainResolver domainResolver() {
58 final Session.Cookie sessionCookie = this.serverProperties.getSession().getCookie();
59 return new DefaultDomainResolver(
60 sessionCookie.getDomain(),
61 this.appProperties.getProdEnvironment()
62 );
63 }
64
65
66
67 @Bean
68 public RequestResolver requestResolver() {
69 return new DefaultRequestResolver(this.appProperties.getSecurity().getLoginEndpoint());
70 }
71
72 @Bean
73 public DefaultHttpEntityMethodProcessor defaultHttpEntityMethodProcessor() {
74 return DefaultHttpEntityMethodProcessor.defaultHttpEntityMethodProcessor(this.environment, this.objectMapper);
75 }
76
77
78
79
80
81
82
83
84
85 @Deprecated
86 static String envDomain(
87 final String env,
88 final String rootDomain,
89 final String productionSubDomain
90 ) {
91 final String domain;
92 if (AppProperties.getProdEnvironment(env)) {
93 domain = isNotBlank(productionSubDomain) ?
94 productionSubDomain + "." + rootDomain :
95 rootDomain;
96 } else {
97 domain = env + "." + rootDomain;
98 }
99 return domain;
100 }
101 }