ThronesFrontConfig.java 10.4 KB
Newer Older
街头小贩's avatar
+  
街头小贩 已提交
1 2
package com.apobates.forum.thrones;

街头小贩's avatar
街头小贩 已提交
3
import com.apobates.forum.thrones.controller.helper.StrategyCondition;
4
import com.apobates.forum.attention.ImageStoreDomain;
5
import com.apobates.forum.core.ImageIOMeta;
街头小贩's avatar
+  
街头小贩 已提交
6 7
import com.apobates.forum.member.storage.OnlineMemberStorage;
import com.apobates.forum.member.storage.cookie.CookieMetaConfig;
街头小贩's avatar
街头小贩 已提交
8
import com.apobates.forum.thrones.controller.helper.AuthenticationInterceptor;
街头小贩's avatar
街头小贩 已提交
9
import com.apobates.forum.thrones.controller.helper.MemberInviteCodeInterceptorAdapter;
10
import com.apobates.forum.thrones.controller.helper.OnlineDescriptorAspect;
街头小贩's avatar
街头小贩 已提交
11
import com.apobates.forum.thrones.controller.helper.RegisteChannelInterceptor;
12
import com.apobates.forum.thrones.controller.helper.StrategyInterceptorAdapter;
街头小贩's avatar
+  
街头小贩 已提交
13 14
import com.apobates.forum.thrones.rss.TopicRssView;
import org.springframework.beans.factory.annotation.Value;
街头小贩's avatar
街头小贩 已提交
15
import org.springframework.context.MessageSource;
街头小贩's avatar
+  
街头小贩 已提交
16 17 18
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
街头小贩's avatar
街头小贩 已提交
19
import org.springframework.context.annotation.Conditional;
街头小贩's avatar
+  
街头小贩 已提交
20
import org.springframework.context.annotation.Configuration;
21
import org.springframework.context.annotation.EnableAspectJAutoProxy;
22
import org.springframework.context.annotation.Import;
街头小贩's avatar
+  
街头小贩 已提交
23 24
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
街头小贩's avatar
街头小贩 已提交
25
import org.springframework.context.support.ResourceBundleMessageSource;
街头小贩's avatar
+  
街头小贩 已提交
26
import org.springframework.http.MediaType;
街头小贩's avatar
街头小贩 已提交
27 28
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
街头小贩's avatar
+  
街头小贩 已提交
29
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
30
import org.springframework.web.servlet.config.annotation.CorsRegistry;
街头小贩's avatar
+  
街头小贩 已提交
31 32
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
街头小贩's avatar
街头小贩 已提交
33
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
街头小贩's avatar
+  
街头小贩 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * 前端Spring MVC配置类
 *
 * @author xiaofanku
 * @since 20200511
 */
@Configuration
@PropertySource(value = "classpath:global.properties", ignoreResourceNotFound = true, encoding = "UTF-8")
@EnableWebMvc
51
@EnableAspectJAutoProxy(proxyTargetClass=true)
街头小贩's avatar
+  
街头小贩 已提交
52
@ComponentScan(basePackages = {"com.apobates.forum.thrones.controller"}, useDefaultFilters = false, includeFilters = {@Filter(classes = {org.springframework.stereotype.Controller.class}),@Filter(classes = {org.springframework.stereotype.Component.class})})
街头小贩's avatar
街头小贩 已提交
53
@Import(value = {WebSocketConfig.class})
街头小贩's avatar
+  
街头小贩 已提交
54 55 56 57 58 59 60 61 62
public class ThronesFrontConfig implements WebMvcConfigurer {
    @Value("${site.cookieSymbol}")
    private String cookieName;
    @Value("${site.cookieDomain}")
    private String cookieDomain;
    @Value("${site.cookiePath}")
    private String cookiePath;
    @Value("${site.domain}")
    private String siteDomain;
63 64
    @Value("${img.bucket.domain}")
    private String imageBucketDomain;
65 66
    @Value("${img.bucket.upload.direct}")
    private String imageBucketUploadDirect;
67 68
    @Value("${site.origin.admin}")
    private String adminDomain;
街头小贩's avatar
街头小贩 已提交
69
    
70
    //授权访问的拦截器
街头小贩's avatar
街头小贩 已提交
71 72 73 74
    @Bean
    public AuthenticationInterceptor getAuthInter() {
        return new AuthenticationInterceptor();
    }
75
    //注册通道检查拦截器
街头小贩's avatar
街头小贩 已提交
76 77 78 79
    @Bean
    public RegisteChannelInterceptor getRegisInter() {
        return new RegisteChannelInterceptor();
    }
街头小贩's avatar
街头小贩 已提交
80
    //策略检测拦截器,以Env(site.strategy)来控制开启,默认开启
81
    @Bean
街头小贩's avatar
街头小贩 已提交
82
    @Conditional(StrategyCondition.class)
83 84 85
    public StrategyInterceptorAdapter getStrategyInter(){
        return new StrategyInterceptorAdapter();
    }
86
    //注册邀请码拦截器
街头小贩's avatar
街头小贩 已提交
87 88 89 90
    @Bean
    public MemberInviteCodeInterceptorAdapter getInviteCodeInter(){
        return new MemberInviteCodeInterceptorAdapter();
    }
91
    //会员在线记录 aspectJ 
92
    @Bean
93
    public OnlineDescriptorAspect getOnlineAspect() { //在线会员
94
        return new OnlineDescriptorAspect();
95
    }
街头小贩's avatar
+  
街头小贩 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    /**
     * 配置请求视图映射
     *
     * @return
     */
    @Bean
    public InternalResourceViewResolver resourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        //请求视图文件的前缀地址
        internalResourceViewResolver.setPrefix("/WEB-INF/layout/page/");
        //请求视图文件的后缀
        internalResourceViewResolver.setSuffix(".jsp");
        internalResourceViewResolver.setViewClass(JstlView.class);
        return internalResourceViewResolver;
    }
    //静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(31536000);
    }
    //定义无Controller的path<->view直接映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/home");
    }
    //RSS
    @Bean(name = "topicRssView")
    public TopicRssView getRssView() {
        return new com.apobates.forum.thrones.rss.TopicRssView();
    }
    //定义视图文件解析View resolvers
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        BeanNameViewResolver beanResolver = new BeanNameViewResolver();
        beanResolver.setOrder(1);
        registry.viewResolver(beanResolver);
        registry.viewResolver(resourceViewResolver());
