一体化平台对接服务调整
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,17 +28,18 @@ 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 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序列化
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.tobacco.mp.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.tobacco.mp.annotation.PassOperation;
|
||||
import com.tobacco.mp.dto.PlatformMessageDTO;
|
||||
import com.tobacco.mp.dto.PlatformNoticeDTO;
|
||||
import com.tobacco.mp.dto.PlatformTaskDTO;
|
||||
@ -28,6 +29,7 @@ public class PlatformController {
|
||||
PlatformMessageService messageService;
|
||||
|
||||
// 查询一体化平台的用户列表
|
||||
@PassOperation
|
||||
@GetMapping(value = "/getUserList")
|
||||
public ResponseResult<Void> getUserList() {
|
||||
userService.getUserList();
|
||||
@ -35,18 +37,21 @@ public class PlatformController {
|
||||
}
|
||||
|
||||
// 查询一体化平台的用户ID
|
||||
@PassOperation
|
||||
@GetMapping(value = "/getUserIdByAccount")
|
||||
public ResponseResult<String> getUserIdByAccount(@RequestParam String loginName) {
|
||||
return ResponseResult.success(userService.getUserIdByAccount(loginName));
|
||||
}
|
||||
|
||||
// 一体化平台身份令牌认证
|
||||
@PassOperation
|
||||
@GetMapping(value = "/tokenAccess")
|
||||
public ResponseResult<JSONObject> tokenAccess(@RequestParam String token) {
|
||||
return ResponseResult.success(userService.tokenAccess(token));
|
||||
}
|
||||
|
||||
// 向一体化平台发送消息
|
||||
@PassOperation
|
||||
@PostMapping(value = "/sendMessage")
|
||||
public ResponseResult<Void> sendMessage(@RequestBody PlatformMessageDTO dataDTO) {
|
||||
messageService.sendMessage(dataDTO);
|
||||
@ -54,6 +59,7 @@ public class PlatformController {
|
||||
}
|
||||
|
||||
// 向一体化平台发送通知/公告
|
||||
@PassOperation
|
||||
@PostMapping(value = "/sendNotice")
|
||||
public ResponseResult<Void> sendNotice(@RequestBody PlatformNoticeDTO dataDTO) {
|
||||
messageService.sendNotice(dataDTO);
|
||||
@ -61,6 +67,7 @@ public class PlatformController {
|
||||
}
|
||||
|
||||
// 向一体化平台发送待办
|
||||
@PassOperation
|
||||
@PostMapping(value = "/sendToDo")
|
||||
public ResponseResult<Void> sendToDo(@RequestBody PlatformTaskDTO taskDTO) {
|
||||
messageService.sendToDo(taskDTO);
|
||||
@ -68,6 +75,7 @@ public class PlatformController {
|
||||
}
|
||||
|
||||
// 向一体化平台发送待办处置信息
|
||||
@PassOperation
|
||||
@PostMapping(value = "/sendHandleToDo")
|
||||
public ResponseResult<Void> sendHandleToDo(@RequestBody PlatformTaskHandleDTO handleDTO) {
|
||||
messageService.sendHandleToDo(handleDTO);
|
||||
|
||||
@ -32,6 +32,7 @@ public class AnnotationUtil {
|
||||
|
||||
/**
|
||||
* 加载自定义注解
|
||||
* 用以加载相关注解的接口
|
||||
*/
|
||||
public void loadCustomAnnotation() {
|
||||
Map<String, List<String>> mapApis = getAccessAnnotations(RESOURCE_PATTERN);
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
package com.tobacco.mp.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SpringBeanUtils implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if(SpringBeanUtils.applicationContext == null) {
|
||||
SpringBeanUtils.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取applicationContext
|
||||
* @return
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name获取 Bean.
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static Object getBean(String name){
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean.
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz){
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name,以及Clazz返回指定的Bean
|
||||
* @param name
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(String name,Class<T> clazz){
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user