一体化平台对接服务调整

This commit is contained in:
xlj
2026-03-03 09:33:55 +08:00
parent 39ee3a62f0
commit 704681ef2f
8 changed files with 123 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package com.tobacco.mp.config;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -15,8 +14,7 @@ import java.io.IOException;
public class CorsFilter implements Filter { public class CorsFilter implements Filter {
@Override @Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res; HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req; HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin"); String origin = request.getHeader("Origin");

View File

@ -28,22 +28,23 @@ public class RedisConfig extends CachingConfigurerSupport {
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂 // 配置连接工厂
template.setConnectionFactory(factory); template.setConnectionFactory(factory);
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值默认使用JDK的序列化方式
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值默认使用JDK的序列化方式
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper(); ObjectMapper om = new ObjectMapper();
// 指定要序列化的域field,get和set,以及修饰符范围ANY是都有包括private和public // 指定要序列化的域field,get和set,以及修饰符范围
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型类必须是非final修饰的final修饰的类比如String,Integer等会跑出异常 // 指定序列化输入的类型类必须是非final修饰的final修饰的类
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om); jacksonSeial.setObjectMapper(om);
// 值采用json序列化 // 值采用json序列化
template.setValueSerializer(jacksonSeial); template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值 // 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式 // 设置hash key 和value序列化模式

View File

@ -1,7 +1,11 @@
package com.tobacco.mp.config; 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 org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -13,7 +17,42 @@ public class RequestInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 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; 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;
}
} }

View File

@ -12,7 +12,7 @@ public class ServerRunner implements CommandLineRunner {
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
// 服务器成功启动加载其他 // 服务器成功启动加载其他
} }

View File

@ -1,9 +1,11 @@
package com.tobacco.mp.config; package com.tobacco.mp.config;
import com.tobacco.mp.utils.AnnotationUtil;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 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 @Configuration
public class WebMvcConfig implements WebMvcConfigurer { public class WebMvcConfig implements WebMvcConfigurer {
/** @Resource
* 声明需要放行的swagger资源路径 AnnotationUtil annotationUtil;
*/
private String[] newPassPath = new String[]{"/login", "/swagger-ui.html/**", "/v2/**","/files/**", "/webjars/**", "/swagger-resources/**",
"/**.js", "/static/**", "/images/**", "/index.html", "/css/**", "/js/**", "/file/**"};
/** /**
* 添加资源站点 * 添加资源站点
@ -24,7 +22,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
*/ */
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 指定资源映射
} }
/** /**
@ -33,8 +31,10 @@ public class WebMvcConfig implements WebMvcConfigurer {
*/ */
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
// 加载自定义注解
annotationUtil.loadCustomAnnotation();
// 注册拦截器 // 注册拦截器
registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns(newPassPath); registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns(AnnotationUtil.PASS_API);
} }
} }

View File

@ -1,6 +1,7 @@
package com.tobacco.mp.controller; package com.tobacco.mp.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tobacco.mp.annotation.PassOperation;
import com.tobacco.mp.dto.PlatformMessageDTO; import com.tobacco.mp.dto.PlatformMessageDTO;
import com.tobacco.mp.dto.PlatformNoticeDTO; import com.tobacco.mp.dto.PlatformNoticeDTO;
import com.tobacco.mp.dto.PlatformTaskDTO; import com.tobacco.mp.dto.PlatformTaskDTO;
@ -28,6 +29,7 @@ public class PlatformController {
PlatformMessageService messageService; PlatformMessageService messageService;
// 查询一体化平台的用户列表 // 查询一体化平台的用户列表
@PassOperation
@GetMapping(value = "/getUserList") @GetMapping(value = "/getUserList")
public ResponseResult<Void> getUserList() { public ResponseResult<Void> getUserList() {
userService.getUserList(); userService.getUserList();
@ -35,18 +37,21 @@ public class PlatformController {
} }
// 查询一体化平台的用户ID // 查询一体化平台的用户ID
@PassOperation
@GetMapping(value = "/getUserIdByAccount") @GetMapping(value = "/getUserIdByAccount")
public ResponseResult<String> getUserIdByAccount(@RequestParam String loginName) { public ResponseResult<String> getUserIdByAccount(@RequestParam String loginName) {
return ResponseResult.success(userService.getUserIdByAccount(loginName)); return ResponseResult.success(userService.getUserIdByAccount(loginName));
} }
// 一体化平台身份令牌认证 // 一体化平台身份令牌认证
@PassOperation
@GetMapping(value = "/tokenAccess") @GetMapping(value = "/tokenAccess")
public ResponseResult<JSONObject> tokenAccess(@RequestParam String token) { public ResponseResult<JSONObject> tokenAccess(@RequestParam String token) {
return ResponseResult.success(userService.tokenAccess(token)); return ResponseResult.success(userService.tokenAccess(token));
} }
// 向一体化平台发送消息 // 向一体化平台发送消息
@PassOperation
@PostMapping(value = "/sendMessage") @PostMapping(value = "/sendMessage")
public ResponseResult<Void> sendMessage(@RequestBody PlatformMessageDTO dataDTO) { public ResponseResult<Void> sendMessage(@RequestBody PlatformMessageDTO dataDTO) {
messageService.sendMessage(dataDTO); messageService.sendMessage(dataDTO);
@ -54,6 +59,7 @@ public class PlatformController {
} }
// 向一体化平台发送通知/公告 // 向一体化平台发送通知/公告
@PassOperation
@PostMapping(value = "/sendNotice") @PostMapping(value = "/sendNotice")
public ResponseResult<Void> sendNotice(@RequestBody PlatformNoticeDTO dataDTO) { public ResponseResult<Void> sendNotice(@RequestBody PlatformNoticeDTO dataDTO) {
messageService.sendNotice(dataDTO); messageService.sendNotice(dataDTO);
@ -61,6 +67,7 @@ public class PlatformController {
} }
// 向一体化平台发送待办 // 向一体化平台发送待办
@PassOperation
@PostMapping(value = "/sendToDo") @PostMapping(value = "/sendToDo")
public ResponseResult<Void> sendToDo(@RequestBody PlatformTaskDTO taskDTO) { public ResponseResult<Void> sendToDo(@RequestBody PlatformTaskDTO taskDTO) {
messageService.sendToDo(taskDTO); messageService.sendToDo(taskDTO);
@ -68,6 +75,7 @@ public class PlatformController {
} }
// 向一体化平台发送待办处置信息 // 向一体化平台发送待办处置信息
@PassOperation
@PostMapping(value = "/sendHandleToDo") @PostMapping(value = "/sendHandleToDo")
public ResponseResult<Void> sendHandleToDo(@RequestBody PlatformTaskHandleDTO handleDTO) { public ResponseResult<Void> sendHandleToDo(@RequestBody PlatformTaskHandleDTO handleDTO) {
messageService.sendHandleToDo(handleDTO); messageService.sendHandleToDo(handleDTO);

View File

@ -32,6 +32,7 @@ public class AnnotationUtil {
/** /**
* 加载自定义注解 * 加载自定义注解
* 用以加载相关注解的接口
*/ */
public void loadCustomAnnotation() { public void loadCustomAnnotation() {
Map<String, List<String>> mapApis = getAccessAnnotations(RESOURCE_PATTERN); Map<String, List<String>> mapApis = getAccessAnnotations(RESOURCE_PATTERN);

View File

@ -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);
}
}