街头小贩's avatar
街头小贩 已提交
133
        registry.enableContentNegotiation(getRssView());
街头小贩's avatar
+  
街头小贩 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }
    //对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    //注册媒体类型
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.ignoreAcceptHeader(false)
                .favorPathExtension(true)
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .mediaType("rss", MediaType.APPLICATION_RSS_XML);
    }
街头小贩's avatar
街头小贩 已提交
149 150 151 152
    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 授权访问的拦截器
街头小贩's avatar
街头小贩 已提交
153
        registry.addInterceptor(getAuthInter()).excludePathPatterns("/member/home/board/active/json", "/member/home/topic/publish/json", "/member/home/topic/reply/json").addPathPatterns("/member/home/**", "/message/**", "/topic/create", "/posts/create", "/posts/reply", "/search/");
街头小贩's avatar
街头小贩 已提交
154 155
        // 注册通道检查拦截器
        registry.addInterceptor(getRegisInter()).addPathPatterns("/member/register");
156
        // 策略检测拦截器
157
        registry.addInterceptor(getStrategyInter()).addPathPatterns("/board/**", "/posts/**", "/topic/**");
街头小贩's avatar
街头小贩 已提交
158 159
        // 邀请码拦截器
        registry.addInterceptor(getInviteCodeInter()).addPathPatterns("/member/register");
街头小贩's avatar
街头小贩 已提交
160
    }
街头小贩's avatar
街头小贩 已提交
161
    //HV && BV
街头小贩's avatar
街头小贩 已提交
162 163 164 165 166 167 168
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean lvfb = new org.springframework.validation.beanvalidation.LocalValidatorFactoryBean();
        lvfb.setProviderClass(org.hibernate.validator.HibernateValidator.class);
        lvfb.setValidationMessageSource(messageSource());
        return lvfb;
    }
169 170 171 172 173
    //To enable CORS for the whole application
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/member/home/topic/**").allowedMethods("GET").allowedOrigins(adminDomain);
    }
街头小贩's avatar
街头小贩 已提交
174 175 176 177 178 179 180 181 182
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(60);
        messageSource.setUseCodeAsDefaultMessage(false);
        return messageSource;
    }
街头小贩's avatar
+  
街头小贩 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourceConfigurer() {
        PropertySourcesPlaceholderConfigurer pp = new PropertySourcesPlaceholderConfigurer();
        pp.setIgnoreUnresolvablePlaceholders(true);
        return pp;
    }
    /*apache fileupload
    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        commonsMultipartResolver.setMaxUploadSize(maxBytes);
        commonsMultipartResolver.setResolveLazily(false);
        return commonsMultipartResolver;
    }*/
    //Cookie
    @Bean(name = "cookieConfig")
    public CookieMetaConfig getCookieConfig() {
        CookieMetaConfig cnf = new com.apobates.forum.member.storage.cookie.CookieMetaConfig(cookieName, cookieDomain, cookiePath, siteDomain);
        return cnf;
    }
    @Bean(name = "onlineMemberStorage")
    public OnlineMemberStorage getMemberStorage(CookieMetaConfig cookieConfig) {
        return new com.apobates.forum.member.storage.cookie.HttpCookieProvider(cookieConfig);
    }
街头小贩's avatar
街头小贩 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    @Bean(name="imageStoreDomain")
    public ImageStoreDomain getImageStore(){
        return new com.apobates.forum.attention.ImageStoreDomain(){
            @Override
            public String getDefaultDomain() {
                return imageBucketDomain;
            }
            
            @Override
            public String getLocalDomain() {
                return siteDomain;
            }
        };
    }
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    @Bean(name="imageIOMeta")
    public ImageIOMeta getImageIOMeta(){
        return new com.apobates.forum.core.ImageIOMeta(){
            @Override
            public String getImageBucketDomain() {
                return imageBucketDomain;
            }
            
            @Override
            public String getUploadImageDirectName() {
                return imageBucketUploadDirect;
            }
        };
    }
}