WebSecurityConfig.java 3.9 KB
Newer Older
yuquan.zhu committed
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
package cn.timer.api.config.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
public class WebSecurityConfig implements WebMvcConfigurer {
	
	@Resource
	private UserMethodArgumentResolver userMethodArgumentResolver;
	
	@Bean
	public RedisSessionInterceptor getSessionInterceptor() {
		return new RedisSessionInterceptor();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 所有已api开头的访问都要进入RedisSessionInterceptor拦截器进行登录验证,并排除login接口(全路径)。必须写成链式,分别设置的话会创建多个拦截器。
		// 必须写成getSessionInterceptor(),否则SessionInterceptor中的@Autowired会无效
		//.excludePathPatterns("/")
		registry.addInterceptor(getSessionInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns("/doc*")
39 40
				.excludePathPatterns("/v2/**")
				.excludePathPatterns("/**/*.js")
yuquan.zhu committed
41
				.excludePathPatterns("/8timer/**")
yuquan.zhu committed
42
				.excludePathPatterns("/kqz/punchclock/**")
yuquan.zhu committed
43
				.excludePathPatterns("/kqz/sauserregdata/**")
yuquan.zhu committed
44
				.excludePathPatterns("/login/*")
yuquan.zhu committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58
				.excludePathPatterns("/swagger*/**")
				.excludePathPatterns("/v2/api-docs")
				.excludePathPatterns("/druid/login*")
				.excludePathPatterns("/webjars/**");
		// registry.addInterceptor(getSessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/swagger-ui*");
	}
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	    // 1.需要先定义一个convert 转换消息的对象
	    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
	    // 2.添加fastJson的配置信息,比如,是否需要格式化返回的json数据
	    FastJsonConfig fastJsonConfig = new FastJsonConfig();
	    // 时间格式化
yuquan.zhu committed
59
	    // fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
yuquan.zhu committed
60 61 62 63 64 65
	    // fastJsonConfig.setDateFormat("yyyy-MM-dd");
	    // 空值特别处理
	    // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
	    // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
	    // WriteNullNumberAsZero 将数值类型字段的空值输出为0
	    // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
yuquan.zhu committed
66 67
	    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty,
	        SerializerFeature.WriteNullStringAsEmpty);
yuquan.zhu committed
68 69 70 71 72 73 74
	    // 处理中文乱码问题
	    List<MediaType> fastMediaTypes = new ArrayList<>();
	    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	    fastConverter.setSupportedMediaTypes(fastMediaTypes);
	    // 3.在convert中添加配置信息
	    fastConverter.setFastJsonConfig(fastJsonConfig);
	    // 4.将convert添加到converters当中
yuquan.zhu committed
75
	    converters.add(fastConverter);
yuquan.zhu committed
76 77 78 79 80 81 82 83

	}
	
	@Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(userMethodArgumentResolver);
    }
	
yuquan.zhu committed
84 85 86 87 88 89 90
//	@Override  
//    public void addCorsMappings(CorsRegistry registry) {  
//        registry.addMapping("/**")  
//                .allowedOrigins("*")  
//                .allowCredentials(true)  
//                .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")  
//                .maxAge(3600);  
yuquan.zhu committed
91
//    }
yuquan.zhu committed
92
	
yuquan.zhu committed
93
}