一体化平台对接服务调整
This commit is contained in:
@ -2,7 +2,6 @@ package com.tobacco.mp.config;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -15,8 +14,7 @@ import java.io.IOException;
|
||||
public class CorsFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletResponse response = (HttpServletResponse) res;
|
||||
HttpServletRequest request = (HttpServletRequest) req;
|
||||
String origin = request.getHeader("Origin");
|
||||
|
||||
@ -28,22 +28,23 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
|
||||
// 配置连接工厂
|
||||
template.setConnectionFactory(factory);
|
||||
|
||||
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
|
||||
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
|
||||
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
|
||||
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
|
||||
// 指定要序列化的域,field,get和set,以及修饰符范围
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
|
||||
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
|
||||
jacksonSeial.setObjectMapper(om);
|
||||
|
||||
// 值采用json序列化
|
||||
template.setValueSerializer(jacksonSeial);
|
||||
//使用StringRedisSerializer来序列化和反序列化redis的key值
|
||||
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
|
||||
// 设置hash key 和value序列化模式
|
||||
|
||||
@ -1,7 +1,11 @@
|
||||
package com.tobacco.mp.config;
|
||||
|
||||
import com.tobacco.mp.constant.Constant;
|
||||
import com.tobacco.mp.constant.Symbol;
|
||||
import com.tobacco.mp.utils.RedisUtils;
|
||||
import com.tobacco.mp.utils.SpringBeanUtils;
|
||||
import com.tobacco.mp.utils.StringUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@ -13,7 +17,42 @@ public class RequestInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// 请求拦截处理
|
||||
String api = request.getServletPath();
|
||||
// 开放接口的秘钥验证(当检测到请求头携带指定字段意味着该接口为开放接口需验证)
|
||||
String secretKey = request.getHeader("SecretKey");
|
||||
if (StringUtils.isNotEmpty(secretKey)) {
|
||||
return verifyOpenApi(secretKey, api);
|
||||
}
|
||||
// 处理其他非开放接口的校验,判断是否拦截
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证开放的接口
|
||||
* @param secretKey 秘钥
|
||||
* @param api 接口
|
||||
* @return 是否允许请求
|
||||
*/
|
||||
public boolean verifyOpenApi(String secretKey, String api) {
|
||||
// 验证请求的合法性
|
||||
String verifyCode = "This is a secretKey";
|
||||
if (!verifyCode.equals(secretKey)) {
|
||||
return false;
|
||||
}
|
||||
// 验证接口是否是开放接口
|
||||
RedisUtils redisUtils = SpringBeanUtils.getBean(RedisUtils.class);
|
||||
Object o = redisUtils.get(Constant.OPEN_API);
|
||||
if (o != null) {
|
||||
String apiStr = String.valueOf(o);
|
||||
String[] array = apiStr.split(Symbol.COMMA);
|
||||
for (String s : array) {
|
||||
if (s.equals(api)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ public class ServerRunner implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// 服务器成功启动加载其他
|
||||
// 服务器成功启动后加载其他
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
package com.tobacco.mp.config;
|
||||
|
||||
import com.tobacco.mp.utils.AnnotationUtil;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 资源管理配置
|
||||
@ -11,12 +13,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
/**
|
||||
* 声明需要放行的swagger资源路径
|
||||
*/
|
||||
private String[] newPassPath = new String[]{"/login", "/swagger-ui.html/**", "/v2/**","/files/**", "/webjars/**", "/swagger-resources/**",
|
||||
"/**.js", "/static/**", "/images/**", "/index.html", "/css/**", "/js/**", "/file/**"};
|
||||
|
||||
@Resource
|
||||
AnnotationUtil annotationUtil;
|
||||
|
||||
/**
|
||||
* 添加资源站点
|
||||
@ -24,7 +22,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
|
||||
// 指定资源映射
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,8 +31,10 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 加载自定义注解
|
||||
annotationUtil.loadCustomAnnotation();
|
||||
// 注册拦截器
|
||||
registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns(newPassPath);
|
||||
registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns(AnnotationUtil.PASS_API);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user