From ec2540e000f7c1b610c72c54af307c2a53fac4cb Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Tue, 9 Aug 2022 14:37:00 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=BC=95=E5=85=A5redis=E3=80=822.=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0token=E6=A0=A1=E9=AA=8C=EF=BC=8C=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E7=9A=84=E6=98=AFweb=E5=B7=A5=E4=BD=9C=E7=AB=AF=E7=9A=84token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++- .../epmet/jmreport/aop/RequestLogAspect.java | 40 ++++++++++++++ .../config/JimuReportTokenService.java | 43 +++++++++++++-- .../jmreport/config/NacosRegisterConfig.java | 17 +++++- .../config/redis/JsonRedisSerializer.java | 54 +++++++++++++++++++ .../jmreport/config/redis/RedisConfig.java | 32 +++++++++++ src/main/resources/application-dev.yml | 10 +++- src/main/resources/application-local.yml | 20 +++++++ src/main/resources/application-prod.yml | 11 +++- src/main/resources/application-test.yml | 10 +++- src/main/resources/application.yml | 2 +- 11 files changed, 235 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/epmet/jmreport/aop/RequestLogAspect.java create mode 100644 src/main/java/com/epmet/jmreport/config/redis/JsonRedisSerializer.java create mode 100644 src/main/java/com/epmet/jmreport/config/redis/RedisConfig.java create mode 100644 src/main/resources/application-local.yml diff --git a/pom.xml b/pom.xml index 24e28a5..ebc121d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,13 +47,18 @@ - com.alibaba.boot nacos-discovery-spring-boot-starter 0.2.2 + + org.springframework.boot + spring-boot-starter-data-redis + + + org.projectlombok diff --git a/src/main/java/com/epmet/jmreport/aop/RequestLogAspect.java b/src/main/java/com/epmet/jmreport/aop/RequestLogAspect.java new file mode 100644 index 0000000..9edd286 --- /dev/null +++ b/src/main/java/com/epmet/jmreport/aop/RequestLogAspect.java @@ -0,0 +1,40 @@ +//package com.epmet.jmreport.aop; +// +//import com.epmet.commons.tools.aspect.BaseRequestLogAspect; +//import org.aspectj.lang.ProceedingJoinPoint; +//import org.aspectj.lang.annotation.Around; +//import org.aspectj.lang.annotation.Aspect; +//import org.springframework.core.annotation.Order; +//import org.springframework.stereotype.Component; +//import org.springframework.web.context.request.RequestAttributes; +//import org.springframework.web.context.request.RequestContextHolder; +//import org.springframework.web.context.request.ServletRequestAttributes; +// +//import javax.servlet.http.HttpServletRequest; +// +///** +// * 日志/异常处理切面实现,调用父类方法完成日志记录和异常处理。 +// */ +//@Aspect +//@Component +//@Order(0) +//public class RequestLogAspect { +// +// @Override +// @Around(value = "execution(* com...*Controller*.*(..)) ") +// public Object proceed(ProceedingJoinPoint point) throws Throwable { +// return super.proceed(point, getRequest()); +// } +// +// /** +// * 获取Request对象 +// * +// * @return +// */ +// private HttpServletRequest getRequest() { +// RequestAttributes ra = RequestContextHolder.getRequestAttributes(); +// ServletRequestAttributes sra = (ServletRequestAttributes) ra; +// return sra.getRequest(); +// } +// +//} diff --git a/src/main/java/com/epmet/jmreport/config/JimuReportTokenService.java b/src/main/java/com/epmet/jmreport/config/JimuReportTokenService.java index acdffcc..c44ebd7 100644 --- a/src/main/java/com/epmet/jmreport/config/JimuReportTokenService.java +++ b/src/main/java/com/epmet/jmreport/config/JimuReportTokenService.java @@ -1,13 +1,18 @@ package com.epmet.jmreport.config; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.jeecg.modules.jmreport.api.JmReportTokenServiceI; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestAttributes; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; +import java.util.Base64; +import java.util.Map; /** * 自定义积木报表鉴权(如果不进行自定义,则所有请求不做权限控制) @@ -15,8 +20,14 @@ import javax.servlet.http.HttpServletRequest; * 2.自定义获取登录用户 */ @Component +@Slf4j public class JimuReportTokenService implements JmReportTokenServiceI { + @Autowired + private RedisTemplate redisTemplate; + + public static final String tokenRedisKey = "Authorization"; + /** * 通过请求获取Token(前端通过?token=xxx访问预览,预览会以X-Access-Token/token为header访问后台,然后会调用此方法) * 然后请求业务后台api,会以X-Access-Token/token为header访问后台。 @@ -50,8 +61,30 @@ public class JimuReportTokenService implements JmReportTokenServiceI { */ @Override public Boolean verifyToken(String token) { - System.out.println("---------verify-----Token---------------"); - //return TokenUtils.verifyToken(token, sysBaseAPI, redisUtil); + + log.info("JimuReportTokenService#verifyToken :: 接收到token为:{}", token); + + if (StringUtils.isBlank(token)) { + // 没有token,不样访问 + return false; + } + + try { + String[] parts = token.split("\\."); + byte[] jsonByte = Base64.getDecoder().decode(parts[1]); + Object userId = JSON.parseObject(new String(jsonByte)).get("userId"); + + HashOperations hashOperations = redisTemplate.opsForHash(); + Map values = hashOperations.entries("epmet:sys:security:user:gov:web:" + userId); + + if (values.size() == 0) { + return false; + } + } catch (Exception e) { + log.error("token无法截取,格式错误"); + return false; + } + return true; } diff --git a/src/main/java/com/epmet/jmreport/config/NacosRegisterConfig.java b/src/main/java/com/epmet/jmreport/config/NacosRegisterConfig.java index a5df626..be79424 100644 --- a/src/main/java/com/epmet/jmreport/config/NacosRegisterConfig.java +++ b/src/main/java/com/epmet/jmreport/config/NacosRegisterConfig.java @@ -38,6 +38,9 @@ public class NacosRegisterConfig { @Value("${nacos.discovery.server-addr}") private String nacosServerAddr; + @Value("${nacos.discovery.enabled}") + private String enabled; + private NamingService naming; /** @@ -45,6 +48,12 @@ public class NacosRegisterConfig { */ @PostConstruct public void register() { + + // 未开启nacos,则不注册 + if (!Boolean.valueOf(enabled)) { + return; + } + try { naming = NamingFactory.createNamingService(initProperties()); @@ -69,11 +78,17 @@ public class NacosRegisterConfig { return properties; } - /** + /**s * 关闭之前,手动调用注销接口,从nacos注销此实例 */ @PreDestroy public void deregister() { + + // 未开启nacos,则不注册 + if (!Boolean.valueOf(enabled)) { + return; + } + try { InetAddress localIp = findFirstNonLoopbackAddress(); naming.deregisterInstance(serviceName, localIp.getHostAddress(), Integer.valueOf(port)); diff --git a/src/main/java/com/epmet/jmreport/config/redis/JsonRedisSerializer.java b/src/main/java/com/epmet/jmreport/config/redis/JsonRedisSerializer.java new file mode 100644 index 0000000..e890503 --- /dev/null +++ b/src/main/java/com/epmet/jmreport/config/redis/JsonRedisSerializer.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.epmet.jmreport.config.redis; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.ParserConfig; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson.util.IOUtils; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + + +/** + * Redis序列化 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +public class JsonRedisSerializer implements RedisSerializer { + private static ParserConfig defaultRedisConfig = new ParserConfig(); + static { + defaultRedisConfig.setAutoTypeSupport(true); + } + + private Class type; + + public JsonRedisSerializer(Class type) { + this.type = type; + } + + @Override + public byte[] serialize(T t) throws SerializationException { + if (t == null) { + return new byte[0]; + } + return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(IOUtils.UTF8); + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException { + if (bytes == null || bytes.length <= 0) { + return null; + } + String str = new String(bytes, IOUtils.UTF8); + + return JSON.parseObject(str, type, defaultRedisConfig); + } +} diff --git a/src/main/java/com/epmet/jmreport/config/redis/RedisConfig.java b/src/main/java/com/epmet/jmreport/config/redis/RedisConfig.java new file mode 100644 index 0000000..cfc7f6f --- /dev/null +++ b/src/main/java/com/epmet/jmreport/config/redis/RedisConfig.java @@ -0,0 +1,32 @@ +package com.epmet.jmreport.config.redis; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import javax.annotation.Resource; + +/** + * Redis配置 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Configuration +public class RedisConfig { + @Resource + private RedisConnectionFactory factory; + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JsonRedisSerializer<>(Object.class)); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(new JsonRedisSerializer<>(Object.class)); + redisTemplate.setConnectionFactory(factory); + return redisTemplate; + } +} \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index ddcca12..6aa8ff3 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -9,4 +9,12 @@ mysql: nacos: discovery: server-addr: 192.168.1.140:8848 - namespace: 1fecc730-5e6e-464c-aae9-7567944e7936 \ No newline at end of file + namespace: 1fecc730-5e6e-464c-aae9-7567944e7936 + enabled: true + +spring: + redis: + host: 192.168.1.140 + port: 6379 + database: 0 + password: 123456 diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml new file mode 100644 index 0000000..5e5a49f --- /dev/null +++ b/src/main/resources/application-local.yml @@ -0,0 +1,20 @@ +# 项目启动的时候,会根据环境,取这个配置 +mysql: + host: 192.168.1.140 + port: 3306 + db: epmet_jmreport + username: root + password: root + +nacos: + discovery: + server-addr: 192.168.1.140:8848 + namespace: 1fecc730-5e6e-464c-aae9-7567944e7936 + enabled: false + +spring: + redis: + host: 192.168.1.140 + port: 6379 + database: 0 + password: 123456 \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 0e5b8cd..6a9b42a 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -9,4 +9,13 @@ mysql: nacos: discovery: server-addr: 192.168.11.180:8848 - namespace: bd205d23-e696-47be-b995-916313f86e99 \ No newline at end of file + namespace: bd205d23-e696-47be-b995-916313f86e99 + enabled: true + + +spring: + redis: + host: r-m5ez3n1j0qc3ykq2ut.redis.rds.aliyuncs.com + port: 6379 + database: 0 + password: EpmEtclOUdrEdIs!Q2w \ No newline at end of file diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index af68834..a5431b3 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -10,4 +10,12 @@ mysql: nacos: discovery: server-addr: 192.168.10.150:8848 - namespace: 67e3c350-533e-4d7c-9f8f-faf1b4aa82ae \ No newline at end of file + namespace: 67e3c350-533e-4d7c-9f8f-faf1b4aa82ae + enabled: true + +spring: + redis: + host: 192.168.10.150 + port: 6379 + database: 0 + password: EpmEtrEdIs!q@w \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ada1912..a71997a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,7 +10,7 @@ spring: password: ${mysql.password:root} driver-class-name: com.mysql.cj.jdbc.Driver profiles: - active: dev + active: local jeecg : # local|minio|alioss