From a2e5eac4dcdd530d48b0dee0ed6a131b4948b407 Mon Sep 17 00:00:00 2001 From: liu Date: Mon, 24 Jun 2024 09:02:27 +0800 Subject: [PATCH] init --- .../src/main/java/com/supervision/knowsub/1 | 0 .../knowsub/config/JwtInterceptor.java | 91 ++++++++++ .../knowsub/config/MybatisPlusConfig.java | 36 ++++ .../knowsub/config/RequestLogConfig.java | 159 ++++++++++++++++++ .../knowsub/config/ResponseConfig.java | 138 +++++++++++++++ .../knowsub/config/SwaggerConfiguration.java | 50 ++++++ .../supervision/knowsub/config/WebConfig.java | 71 ++++++++ .../knowsub/exception/BusinessException.java | 76 +++++++++ .../knowsub/util/SpringBeanUtil.java | 128 ++++++++++++++ .../supervision/knowsub/util/TokenUtil.java | 19 +++ 10 files changed, 768 insertions(+) delete mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/1 create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/JwtInterceptor.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/MybatisPlusConfig.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/RequestLogConfig.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/ResponseConfig.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/SwaggerConfiguration.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/config/WebConfig.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/exception/BusinessException.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/util/SpringBeanUtil.java create mode 100644 know_sub_common/src/main/java/com/supervision/knowsub/util/TokenUtil.java diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/1 b/know_sub_common/src/main/java/com/supervision/knowsub/1 deleted file mode 100644 index e69de29..0000000 diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/JwtInterceptor.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/JwtInterceptor.java new file mode 100644 index 0000000..f1d031c --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/JwtInterceptor.java @@ -0,0 +1,91 @@ +package com.supervision.knowsub.config; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import cn.hutool.jwt.JWT; +import cn.hutool.jwt.JWTUtil; +import com.supervision.exception.BusinessException; +import com.supervision.util.TokenUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.HttpStatus; +import org.springframework.lang.Nullable; +import org.springframework.web.servlet.HandlerInterceptor; + +import java.util.HashMap; +import java.util.Map; + +@Slf4j +public class JwtInterceptor implements HandlerInterceptor { + + public JwtInterceptor() { + } + + + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + //请求消息头获取用户ID + String token = request.getHeader("token"); + if (StrUtil.isBlank(token)) { + // 如果是swagger来的接口,说明这里是测试的,会伪造一个用户 + String referer = request.getHeader("Referer"); + if (StrUtil.isNotBlank(referer) && StrUtil.contains(referer, "doc.html")) { + cacheAuth(JWTUtil.parseToken(devActiveUser())); + return true; + } else { + throw new BusinessException("当前用户未登录", HttpStatus.UNAUTHORIZED.value()); + } + } + JWT jwt = JWTUtil.parseToken(token); + // 校验token是否过期,如果过期了,需要提示过期重新登录 + checkTokenExpire(jwt); + cacheAuth(jwt); + return true; + } + + + @Override + public void afterCompletion(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler, + @Nullable Exception ex) throws Exception { + // 请求结束,将用户信息从thread中移除 + clearAuth(); + HandlerInterceptor.super.afterCompletion(request, response, handler, ex); + } + + + private void checkTokenExpire(JWT jwt) { + Object expireTime = jwt.getPayload("expireTime"); + long l = Long.parseLong(String.valueOf(expireTime)); + // 校验是否比当前时间大 + long currentTimeMillis = System.currentTimeMillis(); + if (currentTimeMillis > l) { + throw new BusinessException("用户登录已过期,请重新登录", HttpStatus.UNAUTHORIZED.value()); + } + } + + + private void cacheAuth(JWT jwt) { + try { + JSONObject claimsJson = jwt.getPayload().getClaimsJson(); + ThreadCache.USER.set(claimsJson.toString()); + } catch (Exception e) { + log.error("用户信息异常", e); + } + } + + private String devActiveUser() { + Map map = new HashMap<>(); + map.put("id", "1"); + map.put("account", "test"); + map.put("name", "测试"); + return TokenUtil.creatToken(JSONUtil.toJsonStr(map)); + } + + private void clearAuth() { + ThreadCache.USER.remove(); + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/MybatisPlusConfig.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/MybatisPlusConfig.java new file mode 100644 index 0000000..34925a8 --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/MybatisPlusConfig.java @@ -0,0 +1,36 @@ +/* + * 文 件 名: MybatisPlusConfig + * 版 权: + * 描 述: <描述> + * 修 改 人: RedName + * 修改时间: 2022/8/9 + * 跟踪单号: <跟踪单号> + * 修改单号: <修改单号> + * 修改内容: <修改内容> + */ +package com.supervision.knowsub.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * <功能详细描述> + * + * @author RedName + * @version [版本号, 2022/8/9] + * @see [相关类/方法] + * @since [产品/模块版本] + */ +@Configuration +public class MybatisPlusConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); + return interceptor; + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/RequestLogConfig.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/RequestLogConfig.java new file mode 100644 index 0000000..a2f9cff --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/RequestLogConfig.java @@ -0,0 +1,159 @@ +package com.supervision.knowsub.config; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.stereotype.Component; +import org.springframework.util.ObjectUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.multipart.MultipartFile; + +import java.util.HashMap; +import java.util.Map; + +/** + * 日志切面 + * + * @author :liu + * create :2020-11-16 10:50 + **/ +@Component +@Aspect +@Slf4j +public class RequestLogConfig { + + + @SuppressWarnings("all") + @Around("within(com..*..controller..*) && @within(org.springframework.web.bind.annotation.RestController)") + public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + long start = System.currentTimeMillis(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + Object result = proceedingJoinPoint.proceed(); + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setUrl(request.getRequestURL().toString()); + requestInfo.setHttpMethod(request.getMethod()); + requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(), + proceedingJoinPoint.getSignature().getName())); + requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint)); + requestInfo.setResult(proceedResult(result)); + requestInfo.setTimeCost(System.currentTimeMillis() - start); + log.info("Request Info: {}", JSONUtil.toJsonStr(requestInfo)); + return result; + } + + private Object proceedResult(Object result) { + String value = null; + try { + value = JSONUtil.toJsonStr(result); + } catch (Exception e) { + value = result.toString(); + } + if (StrUtil.isNotBlank(value) && value.length() > 10240){ + value = value.substring(0, 1024) + "......" + value.substring(value.length() - 1024); + return value; + } + return result; + } + + + @SuppressWarnings("all") + @AfterThrowing(pointcut = "@within(org.springframework.web.bind.annotation.RestController)", throwing = "e") + public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + RequestErrorInfo requestErrorInfo = new RequestErrorInfo(); + requestErrorInfo.setUrl(request.getRequestURL().toString()); + requestErrorInfo.setHttpMethod(request.getMethod()); + requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(), + joinPoint.getSignature().getName())); + requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint)); + requestErrorInfo.setException(e); + log.info("Error Request Info : {}", JSONUtil.toJsonStr(requestErrorInfo)); + } + + /** + * 获取入参 + * + * @param proceedingJoinPoint + * @return + */ + private Map getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) { + //参数名 + String[] paramNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames(); + //参数值 + Object[] paramValues = proceedingJoinPoint.getArgs(); + + return buildRequestParam(paramNames, paramValues); + } + + private Map getRequestParamsByJoinPoint(JoinPoint joinPoint) { + //参数名 + String[] paramNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); + //参数值 + Object[] paramValues = joinPoint.getArgs(); + + return buildRequestParam(paramNames, paramValues); + } + + private Map buildRequestParam(String[] paramNames, Object[] paramValues) { + Map requestParams = new HashMap<>(); + if (paramNames == null){ + return requestParams; + } + for (int i = 0; i < paramNames.length; i++) { + Object value = paramValues[i]; + if (value instanceof ServletRequest || value instanceof ServletResponse) { + //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false) + //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response + continue; + } + + //如果是文件对象 + if (value instanceof MultipartFile) { + MultipartFile file = (MultipartFile) value; + //获取文件名 + value = file.getOriginalFilename(); + } else if (value instanceof String) { + // 如果参数很长,就对参数进行修剪 + String temp = (String) value; + if (!ObjectUtils.isEmpty(temp) && temp.length() > 1024) { + value = temp.substring(0, 10) + "......" + temp.substring(temp.length() - 10); + } + } + requestParams.put(paramNames[i], value); + } + + return requestParams; + } + + @Data + public static class RequestInfo { + private String url; + private String httpMethod; + private String classMethod; + private Object requestParams; + private Object result; + private Long timeCost; + } + + @Data + public static class RequestErrorInfo { + private String url; + private String httpMethod; + private String classMethod; + private Object requestParams; + private RuntimeException exception; + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/ResponseConfig.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/ResponseConfig.java new file mode 100644 index 0000000..67bb7c7 --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/ResponseConfig.java @@ -0,0 +1,138 @@ +package com.supervision.knowsub.config; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; +import com.supervision.domain.GlobalResult; +import com.supervision.exception.BusinessException; +import com.supervision.exception.HumanException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.lang.Nullable; +import org.springframework.validation.BindException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * 统一返回 + * + * @author wb + * @date 2022/3/10 13:24 + */ +@Slf4j +@RestControllerAdvice(annotations = RestController.class, basePackages = {"com.supervision.**.controller"}) +public class ResponseConfig implements ResponseBodyAdvice { + + + @Override + public boolean supports(@Nullable MethodParameter methodParameter, + @Nullable Class> aClass) { + assert methodParameter != null; + String lowerCase = methodParameter.getDeclaringClass().getName().toLowerCase(); + log.info(lowerCase); + if (lowerCase.contains("swagger") || lowerCase.contains("springdoc")) { + return false; + } else { + log.info("--------{}---------", lowerCase); + return true; + } + } + + @Override + @SuppressWarnings("all") + public Object beforeBodyWrite(Object o, @Nullable MethodParameter methodParameter, @Nullable MediaType mediaType, + @Nullable Class> aClass, @Nullable ServerHttpRequest serverHttpRequest, + @Nullable ServerHttpResponse serverHttpResponse) { + serverHttpResponse.getHeaders().set("Content-Type", "application/json"); + + if (Objects.isNull(o)) { + return GlobalResult.ok(null, "success"); + } + if (o instanceof GlobalResult) { + return o; + } + // 对于String类型的返回值需要进行特殊处理 + if (o instanceof String) { + return JSONUtil.toJsonStr(GlobalResult.ok(o, "success")); + } + return GlobalResult.ok(o, "success"); + } + + /** + * 业务异常处理 + * + * @param exception 业务异常 + * @return 通用返回值 + */ + @ExceptionHandler(BusinessException.class) + public GlobalResult businessExceptionResponse(BusinessException exception) { + log.error(exception.getMessage(), exception); + Integer code = null == exception.getCode() ? HttpStatus.INTERNAL_SERVER_ERROR.value(): exception.getCode(); + String message = StrUtil.isNotEmpty(exception.getMessage()) ? exception.getMessage() : "业务异常"; + return GlobalResult.error( code, exception.getMessage(), message); + } + + /** + * 数字人异常处理 + * + * @param exception 业务异常 + * @return 通用返回值 + */ + @ExceptionHandler(HumanException.class) + public GlobalResult humanExceptionResponse(HumanException exception) { + log.error(exception.getMessage(), exception); + return GlobalResult.error(exception.getCode(), exception.getMessage(), "数字人异常"); + } + + + /** + * 参数验证异常处理 + * + * @param exception 参数验证异常 + * @return 通用返回值 + */ + @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class}) + public GlobalResult validationExceptionResponse(MethodArgumentNotValidException exception) { + log.error(exception.getMessage(), exception); + // 格式化错误信息 + String errorMsg = exception.getBindingResult().getFieldErrors().stream() + .map(e -> e.getField() + ":" + e.getDefaultMessage()).collect(Collectors.joining("、")); + return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), errorMsg, "参数验证异常"); + } + + + /** + * 添加手动校验参数的异常处理 + * @param exception 参数验证异常 + * @return 通用返回值 + */ + @ExceptionHandler(IllegalArgumentException.class) + public GlobalResult manualValidationExceptionResponse(IllegalArgumentException exception) { + log.error(exception.getMessage(), exception); + return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage(), "参数验证异常"); + } + + /** + * 未知异常处理 + * + * @param exception 未知异常 + * @return 通用返回值 + */ + @ExceptionHandler(Exception.class) + public GlobalResult validationExceptionResponse(Exception exception) { + log.error(exception.getMessage(), exception); + return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知错误", "未知错误"); + } + + +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/SwaggerConfiguration.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/SwaggerConfiguration.java new file mode 100644 index 0000000..02bac0f --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/SwaggerConfiguration.java @@ -0,0 +1,50 @@ +package com.supervision.knowsub.config; + +import cn.hutool.core.util.RandomUtil; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import org.springdoc.core.customizers.GlobalOpenApiCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class SwaggerConfiguration { + + /** + * 根据@Tag 上的排序,写入x-order + * + * @return the global open api customizer + */ + @Bean + public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() { + return openApi -> { + if (openApi.getTags() != null) { + openApi.getTags().forEach(tag -> { + Map map = new HashMap<>(); + map.put("x-order", RandomUtil.randomInt(0, 100)); + tag.setExtensions(map); + }); + } + if (openApi.getPaths() != null) { + openApi.addExtension("x-test123", "333"); + openApi.getPaths().addExtension("x-abb", RandomUtil.randomInt(1, 100)); + } + }; + } + + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .info(new Info() + .title("虚拟病人系统API") + .version("1.3") + .description("Knife4j集成springdoc-openapi示例") + .termsOfService("http://doc.xiaominfo.com") + .license(new License().name("Apache 2.0") + .url("http://doc.xiaominfo.com"))); + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/config/WebConfig.java b/know_sub_common/src/main/java/com/supervision/knowsub/config/WebConfig.java new file mode 100644 index 0000000..2bed14c --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/config/WebConfig.java @@ -0,0 +1,71 @@ +package com.supervision.knowsub.config; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 添加权限拦截器 + registry.addInterceptor(new JwtInterceptor()) + .addPathPatterns("/**") + .excludePathPatterns(ignorePathPatterns()); + } + + public List ignorePathPatterns() { + List paths = new ArrayList<>(); + paths.add("/swagger-resources/**"); + paths.add("/webjars/**"); + paths.add("/v3/**"); + paths.add("/swagger-ui.html/**"); + paths.add("/swagger-ui/**"); + paths.add("/webjars/"); + paths.add("/doc.html/**"); + paths.add("/error"); + paths.add("/favicon.ico"); + paths.add("/user/login"); + paths.add("/user/register"); + paths.add("/user/changePassWord"); + paths.add("/user/checkAccount"); + paths.add("/webSocket/**"); + paths.add("/ask/downloadTalkVideo"); + paths.add("/fileManage/downloadFile"); + paths.add("/aqLibrary/downloadQuestionLibraryTemplate"); + paths.add("/medicalRecManage/downloadMedicalAnswerTemplate"); + paths.add("/qaKnowledge/**"); + // 开发环境,放开不校验token.每次修改这里需要重启(热部署不行) +// paths.add("/**"); + return paths; + } + + @Bean + public ObjectMapper objectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + JavaTimeModule javaTimeModule = new JavaTimeModule(); + javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + + objectMapper.registerModule(javaTimeModule); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + + return objectMapper; + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/exception/BusinessException.java b/know_sub_common/src/main/java/com/supervision/knowsub/exception/BusinessException.java new file mode 100644 index 0000000..4b4061c --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/exception/BusinessException.java @@ -0,0 +1,76 @@ +/* + * 文 件 名: CustomException + * 版 权: + * 描 述: <描述> + * 修 改 人: RedName + * 修改时间: 2022/8/5 + * 跟踪单号: <跟踪单号> + * 修改单号: <修改单号> + * 修改内容: <修改内容> + */ +package com.supervision.knowsub.exception; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; + +/** + * <功能详细描述> + * 自定义异常 + * + * @author ljt + * @version [版本号, 2022/8/5] + * @see [相关类/方法] + * @since [产品/模块版本] + */ +@Slf4j +public class BusinessException extends RuntimeException { + /** + * 异常编码 + */ + private final Integer code; + + /** + * 异常信息 + */ + private final String message; + + public BusinessException(Throwable cause) { + super(cause); + this.code = HttpStatus.INTERNAL_SERVER_ERROR.value(); + this.message = null; + + } + + public BusinessException(Throwable cause, String message) { + super(cause); + this.code = HttpStatus.INTERNAL_SERVER_ERROR.value(); + this.message = message; + + } + + public BusinessException(String message) { + this.code = HttpStatus.INTERNAL_SERVER_ERROR.value(); + this.message = message; + } + + public BusinessException(String message, Integer code) { + this.message = message; + this.code = code; + } + + public BusinessException(String message, Throwable e) { + super(message, e); + log.error(message, e); + this.code = HttpStatus.INTERNAL_SERVER_ERROR.value(); + this.message = message; + } + + @Override + public String getMessage() { + return message; + } + + public Integer getCode() { + return code; + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/util/SpringBeanUtil.java b/know_sub_common/src/main/java/com/supervision/knowsub/util/SpringBeanUtil.java new file mode 100644 index 0000000..4c9cba8 --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/util/SpringBeanUtil.java @@ -0,0 +1,128 @@ +package com.supervision.knowsub.util; + +import cn.hutool.core.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/*** + * @author ljt + * @since 2020/8/4 17:37 + * + */ +@Component +@Lazy(false) +public class SpringBeanUtil implements ApplicationContextAware { + + private static ApplicationContext applicationContext; + + /** + * 获取applicationContext + * + * @return ApplicationContext + */ + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + @Override + public void setApplicationContext(@NotNull ApplicationContext applicationContext) { + SpringBeanUtil.applicationContext = applicationContext; + } + + //通过name获取 Bean. + + /** + * 通过name获取 Bean + * + * @param Bean类型 + * @param name Bean名称 + * @return Bean + */ + @SuppressWarnings("unchecked") + public static T getBean(String name) { + return (T) applicationContext.getBean(name); + } + + /** + * 通过class获取Bean + * + * @param Bean类型 + * @param clazz Bean类 + * @return Bean对象 + */ + public static T getBean(Class clazz) { + return applicationContext.getBean(clazz); + } + + /** + * 通过name,以及Clazz返回指定的Bean + * + * @param bean类型 + * @param name Bean名称 + * @param clazz bean类型 + * @return Bean对象 + */ + public static T getBean(String name, Class clazz) { + return applicationContext.getBean(name, clazz); + } + + /** + * 获取指定类型对应的所有Bean,包括子类 + * + * @param Bean类型 + * @param type 类、接口,null表示获取所有bean + * @return 类型对应的bean,key是bean注册的name,value是Bean + * @since 5.3.3 + */ + public static Map getBeansOfType(Class type) { + return applicationContext.getBeansOfType(type); + } + + /** + * 获取指定类型对应的Bean名称,包括子类 + * + * @param type 类、接口,null表示获取所有bean名称 + * @return bean名称 + * @since 5.3.3 + */ + public static String[] getBeanNamesForType(Class type) { + return applicationContext.getBeanNamesForType(type); + } + + /** + * 获取配置文件配置项的值 + * + * @param key 配置项key + * @return 属性值 + * @since 5.3.3 + */ + public static String getProperty(String key) { + return applicationContext.getEnvironment().getProperty(key); + } + + /** + * 获取当前的环境配置,无配置返回null + * + * @return 当前的环境配置 + * @since 5.3.3 + */ + public static String[] getActiveProfiles() { + return applicationContext.getEnvironment().getActiveProfiles(); + } + + /** + * 获取当前的环境配置,当有多个环境配置时,只获取第一个 + * + * @return 当前的环境配置 + * @since 5.3.3 + */ + public static String getActiveProfile() { + final String[] activeProfiles = getActiveProfiles(); + return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; + } +} diff --git a/know_sub_common/src/main/java/com/supervision/knowsub/util/TokenUtil.java b/know_sub_common/src/main/java/com/supervision/knowsub/util/TokenUtil.java new file mode 100644 index 0000000..b3c77e9 --- /dev/null +++ b/know_sub_common/src/main/java/com/supervision/knowsub/util/TokenUtil.java @@ -0,0 +1,19 @@ +package com.supervision.knowsub.util; + +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import cn.hutool.jwt.JWTUtil; +import cn.hutool.jwt.signers.JWTSigner; +import cn.hutool.jwt.signers.JWTSignerUtil; + +public class TokenUtil { + + public static String creatToken(String userInfo){ + final JWTSigner signer = JWTSignerUtil.hs256("123456".getBytes()); + JSONObject info = JSONUtil.parseObj(userInfo); + // 过期时间一天,同时这个字段也作为单点登录使用 + info.putOnce("expireTime",System.currentTimeMillis() + 1000 * 60 * 60 * 24); + info.putOnce("issueTime",System.currentTimeMillis()); + return JWTUtil.createToken(info, signer); + } +}