From 2f05a416acaf3cfa50473219d38385f92cbe3b71 Mon Sep 17 00:00:00 2001 From: Administrator <815176682@qq.com> Date: Fri, 10 Jul 2020 15:24:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E5=96=84Jwt=E6=8B=A6=E6=88=AA=E5=99=A8?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E5=88=87=E9=9D=A2?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E6=B7=BB=E5=8A=A0utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/axq/common/aspect/LogAspect.java | 161 +++++++++++ .../axq/common/cache/GuavaFunctionCache.java | 45 ++++ .../axq/common/conf/DruidConfiguration.java | 20 ++ .../axq/common/conf/InterceptorConfig.java | 28 ++ .../com/axq/common/conf/MultipartConfig.java | 27 ++ .../exception/BaseBusinessException.java | 2 +- .../ExceptionHandler.java | 6 +- .../intercepter/AuthInterceptor.java | 9 +- .../intercepter/CorsInterceptor.java | 2 +- .../{api => }/intercepter/JWTInterceptor.java | 13 +- .../ResonseResultData.java | 2 +- .../{api => responseDto}/ResponseCode.java | 2 +- src/main/java/com/axq/common/test/Test1.java | 13 - .../java/com/axq/common/utils/AESUtils.java | 67 +++++ .../axq/common/utils/AccessTokenUtils.java | 93 +++++++ .../java/com/axq/common/utils/GuavaCache.java | 41 +++ .../com/axq/common/utils/HttpClientUtils.java | 252 ++++++++++++++++++ .../java/com/axq/common/utils/IPUtil.java | 46 ++++ .../java/com/axq/common/utils/JwtUtils.java | 101 +++++++ .../java/com/axq/common/utils/MD5Util.java | 35 +++ .../com/axq/common/utils/RandomUtils.java | 14 + 21 files changed, 956 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/axq/common/aspect/LogAspect.java create mode 100644 src/main/java/com/axq/common/cache/GuavaFunctionCache.java create mode 100644 src/main/java/com/axq/common/conf/DruidConfiguration.java create mode 100644 src/main/java/com/axq/common/conf/InterceptorConfig.java create mode 100644 src/main/java/com/axq/common/conf/MultipartConfig.java rename src/main/java/com/axq/common/{api/exceptionhandler => exception}/ExceptionHandler.java (95%) rename src/main/java/com/axq/common/{api => }/intercepter/AuthInterceptor.java (64%) rename src/main/java/com/axq/common/{api => }/intercepter/CorsInterceptor.java (95%) rename src/main/java/com/axq/common/{api => }/intercepter/JWTInterceptor.java (52%) rename src/main/java/com/axq/common/{api => responseDto}/ResonseResultData.java (91%) rename src/main/java/com/axq/common/{api => responseDto}/ResponseCode.java (95%) delete mode 100644 src/main/java/com/axq/common/test/Test1.java create mode 100644 src/main/java/com/axq/common/utils/AESUtils.java create mode 100644 src/main/java/com/axq/common/utils/AccessTokenUtils.java create mode 100644 src/main/java/com/axq/common/utils/GuavaCache.java create mode 100644 src/main/java/com/axq/common/utils/HttpClientUtils.java create mode 100644 src/main/java/com/axq/common/utils/IPUtil.java create mode 100644 src/main/java/com/axq/common/utils/JwtUtils.java create mode 100644 src/main/java/com/axq/common/utils/MD5Util.java create mode 100644 src/main/java/com/axq/common/utils/RandomUtils.java diff --git a/src/main/java/com/axq/common/aspect/LogAspect.java b/src/main/java/com/axq/common/aspect/LogAspect.java new file mode 100644 index 0000000..2b28636 --- /dev/null +++ b/src/main/java/com/axq/common/aspect/LogAspect.java @@ -0,0 +1,161 @@ +package com.axq.common.aspect; + +/** + * @author Xiao Qiang + * @Class: ${file_name} + * @Description: ${todo} + * @date 2020/7/10 14:39 + */ +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.SourceLocation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + +@Component +@Aspect +public class LogAspect { + + private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private final String POINT_CUT = "execution(public * com.axq.controller.*.*(..))"; + + @Pointcut(POINT_CUT) + public void pointCut(){} + + @Before(value = "pointCut()") + public void before(JoinPoint joinPoint){ + logger.info("@Before通知执行"); + //获取目标方法参数信息 + Object[] args = joinPoint.getArgs(); + Arrays.stream(args).forEach(arg->{ // 大大 + try { + logger.info(OBJECT_MAPPER.writeValueAsString(arg)); + } catch (JsonProcessingException e) { + logger.info(arg.toString()); + } + }); + //aop代理对象 + Object aThis = joinPoint.getThis(); + logger.info(aThis.toString()); //com.xhx.springboot.controller.HelloController@69fbbcdd + + //被代理对象 + Object target = joinPoint.getTarget(); + logger.info(target.toString()); //com.xhx.springboot.controller.HelloController@69fbbcdd + + //获取连接点的方法签名对象 + Signature signature = joinPoint.getSignature(); + logger.info(signature.toLongString()); //public java.lang.String com.xhx.springboot.controller.HelloController.getName(java.lang.String) + logger.info(signature.toShortString()); //HelloController.getName(..) + logger.info(signature.toString()); //String com.xhx.springboot.controller.HelloController.getName(String) + //获取方法名 + logger.info(signature.getName()); //getName + //获取声明类型名 + logger.info(signature.getDeclaringTypeName()); //com.xhx.springboot.controller.HelloController + //获取声明类型 方法所在类的class对象 + logger.info(signature.getDeclaringType().toString()); //class com.xhx.springboot.controller.HelloController + //和getDeclaringTypeName()一样 + logger.info(signature.getDeclaringType().getName());//com.xhx.springboot.controller.HelloController + + //连接点类型 + String kind = joinPoint.getKind(); + logger.info(kind);//method-execution + + //返回连接点方法所在类文件中的位置 打印报异常 + SourceLocation sourceLocation = joinPoint.getSourceLocation(); + logger.info(sourceLocation.toString()); + //logger.info(sourceLocation.getFileName()); + //logger.info(sourceLocation.getLine()+""); + //logger.info(sourceLocation.getWithinType().toString()); //class com.xhx.springboot.controller.HelloController + + ///返回连接点静态部分 + JoinPoint.StaticPart staticPart = joinPoint.getStaticPart(); + logger.info(staticPart.toLongString()); //execution(public java.lang.String com.xhx.springboot.controller.HelloController.getName(java.lang.String)) + + + //attributes可以获取request信息 session信息等 + ServletRequestAttributes attributes = + (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + logger.info(request.getRequestURL().toString()); //http://127.0.0.1:8080/hello/getName + logger.info(request.getRemoteAddr()); //127.0.0.1 + logger.info(request.getMethod()); //GET + + logger.info("before通知执行结束"); + } + + + /** + * 后置返回 + * 如果第一个参数为JoinPoint,则第二个参数为返回值的信息 + * 如果第一个参数不为JoinPoint,则第一个参数为returning中对应的参数 + * returning:限定了只有目标方法返回值与通知方法参数类型匹配时才能执行后置返回通知,否则不执行, + * 参数为Object类型将匹配任何目标返回值 + */ + @AfterReturning(value = POINT_CUT,returning = "result") + public void doAfterReturningAdvice1(JoinPoint joinPoint,Object result){ + logger.info("第一个后置返回通知的返回值:"+result); + } + + @AfterReturning(value = POINT_CUT,returning = "result",argNames = "result") + public void doAfterReturningAdvice2(String result){ + logger.info("第二个后置返回通知的返回值:"+result); + } + //第一个后置返回通知的返回值:姓名是大大 + //第二个后置返回通知的返回值:姓名是大大 + //第一个后置返回通知的返回值:{name=小小, id=1} + + + /** + * 后置异常通知 + * 定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法; + * throwing:限定了只有目标方法抛出的异常与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行, + * 对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。 + * @param joinPoint + * @param exception + */ + @AfterThrowing(value = POINT_CUT,throwing = "exception") + public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){ + logger.info(joinPoint.getSignature().getName()); + if(exception instanceof NullPointerException){ + logger.info("发生了空指针异常!!!!!"); + } + } + + @After(value = POINT_CUT) + public void doAfterAdvice(JoinPoint joinPoint){ + logger.info("后置通知执行了!"); + } + + /** + * 环绕通知: + * 注意:Spring AOP的环绕通知会影响到AfterThrowing通知的运行,不要同时使用 + * + * 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。 + * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型 + */ + @Around(value = POINT_CUT) + public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ + logger.info("@Around环绕通知:"+proceedingJoinPoint.getSignature().toString()); + Object obj = null; + try { + obj = proceedingJoinPoint.proceed(); //可以加参数 + logger.info(obj.toString()); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + logger.info("@Around环绕通知执行结束"); + return obj; + } +} \ No newline at end of file diff --git a/src/main/java/com/axq/common/cache/GuavaFunctionCache.java b/src/main/java/com/axq/common/cache/GuavaFunctionCache.java new file mode 100644 index 0000000..35350c7 --- /dev/null +++ b/src/main/java/com/axq/common/cache/GuavaFunctionCache.java @@ -0,0 +1,45 @@ +package com.axq.common.cache; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class GuavaFunctionCache { + + private static final Log logger = LogFactory.getLog(GuavaFunctionCache.class); + + // LRU算法 + private static LoadingCache> localCache = CacheBuilder.newBuilder().initialCapacity(10).maximumSize(10000) + .expireAfterAccess(12, TimeUnit.HOURS).build(new CacheLoader>() { + // 默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载. + @Override + public List load(String userId) throws Exception { + + return new ArrayList<>(); + } + }); + + public static void setKey(String key, List functionList) { + localCache.put(key, functionList); + } + + public static List getKey(String key) { + List value = new ArrayList<>(); + try { + value = localCache.get(key); + if (value.size() == 0) { + return null; + } + return value; + } catch (Exception e) { + logger.error("localCache get error", e); + } + return null; + } +} diff --git a/src/main/java/com/axq/common/conf/DruidConfiguration.java b/src/main/java/com/axq/common/conf/DruidConfiguration.java new file mode 100644 index 0000000..38514c1 --- /dev/null +++ b/src/main/java/com/axq/common/conf/DruidConfiguration.java @@ -0,0 +1,20 @@ +package com.axq.common.conf; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +/** + * 数据源配置类 + */ +@Configuration +public class DruidConfiguration { + @Bean + @ConfigurationProperties(prefix = "spring.datasource") + public DataSource druid(){ + return new DruidDataSource(); + } +} diff --git a/src/main/java/com/axq/common/conf/InterceptorConfig.java b/src/main/java/com/axq/common/conf/InterceptorConfig.java new file mode 100644 index 0000000..f697364 --- /dev/null +++ b/src/main/java/com/axq/common/conf/InterceptorConfig.java @@ -0,0 +1,28 @@ +package com.axq.common.conf; + +import com.axq.common.intercepter.AuthInterceptor; +import com.axq.common.intercepter.CorsInterceptor; +import com.axq.common.intercepter.JWTInterceptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +/** + * 拦截器配置类 + */ +@Configuration +public class InterceptorConfig extends WebMvcConfigurationSupport { + + @Autowired + private AuthInterceptor authInterceptor; + + @Override + protected void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**"); + registry.addInterceptor(new JWTInterceptor()).addPathPatterns("/api/*"); +// registry.addInterceptor(authInterceptor).addPathPatterns("/api/*"); + super.addInterceptors(registry); + } + +} diff --git a/src/main/java/com/axq/common/conf/MultipartConfig.java b/src/main/java/com/axq/common/conf/MultipartConfig.java new file mode 100644 index 0000000..5bc9313 --- /dev/null +++ b/src/main/java/com/axq/common/conf/MultipartConfig.java @@ -0,0 +1,27 @@ +package com.axq.common.conf; + +import org.springframework.boot.web.servlet.MultipartConfigFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.unit.DataSize; +import org.springframework.util.unit.DataUnit; + +import javax.servlet.MultipartConfigElement; + +/** + * 附件配置类 + */ +@Configuration +public class MultipartConfig { + + @Bean + MultipartConfigElement multipartConfigElement() { + MultipartConfigFactory factory = new MultipartConfigFactory(); + factory.setLocation("/data/tmp"); + //文件最大 + factory.setMaxFileSize(DataSize.of(10240, DataUnit.KILOBYTES)); //KB,MB + /// 设置总上传数据总大小 + factory.setMaxRequestSize(DataSize.of(102400, DataUnit.KILOBYTES)); + return factory.createMultipartConfig(); + } +} diff --git a/src/main/java/com/axq/common/exception/BaseBusinessException.java b/src/main/java/com/axq/common/exception/BaseBusinessException.java index 639e9aa..36837cd 100644 --- a/src/main/java/com/axq/common/exception/BaseBusinessException.java +++ b/src/main/java/com/axq/common/exception/BaseBusinessException.java @@ -1,6 +1,6 @@ package com.axq.common.exception; -import com.axq.common.api.ResponseCode; +import com.axq.common.responseDto.ResponseCode; import lombok.Data; /** diff --git a/src/main/java/com/axq/common/api/exceptionhandler/ExceptionHandler.java b/src/main/java/com/axq/common/exception/ExceptionHandler.java similarity index 95% rename from src/main/java/com/axq/common/api/exceptionhandler/ExceptionHandler.java rename to src/main/java/com/axq/common/exception/ExceptionHandler.java index d3d948d..8d56faa 100644 --- a/src/main/java/com/axq/common/api/exceptionhandler/ExceptionHandler.java +++ b/src/main/java/com/axq/common/exception/ExceptionHandler.java @@ -1,7 +1,7 @@ -package com.axq.common.api.exceptionhandler; +package com.axq.common.exception; -import com.axq.common.api.ResonseResultData; -import com.axq.common.api.ResponseCode; +import com.axq.common.responseDto.ResonseResultData; +import com.axq.common.responseDto.ResponseCode; import com.alibaba.fastjson.JSON; import com.axq.common.exception.BaseBusinessException; import org.apache.commons.lang3.StringUtils; diff --git a/src/main/java/com/axq/common/api/intercepter/AuthInterceptor.java b/src/main/java/com/axq/common/intercepter/AuthInterceptor.java similarity index 64% rename from src/main/java/com/axq/common/api/intercepter/AuthInterceptor.java rename to src/main/java/com/axq/common/intercepter/AuthInterceptor.java index 9ca5035..7498849 100644 --- a/src/main/java/com/axq/common/api/intercepter/AuthInterceptor.java +++ b/src/main/java/com/axq/common/intercepter/AuthInterceptor.java @@ -1,6 +1,7 @@ -package com.axq.common.api.intercepter; +package com.axq.common.intercepter; +import com.axq.common.cache.GuavaFunctionCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -8,6 +9,7 @@ import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.List; @Component public class AuthInterceptor implements HandlerInterceptor { @@ -17,6 +19,11 @@ public class AuthInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("AuthInterceptor. preHandle"); + //黑白名单验证 + String userId = (String) request.getAttribute("token"); + //获取菜单权限 + List cacheFunctionList = GuavaFunctionCache.getKey(userId); + //如果缓存不存在,查询数据库,并且保存缓存 return true; } diff --git a/src/main/java/com/axq/common/api/intercepter/CorsInterceptor.java b/src/main/java/com/axq/common/intercepter/CorsInterceptor.java similarity index 95% rename from src/main/java/com/axq/common/api/intercepter/CorsInterceptor.java rename to src/main/java/com/axq/common/intercepter/CorsInterceptor.java index a8d92e0..efe15d6 100644 --- a/src/main/java/com/axq/common/api/intercepter/CorsInterceptor.java +++ b/src/main/java/com/axq/common/intercepter/CorsInterceptor.java @@ -1,4 +1,4 @@ -package com.axq.common.api.intercepter; +package com.axq.common.intercepter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/axq/common/api/intercepter/JWTInterceptor.java b/src/main/java/com/axq/common/intercepter/JWTInterceptor.java similarity index 52% rename from src/main/java/com/axq/common/api/intercepter/JWTInterceptor.java rename to src/main/java/com/axq/common/intercepter/JWTInterceptor.java index 89862b7..f04dda0 100644 --- a/src/main/java/com/axq/common/api/intercepter/JWTInterceptor.java +++ b/src/main/java/com/axq/common/intercepter/JWTInterceptor.java @@ -1,5 +1,7 @@ -package com.axq.common.api.intercepter; +package com.axq.common.intercepter; +import com.axq.common.utils.JwtUtils; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; @@ -14,7 +16,14 @@ public class JWTInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - logger.info("JWTInterceptor.preHandle"); + + final String generateToken = JwtUtils.createJwt("userId"); //用户登录生成token + logger.info("JWTInterceptor.preHandle"); + final String token = request.getHeader("token"); //发送请求带上token + String decodeJwt = JwtUtils.decodeJwt(token); + if (StringUtils.isNotBlank(decodeJwt)) { + request.setAttribute("token", decodeJwt); + } return true; } diff --git a/src/main/java/com/axq/common/api/ResonseResultData.java b/src/main/java/com/axq/common/responseDto/ResonseResultData.java similarity index 91% rename from src/main/java/com/axq/common/api/ResonseResultData.java rename to src/main/java/com/axq/common/responseDto/ResonseResultData.java index 72591ba..dd2d370 100644 --- a/src/main/java/com/axq/common/api/ResonseResultData.java +++ b/src/main/java/com/axq/common/responseDto/ResonseResultData.java @@ -1,4 +1,4 @@ -package com.axq.common.api; +package com.axq.common.responseDto; import lombok.Data; diff --git a/src/main/java/com/axq/common/api/ResponseCode.java b/src/main/java/com/axq/common/responseDto/ResponseCode.java similarity index 95% rename from src/main/java/com/axq/common/api/ResponseCode.java rename to src/main/java/com/axq/common/responseDto/ResponseCode.java index 6c3a55f..c2acf3b 100644 --- a/src/main/java/com/axq/common/api/ResponseCode.java +++ b/src/main/java/com/axq/common/responseDto/ResponseCode.java @@ -1,4 +1,4 @@ -package com.axq.common.api; +package com.axq.common.responseDto; /** * @author Xiao Qiang diff --git a/src/main/java/com/axq/common/test/Test1.java b/src/main/java/com/axq/common/test/Test1.java deleted file mode 100644 index 308ad6f..0000000 --- a/src/main/java/com/axq/common/test/Test1.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.axq.common.test; - -/** - * @author Xiao Qiang - * @Class: Test1 - * @Description: ${todo} - * @date 2020/6/26 15:58 - */ -public class Test1 { - public String test () { - return "dfs"; - } -} diff --git a/src/main/java/com/axq/common/utils/AESUtils.java b/src/main/java/com/axq/common/utils/AESUtils.java new file mode 100644 index 0000000..02c7f61 --- /dev/null +++ b/src/main/java/com/axq/common/utils/AESUtils.java @@ -0,0 +1,67 @@ +package com.axq.common.utils; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +/** + * key必须是16bytes + * + * @author Queal + * + */ +public class AESUtils { + + private static final Log logger = LogFactory.getLog(AESUtils.class); + private static final String KEY_ALGORITHM = "AES"; + private static final String SECURE_ALGORITHM = "AES/CBC/PKCS5Padding"; + + public static String encrypt(String key, String content) { + try { + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(SECURE_ALGORITHM); + byte[] byteContent = content.getBytes("utf-8"); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(key.getBytes())); + byte[] result = cipher.doFinal(byteContent); + return Base64.encodeBase64String(result); + } catch (Exception e) { + logger.error("login", e); + } + return null; + } + + public static String decrypt(String key, String content) { + try { + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(SECURE_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(key.getBytes())); + return new String(cipher.doFinal(Base64.decodeBase64(content)), "utf-8"); + } catch (Exception e) { + logger.error("login", e); + } + return null; + } + + public static String wXAppDecrypt(String sessionKey, String encryptedData, String iv) { + try { + // 被加密的数据 + byte[] dataByte = Base64.decodeBase64(encryptedData.getBytes()); + // 加密秘钥 + byte[] keyByte = Base64.decodeBase64(sessionKey.getBytes()); + // 偏移量 + byte[] ivByte = Base64.decodeBase64(iv.getBytes()); + + SecretKeySpec secretKeySpec = new SecretKeySpec(keyByte, KEY_ALGORITHM); + Cipher cipher = Cipher.getInstance(SECURE_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivByte)); + return new String(cipher.doFinal(dataByte), "utf-8"); + } catch (Exception e) { + logger.error("wxAppDecrypt: " + sessionKey + ", " + encryptedData + ", " + iv, e); + } + return null; + } +} diff --git a/src/main/java/com/axq/common/utils/AccessTokenUtils.java b/src/main/java/com/axq/common/utils/AccessTokenUtils.java new file mode 100644 index 0000000..674bb9f --- /dev/null +++ b/src/main/java/com/axq/common/utils/AccessTokenUtils.java @@ -0,0 +1,93 @@ +package com.axq.common.utils; + +import com.alibaba.fastjson.JSON; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.concurrent.TimeUnit; + +public class AccessTokenUtils { + private static final Log logger = LogFactory.getLog(AccessTokenUtils.class); + + private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appsecret}"; + private static final long EXPIRE_TIME = 7000; + + // LRU算法 + private static LoadingCache localCache = CacheBuilder.newBuilder().initialCapacity(1).maximumSize(10) + .expireAfterAccess(EXPIRE_TIME, TimeUnit.SECONDS).build(new CacheLoader() { + @Override + public String load(String siteId) throws Exception { + return StringUtils.EMPTY; + } + }); + + public static void put(String key, String accessToken) { + localCache.put(key, accessToken); + } + + public static String get(String key) { + String ret = null; + try { + ret = localCache.get(key); + } catch (Exception e) { + logger.error("localCache get error", e); + } + return ret; + } + + public static String requestAccessTk(String appId, String appSecret) { + String accessToken = null; + + HttpClient httpClient = HttpClientUtils.getHttpClient(); + String url = TOKEN_URL; + url = url.replace("${appid}", appId); + url = url.replace("${appsecret}", appSecret); + String ret = HttpClientUtils.doGet(httpClient, url); + AccessTokenResp resp = JSON.parseObject(ret, AccessTokenResp.class); + + if (resp.getErrcode() == null || resp.getErrcode() == 0) { + accessToken = resp.getAccess_token(); + } else { + logger.error("获取AccessToken失败: " + ret); + } + + return accessToken; + } + +} + +class AccessTokenResp { + private Integer errcode; + private String access_token; + private Integer expires_in; + + public Integer getErrcode() { + return errcode; + } + + public void setErrcode(Integer errcode) { + this.errcode = errcode; + } + + public String getAccess_token() { + return access_token; + } + + public void setAccess_token(String access_token) { + this.access_token = access_token; + } + + public Integer getExpires_in() { + return expires_in; + } + + public void setExpires_in(Integer expires_in) { + this.expires_in = expires_in; + } + +} diff --git a/src/main/java/com/axq/common/utils/GuavaCache.java b/src/main/java/com/axq/common/utils/GuavaCache.java new file mode 100644 index 0000000..c76e660 --- /dev/null +++ b/src/main/java/com/axq/common/utils/GuavaCache.java @@ -0,0 +1,41 @@ +package com.axq.common.utils; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.concurrent.TimeUnit; + + +public class GuavaCache { + private static final Log logger = LogFactory.getLog(GuavaCache.class); + + private static LoadingCache localCache = CacheBuilder.newBuilder().initialCapacity(10).maximumSize(10000).expireAfterAccess(2, TimeUnit.HOURS) + .build(new CacheLoader() { + //默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载. + @Override + public String load(String s) throws Exception { + return "null"; + } + }); + + public static void setKey(String key,String value){ + localCache.put(key,value); + } + + public static String getKey(String key){ + String value = null; + try { + value = localCache.get(key); + if("null".equals(value)){ + return null; + } + return value; + }catch (Exception e){ + logger.error("localCache get error",e); + } + return null; + } +} diff --git a/src/main/java/com/axq/common/utils/HttpClientUtils.java b/src/main/java/com/axq/common/utils/HttpClientUtils.java new file mode 100644 index 0000000..0824fda --- /dev/null +++ b/src/main/java/com/axq/common/utils/HttpClientUtils.java @@ -0,0 +1,252 @@ +package com.axq.common.utils; + +import org.apache.commons.httpclient.*; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.commons.httpclient.methods.multipart.*; +import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HttpClientUtils { + + /** 连接超时时间 */ + private static final int defaultConnectionTimeout = 10000; + /** 回应超时时间 */ + private static final int defaultSoTimeout = 30000; + /** 闲置连接超时时间 */ + private static final int defaultIdleConnTimeout = 60000; + private static final int defaultMaxConnPerHost = 30; + private static final int defaultMaxTotalConn = 80; + + /** 默认等待HttpConnectionManager返回连接超时(只有在达到最大连接数时起作用) */ + private static final long defaultHttpConnectionManagerTimeout = 3 * 1000; + + private static HttpConnectionManager connectionManager; + + static { + connectionManager = new MultiThreadedHttpConnectionManager(); + connectionManager.getParams().setDefaultMaxConnectionsPerHost( + defaultMaxConnPerHost); + connectionManager.getParams().setMaxTotalConnections( + defaultMaxTotalConn); + + IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread(); + ict.addConnectionManager(connectionManager); + ict.setConnectionTimeout(defaultIdleConnTimeout); + + ict.start(); + } + + public static HttpClient getHttpClient() { + HttpClient httpClient = new HttpClient(connectionManager); + httpClient.getHttpConnectionManager().getParams() + .setConnectionTimeout(defaultConnectionTimeout); + httpClient.getHttpConnectionManager().getParams() + .setSoTimeout(defaultSoTimeout); + httpClient.getParams().setConnectionManagerTimeout( + defaultHttpConnectionManagerTimeout); + return httpClient; + } + + public static String doPost(HttpClient httpClient, String url, + List params) { + String body = ""; + + try { + PostMethod httppost = new PostMethod(url); + httppost.getParams().setContentCharset("utf-8"); + if (params != null) { + NameValuePair[] p = new NameValuePair[params.size()]; + params.toArray(p); + httppost.setRequestBody(p); + } + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static String doPost(HttpClient httpClient, String url, + Map params) { + String body = ""; + + try { + PostMethod httppost = new PostMethod(url); + httppost.getParams().setContentCharset("utf-8"); + if (params != null) { + NameValuePair[] p = new NameValuePair[params.size()]; + int i = 0; + for (String key : params.keySet()) { + p[i++] = new NameValuePair(key, params.get(key)); + } + httppost.setRequestBody(p); + } + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static String doPostJson(HttpClient httpClient, String url, + String json) { + String body = ""; + try { + PostMethod httppost = new PostMethod(url); + StringRequestEntity entity = new StringRequestEntity(json, + "application/json", "utf-8"); + httppost.setRequestEntity(entity); + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static String doPostXml(HttpClient httpClient, String url, String xml) { + String body = ""; + try { + PostMethod httppost = new PostMethod(url); + StringRequestEntity entity = new StringRequestEntity(xml, + "text/xml", "utf-8"); + httppost.setRequestEntity(entity); + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static String doGet(HttpClient httpClient, String url) { + String body = ""; + try { + GetMethod httpget = new GetMethod(url); + httpget.getParams().setContentCharset("utf-8"); + httpClient.executeMethod(httpget); + + if (httpget.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httpget.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httpget.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static byte[] getFile(HttpClient httpClient, String url) { + byte[] file = null; + try { + GetMethod httpget = new GetMethod(url); + httpget.getParams().setContentCharset("utf-8"); + httpClient.executeMethod(httpget); + + if (httpget.getStatusCode() == HttpStatus.SC_OK) { + file = httpget.getResponseBody(); + } + httpget.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return file; + } + + public static String uploadFile(HttpClient httpClient, String url, + List params, String filename, byte[] data) { + String body = ""; + try { + PostMethod httppost = new PostMethod(url); + httppost.getParams().setContentCharset("utf-8"); + + List parts = new ArrayList(); + parts.add(new FilePart(filename, new ByteArrayPartSource(filename, + data))); + + for (NameValuePair pair : params) { + parts.add(new StringPart(pair.getName(), pair.getValue())); + } + + Part[] _parts = new Part[parts.size()]; + parts.toArray(_parts); + + MultipartRequestEntity entity = new MultipartRequestEntity(_parts, + httpClient.getParams()); + httppost.setRequestEntity(entity); + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + + public static String uploadFileWechat(HttpClient httpClient, String url, + List params, String filename, byte[] data) { + String body = ""; + try { + PostMethod httppost = new PostMethod(url); + httppost.getParams().setContentCharset("utf-8"); + + List parts = new ArrayList(); + parts.add(new FilePart("media", new ByteArrayPartSource(filename, + data))); + + for (NameValuePair pair : params) { + parts.add(new StringPart(pair.getName(), pair.getValue())); + } + + Part[] _parts = new Part[parts.size()]; + parts.toArray(_parts); + + MultipartRequestEntity entity = new MultipartRequestEntity(_parts, + httpClient.getParams()); + httppost.setRequestEntity(entity); + httpClient.executeMethod(httppost); + + if (httppost.getStatusCode() == HttpStatus.SC_OK) { + byte[] bytes = httppost.getResponseBody(); + body = new String(bytes, "utf-8"); + } + httppost.releaseConnection(); + } catch (Exception e) { + e.printStackTrace(); + } + return body; + } + +} diff --git a/src/main/java/com/axq/common/utils/IPUtil.java b/src/main/java/com/axq/common/utils/IPUtil.java new file mode 100644 index 0000000..29716b4 --- /dev/null +++ b/src/main/java/com/axq/common/utils/IPUtil.java @@ -0,0 +1,46 @@ +package com.axq.common.utils; + +/** + * @author Xiao Qiang + * @Class: ${file_name} + * @Description: ${todo} + * @date 2020/7/10 14:28 + */ + +import javax.servlet.http.HttpServletRequest; + +/** + * 获取对象的IP地址等信息 */ +public class IPUtil { + + /** + * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, + * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? + * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 + * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, + * 192.168.1.100 + * 用户真实IP为: 192.168.1.110 + * @param request + * @return + */ + public static String getIpAddress(HttpServletRequest request) { + String ip = request.getHeader("X-forwarded-for"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } + +} \ No newline at end of file diff --git a/src/main/java/com/axq/common/utils/JwtUtils.java b/src/main/java/com/axq/common/utils/JwtUtils.java new file mode 100644 index 0000000..42b0237 --- /dev/null +++ b/src/main/java/com/axq/common/utils/JwtUtils.java @@ -0,0 +1,101 @@ +package com.axq.common.utils; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTCreator.Builder; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.SignatureVerificationException; +import com.auth0.jwt.exceptions.TokenExpiredException; +import com.auth0.jwt.interfaces.DecodedJWT; + +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +public class JwtUtils { + + private static final String SECRET = "rcJGQWvhoMHughQg"; + private static final String JWT_KEY = "adminId"; + private static final Algorithm ALGORITHM = Algorithm.HMAC256(SECRET); + + /** + * 过期时间30分钟 + * + * @param key + * @return + */ + public static String createJwt(String key) { + Calendar expiresTime = Calendar.getInstance(); + expiresTime.add(Calendar.MINUTE, 500); + + return JWT.create().withClaim(JWT_KEY, key).withExpiresAt(expiresTime.getTime()).sign(ALGORITHM); + } + + /** + * 过期时间飘逸容错3分钟 + * + * @param token + * @return + * @throws TokenExpiredException + * 过期 Exception + * @throws SignatureVerificationException + * 签名不合法 Exception + */ + public static String decodeJwt(String token) throws TokenExpiredException, SignatureVerificationException { + DecodedJWT jwt = JWT.require(ALGORITHM).acceptExpiresAt(180).build().verify(token); + + return jwt.getClaim(JWT_KEY).asString(); + } + + public static String createJwtWithParams(Map params) { + Calendar expiresTime = Calendar.getInstance(); + expiresTime.add(Calendar.MINUTE, 30); + + Builder jwtBuilder = JWT.create(); + + params.forEach((key, val) -> { + jwtBuilder.withClaim(key, val); + }); + + return jwtBuilder.withExpiresAt(expiresTime.getTime()).sign(ALGORITHM); + } + + public static Map decodeJwtWithParams(String token) throws TokenExpiredException, SignatureVerificationException { + Map params = new HashMap<>(); + DecodedJWT jwt = JWT.require(ALGORITHM).acceptExpiresAt(180).build().verify(token); + + jwt.getClaims().forEach((key, val) -> { + params.put(key, val.asString()); + }); + + return params; + } + + public static void main(String[] args) throws Exception { + + String userId = "queal"; + long start = System.currentTimeMillis(); + String token = null; + for (int i = 0; i < 5; i++) { + token = createJwt(userId); + System.out.println(token); + + System.out.println(decodeJwt(token)); + } + System.out.println((System.currentTimeMillis() - start)); + + for (int i = 0; i < 1; i++) { + start = System.currentTimeMillis(); + + try { + System.out.println(decodeJwt(token)); + } catch (TokenExpiredException e) { + // token 过期 + e.printStackTrace(); + } catch (SignatureVerificationException e) { + // 解密失败 + e.printStackTrace(); + } + System.out.println((System.currentTimeMillis() - start)); + } + } +} diff --git a/src/main/java/com/axq/common/utils/MD5Util.java b/src/main/java/com/axq/common/utils/MD5Util.java new file mode 100644 index 0000000..d4624bd --- /dev/null +++ b/src/main/java/com/axq/common/utils/MD5Util.java @@ -0,0 +1,35 @@ +package com.axq.common.utils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD5加密类(封装jdk自带的md5加密方法) + */ +public class MD5Util { + + public static String encrypt(String source) { + return encodeMd5(source.getBytes()); + } + + private static String encodeMd5(byte[] source) { + try { + return encodeHex(MessageDigest.getInstance("MD5").digest(source)); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + + private static String encodeHex(byte[] bytes) { + StringBuffer buffer = new StringBuffer(bytes.length * 2); + for (int i = 0; i < bytes.length; i++) { + if (((int) bytes[i] & 0xff) < 0x10) { + + buffer.append("0"); + } + buffer.append(Long.toString((int) bytes[i] & 0xff, 16)); + } + return buffer.toString(); + } + +} diff --git a/src/main/java/com/axq/common/utils/RandomUtils.java b/src/main/java/com/axq/common/utils/RandomUtils.java new file mode 100644 index 0000000..49c16b8 --- /dev/null +++ b/src/main/java/com/axq/common/utils/RandomUtils.java @@ -0,0 +1,14 @@ +package com.axq.common.utils; + +import org.apache.commons.lang3.RandomStringUtils; + +public class RandomUtils { + + public static String getRandomId() { + return RandomStringUtils.randomAlphanumeric(32); + } + + public static String getRandomSalt() { + return RandomStringUtils.randomAlphanumeric(16); + } +} -- Gitee From 608e9aa8a6669c9b7bfa9ec0c25637687a085591 Mon Sep 17 00:00:00 2001 From: Administrator <815176682@qq.com> Date: Fri, 10 Jul 2020 15:37:05 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/axq/common/utils/GuavaCache.java | 5 +++++ src/main/java/com/axq/common/utils/JwtUtils.java | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/com/axq/common/utils/GuavaCache.java b/src/main/java/com/axq/common/utils/GuavaCache.java index c76e660..8c88555 100644 --- a/src/main/java/com/axq/common/utils/GuavaCache.java +++ b/src/main/java/com/axq/common/utils/GuavaCache.java @@ -9,6 +9,11 @@ import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; +/** + * 簡單緩存 + * key string + * value string + */ public class GuavaCache { private static final Log logger = LogFactory.getLog(GuavaCache.class); diff --git a/src/main/java/com/axq/common/utils/JwtUtils.java b/src/main/java/com/axq/common/utils/JwtUtils.java index 42b0237..ecbe40c 100644 --- a/src/main/java/com/axq/common/utils/JwtUtils.java +++ b/src/main/java/com/axq/common/utils/JwtUtils.java @@ -11,6 +11,9 @@ import java.util.Calendar; import java.util.HashMap; import java.util.Map; +/** + * Jwt工具类 + */ public class JwtUtils { private static final String SECRET = "rcJGQWvhoMHughQg"; -- Gitee From 7d8770de3a55f93810f7376287c858ff63c6b680 Mon Sep 17 00:00:00 2001 From: Administrator <815176682@qq.com> Date: Fri, 10 Jul 2020 17:57:08 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../axq/common/conf/DruidConfiguration.java | 40 ++++++------- .../axq/common/conf/InterceptorConfig.java | 56 +++++++++---------- .../com/axq/common/conf/MultipartConfig.java | 54 +++++++++--------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/axq/common/conf/DruidConfiguration.java b/src/main/java/com/axq/common/conf/DruidConfiguration.java index 38514c1..4799409 100644 --- a/src/main/java/com/axq/common/conf/DruidConfiguration.java +++ b/src/main/java/com/axq/common/conf/DruidConfiguration.java @@ -1,20 +1,20 @@ -package com.axq.common.conf; - -import com.alibaba.druid.pool.DruidDataSource; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import javax.sql.DataSource; - -/** - * 数据源配置类 - */ -@Configuration -public class DruidConfiguration { - @Bean - @ConfigurationProperties(prefix = "spring.datasource") - public DataSource druid(){ - return new DruidDataSource(); - } -} +//package com.axq.common.conf; +// +//import com.alibaba.druid.pool.DruidDataSource; +//import org.springframework.boot.context.properties.ConfigurationProperties; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//import javax.sql.DataSource; +// +///** +// * 数据源配置类 +// */ +//@Configuration +//public class DruidConfiguration { +// @Bean +// @ConfigurationProperties(prefix = "spring.datasource") +// public DataSource druid(){ +// return new DruidDataSource(); +// } +//} diff --git a/src/main/java/com/axq/common/conf/InterceptorConfig.java b/src/main/java/com/axq/common/conf/InterceptorConfig.java index f697364..aa3807a 100644 --- a/src/main/java/com/axq/common/conf/InterceptorConfig.java +++ b/src/main/java/com/axq/common/conf/InterceptorConfig.java @@ -1,28 +1,28 @@ -package com.axq.common.conf; - -import com.axq.common.intercepter.AuthInterceptor; -import com.axq.common.intercepter.CorsInterceptor; -import com.axq.common.intercepter.JWTInterceptor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; - -/** - * 拦截器配置类 - */ -@Configuration -public class InterceptorConfig extends WebMvcConfigurationSupport { - - @Autowired - private AuthInterceptor authInterceptor; - - @Override - protected void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**"); - registry.addInterceptor(new JWTInterceptor()).addPathPatterns("/api/*"); -// registry.addInterceptor(authInterceptor).addPathPatterns("/api/*"); - super.addInterceptors(registry); - } - -} +//package com.axq.common.conf; +// +//import com.axq.common.intercepter.AuthInterceptor; +//import com.axq.common.intercepter.CorsInterceptor; +//import com.axq.common.intercepter.JWTInterceptor; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +//import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +// +///** +// * 拦截器配置类 +// */ +//@Configuration +//public class InterceptorConfig extends WebMvcConfigurationSupport { +// +// @Autowired +// private AuthInterceptor authInterceptor; +// +// @Override +// protected void addInterceptors(InterceptorRegistry registry) { +// registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**"); +// registry.addInterceptor(new JWTInterceptor()).addPathPatterns("/api/*"); +//// registry.addInterceptor(authInterceptor).addPathPatterns("/api/*"); +// super.addInterceptors(registry); +// } +// +//} diff --git a/src/main/java/com/axq/common/conf/MultipartConfig.java b/src/main/java/com/axq/common/conf/MultipartConfig.java index 5bc9313..248fa40 100644 --- a/src/main/java/com/axq/common/conf/MultipartConfig.java +++ b/src/main/java/com/axq/common/conf/MultipartConfig.java @@ -1,27 +1,27 @@ -package com.axq.common.conf; - -import org.springframework.boot.web.servlet.MultipartConfigFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.util.unit.DataSize; -import org.springframework.util.unit.DataUnit; - -import javax.servlet.MultipartConfigElement; - -/** - * 附件配置类 - */ -@Configuration -public class MultipartConfig { - - @Bean - MultipartConfigElement multipartConfigElement() { - MultipartConfigFactory factory = new MultipartConfigFactory(); - factory.setLocation("/data/tmp"); - //文件最大 - factory.setMaxFileSize(DataSize.of(10240, DataUnit.KILOBYTES)); //KB,MB - /// 设置总上传数据总大小 - factory.setMaxRequestSize(DataSize.of(102400, DataUnit.KILOBYTES)); - return factory.createMultipartConfig(); - } -} +//package com.axq.common.conf; +// +//import org.springframework.boot.web.servlet.MultipartConfigFactory; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.util.unit.DataSize; +//import org.springframework.util.unit.DataUnit; +// +//import javax.servlet.MultipartConfigElement; +// +///** +// * 附件配置类 +// */ +//@Configuration +//public class MultipartConfig { +// +// @Bean +// MultipartConfigElement multipartConfigElement() { +// MultipartConfigFactory factory = new MultipartConfigFactory(); +// factory.setLocation("/data/tmp"); +// //文件最大 +// factory.setMaxFileSize(DataSize.of(10240, DataUnit.KILOBYTES)); //KB,MB +// /// 设置总上传数据总大小 +// factory.setMaxRequestSize(DataSize.of(102400, DataUnit.KILOBYTES)); +// return factory.createMultipartConfig(); +// } +//} -- Gitee From 0dda376f68023e85f1c5699a9174a721b0e18b18 Mon Sep 17 00:00:00 2001 From: Administrator <815176682@qq.com> Date: Fri, 10 Jul 2020 18:20:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.iml | 156 ++++++++++++++++++ .../{conf => config}/DruidConfiguration.java | 2 +- .../{conf => config}/InterceptorConfig.java | 2 +- .../{conf => config}/MultipartConfig.java | 2 +- 4 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 common.iml rename src/main/java/com/axq/common/{conf => config}/DruidConfiguration.java (94%) rename src/main/java/com/axq/common/{conf => config}/InterceptorConfig.java (96%) rename src/main/java/com/axq/common/{conf => config}/MultipartConfig.java (96%) diff --git a/common.iml b/common.iml new file mode 100644 index 0000000..1614392 --- /dev/null +++ b/common.iml @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/axq/common/conf/DruidConfiguration.java b/src/main/java/com/axq/common/config/DruidConfiguration.java similarity index 94% rename from src/main/java/com/axq/common/conf/DruidConfiguration.java rename to src/main/java/com/axq/common/config/DruidConfiguration.java index 4799409..d3d36e5 100644 --- a/src/main/java/com/axq/common/conf/DruidConfiguration.java +++ b/src/main/java/com/axq/common/config/DruidConfiguration.java @@ -1,4 +1,4 @@ -//package com.axq.common.conf; +//package com.axq.common.config; // //import com.alibaba.druid.pool.DruidDataSource; //import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/src/main/java/com/axq/common/conf/InterceptorConfig.java b/src/main/java/com/axq/common/config/InterceptorConfig.java similarity index 96% rename from src/main/java/com/axq/common/conf/InterceptorConfig.java rename to src/main/java/com/axq/common/config/InterceptorConfig.java index aa3807a..09df6e7 100644 --- a/src/main/java/com/axq/common/conf/InterceptorConfig.java +++ b/src/main/java/com/axq/common/config/InterceptorConfig.java @@ -1,4 +1,4 @@ -//package com.axq.common.conf; +//package com.axq.common.config; // //import com.axq.common.intercepter.AuthInterceptor; //import com.axq.common.intercepter.CorsInterceptor; diff --git a/src/main/java/com/axq/common/conf/MultipartConfig.java b/src/main/java/com/axq/common/config/MultipartConfig.java similarity index 96% rename from src/main/java/com/axq/common/conf/MultipartConfig.java rename to src/main/java/com/axq/common/config/MultipartConfig.java index 248fa40..e44f5da 100644 --- a/src/main/java/com/axq/common/conf/MultipartConfig.java +++ b/src/main/java/com/axq/common/config/MultipartConfig.java @@ -1,4 +1,4 @@ -//package com.axq.common.conf; +//package com.axq.common.config; // //import org.springframework.boot.web.servlet.MultipartConfigFactory; //import org.springframework.context.annotation.Bean; -- Gitee