From 5d991923bdfbc6b7f820ba4b9ee3dde0a2cd8b03 Mon Sep 17 00:00:00 2001 From: weikai <123456> Date: Mon, 27 Apr 2020 15:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 28 ++ epdc-cloud-websocket/Dockerfile | 20 ++ epdc-cloud-websocket/pom.xml | 254 ++++++++++++++++++ .../elink/esua/epdc/ModuleApplication.java | 31 +++ .../esua/epdc/config/ModuleConfigImpl.java | 27 ++ .../esua/epdc/config/WebSocketConfig.java | 126 +++++++++ .../epdc/constants/WebSocketConstant.java | 20 ++ .../esua/epdc/controller/Demo2Controller.java | 47 ++++ .../epdc/controller/MenuNoticController.java | 44 +++ .../esua/epdc/exception/ModuleErrorCode.java | 25 ++ .../interceptor/MyHandShakeInterceptor.java | 58 ++++ .../esua/epdc/jwt/JwtTokenProperties.java | 41 +++ .../elink/esua/epdc/jwt/JwtTokenUtils.java | 68 +++++ .../elink/esua/epdc/utils/ModuleConstant.java | 22 ++ .../src/main/resources/application.yml | 62 +++++ .../main/resources/i18n/messages.properties | 1 + .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../resources/i18n/messages_zh_TW.properties | 1 + .../main/resources/i18n/validation.properties | 1 + .../i18n/validation_en_US.properties | 1 + .../i18n/validation_zh_CN.properties | 1 + .../i18n/validation_zh_TW.properties | 1 + .../src/main/resources/logback-spring.xml | 159 +++++++++++ pom.xml | 22 ++ 25 files changed, 1062 insertions(+) create mode 100644 .gitignore create mode 100644 epdc-cloud-websocket/Dockerfile create mode 100644 epdc-cloud-websocket/pom.xml create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/ModuleApplication.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/ModuleConfigImpl.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/WebSocketConfig.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/constants/WebSocketConstant.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/Demo2Controller.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/MenuNoticController.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/exception/ModuleErrorCode.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/interceptor/MyHandShakeInterceptor.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenProperties.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenUtils.java create mode 100644 epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/utils/ModuleConstant.java create mode 100644 epdc-cloud-websocket/src/main/resources/application.yml create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/messages.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/messages_en_US.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/messages_zh_CN.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/messages_zh_TW.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/validation.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/validation_en_US.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/validation_zh_CN.properties create mode 100644 epdc-cloud-websocket/src/main/resources/i18n/validation_zh_TW.properties create mode 100644 epdc-cloud-websocket/src/main/resources/logback-spring.xml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c73fa0e --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +.idea/ +*.iml +**/target/ diff --git a/epdc-cloud-websocket/Dockerfile b/epdc-cloud-websocket/Dockerfile new file mode 100644 index 0000000..9e1c30f --- /dev/null +++ b/epdc-cloud-websocket/Dockerfile @@ -0,0 +1,20 @@ +# 基础镜像 +FROM openjdk:8u242-jdk-buster +# 作者 +MAINTAINER rongchao@elink-cn.com +# 对应pom.xml文件中的dockerfile-maven-plugin插件JAR_FILE的值 +ARG JAR_FILE +# 对应pom.xml文件中的dockerfile-maven-plugin插件JAR_NAME的值 +ARG JAR_NAME +# 对应pom.xml文件中的dockerfile-maven-plugin插件SERVER_PORT的值 +ARG SERVER_PORT +# 复制打包完成后的jar文件到/opt目录下 +ENV JAR_PATH /mnt/epdc/${JAR_NAME}.jar +ADD ${JAR_FILE} $JAR_PATH +# /data设为环境变量 +ENV DATAPATH /data +# 挂载/data目录到主机 +VOLUME $DATAPATH +# 启动容器时执行 +ENTRYPOINT java -jar -Xmx1024m $JAR_PATH +EXPOSE ${SERVER_PORT} diff --git a/epdc-cloud-websocket/pom.xml b/epdc-cloud-websocket/pom.xml new file mode 100644 index 0000000..e3b3674 --- /dev/null +++ b/epdc-cloud-websocket/pom.xml @@ -0,0 +1,254 @@ + + + 4.0.0 + + + com.esua.epdc.yushan + epdc-cloud-parent-yushan + 1.0.0 + ../epdc-cloud-parent-yushan + + + epdc-cloud-websocket + jar + 榆山党群e事通websocket + + + + + org.springframework.boot + spring-boot-starter-websocket + + + + com.fasterxml.jackson.core + jackson-databind + 2.10.0 + + + + + com.fasterxml.jackson.core + jackson-core + 2.10.0 + + + + + com.fasterxml.jackson.core + jackson-annotations + 2.10.0 + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + + io.github.openfeign + feign-httpclient + + + + io.jsonwebtoken + jjwt + 0.7.0 + + + + + com.esua.epdc.yushan + epdc-cloud-websocket-client + ${epdc-cloud-client.version} + + + + com.esua.epdc.yushan + epdc-cloud-user-client + ${epdc-cloud-client.version} + + + + + + com.esua.epdc.yushan + epdc-commons-tools + ${epdc-cloud-commons.version} + + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + com.spotify + dockerfile-maven-plugin + + + + ${project.basedir}/src/main/java + + + + true + ${basedir}/src/main/resources + + **/application*.yml + **/*.properties + logback-spring.xml + registry.conf + + + + ${basedir}/src/main/resources + + **/application*.yml + **/*.properties + logback-spring.xml + registry.conf + + + + + + + + dev + + true + + + dev + dev + + 9988 + + 2 + 47.104.224.45 + 6379 + elink@888 + + + + + epdc + elink833066 + + false + 47.104.224.45:8848 + + 6a3577b4-7b79-43f6-aebb-9c3f31263f6a + + + wx3ef8f2cd12a19fcb + 948aa2f21dbaa3943288ea5b119ac6f2 + 111 + 111 + + wxdd8530c5f4926766 + 5bf4fb813145431b3493a10aa7e041e9 + + + + + test + + test + test + + 10016 + + 2 + 114.215.125.123 + 9603 + epdc!redis@master1405 + + + + + epdc + elink833066 + + true + 47.104.224.45:8848 + 47.104.85.99 + 6a3577b4-7b79-43f6-aebb-9c3f31263f6a + + + wx3ef8f2cd12a19fcb + 948aa2f21dbaa3943288ea5b119ac6f2 + 111 + 111 + + + + + wx5d3e97461d248397 + bfed51b731e53db9affb9e6131e7ae12 + + + + + prod + + prod + prod + + 9988 + + + 0 + 172.16.0.54 + 6379 + Elink833066 + + + + + epdc + Elink@833066 + + true + 172.16.0.52:8848 + + + + wx3ef8f2cd12a19fcb + 948aa2f21dbaa3943288ea5b119ac6f2 + 111 + 111 + + wxdd8530c5f4926766 + 5bf4fb813145431b3493a10aa7e041e9 + + + + + + + + diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/ModuleApplication.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/ModuleApplication.java new file mode 100644 index 0000000..1064023 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/ModuleApplication.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * 模块 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@SpringBootApplication +@EnableDiscoveryClient +@EnableFeignClients +public class ModuleApplication { + + public static void main(String[] args) { + SpringApplication.run(ModuleApplication.class, args); + } + +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/ModuleConfigImpl.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/ModuleConfigImpl.java new file mode 100644 index 0000000..1dc3b48 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/ModuleConfigImpl.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + *

+ * https://www.renren.io + *

+ * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.config; + +import com.elink.esua.epdc.commons.tools.config.ModuleConfig; +import org.springframework.stereotype.Service; + +/** + * 模块配置信息 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Service +public class ModuleConfigImpl implements ModuleConfig { + + @Override + public String getName() { + return "epdc-websocket-server"; + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/WebSocketConfig.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/WebSocketConfig.java new file mode 100644 index 0000000..8243009 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/config/WebSocketConfig.java @@ -0,0 +1,126 @@ +package com.elink.esua.epdc.config; + +import com.elink.esua.epdc.constants.WebSocketConstant; +import com.elink.esua.epdc.jwt.JwtTokenUtils; +import io.jsonwebtoken.Claims; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.converter.MessageConverter; +import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; +import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler; +import org.springframework.messaging.simp.config.ChannelRegistration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.messaging.simp.stomp.StompCommand; +import org.springframework.messaging.simp.stomp.StompHeaderAccessor; +import org.springframework.messaging.support.ChannelInterceptor; +import org.springframework.messaging.support.MessageHeaderAccessor; +import org.springframework.web.socket.config.annotation.*; + +import java.security.Principal; +import java.util.List; + +/** + * 启用STOMP协议来传输基于代理(message broker)的消息 + * + * @author rongchao + * @Date 19-10-25 + */ +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport implements WebSocketMessageBrokerConfigurer { + + @Autowired + private JwtTokenUtils jwtUtil; + + @Override + public void configureWebSocketTransport(WebSocketTransportRegistration registry) { + super.configureWebSocketTransport(registry); + } + + @Override + public boolean configureMessageConverters(List messageConverters) { + return super.configureMessageConverters(messageConverters); + } + + @Override + public void configureClientInboundChannel(ChannelRegistration registration) { + registration.interceptors(new ChannelInterceptor() { + + @Override + public Message preSend(Message message, MessageChannel channel) { + + StompHeaderAccessor accessor = + MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); + + if (StompCommand.CONNECT.equals(accessor.getCommand())) { + String jwtToken = accessor.getFirstNativeHeader("token"); + if (StringUtils.isNotEmpty(jwtToken)) { + Claims claims = jwtUtil.getClaimByToken(jwtToken); + String userId = claims.getSubject(); + accessor.setUser(new FastPrincipal(userId)); + } + } + + return message; + } + }); + } + + @Override + public void configureClientOutboundChannel(ChannelRegistration registration) { + super.configureClientOutboundChannel(registration); + } + + + @Override + public void addArgumentResolvers(List argumentResolvers) { + super.addArgumentResolvers(argumentResolvers); + } + + @Override + public void addReturnValueHandlers(List returnValueHandlers) { + super.addReturnValueHandlers(returnValueHandlers); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint(WebSocketConstant.MENU_NOTICE_ENDPOINT) + // 添加允许跨域访问 + .setAllowedOrigins("*") + .withSockJS(); + + } + + @Override + public void configureMessageBroker(MessageBrokerRegistry registry) { + //客户端发送消息的请求前缀 + registry.setApplicationDestinationPrefixes(WebSocketConstant.APPLICATION_DESTINATION_PREFIXES); + //客户端订阅消息的请求前缀,topic一般用于广播推送,queue用于点对点推送 + registry.enableSimpleBroker(WebSocketConstant.MENU_NOTICE_TOPIC, WebSocketConstant.MENU_NOTICE_QUEUE); + //服务端通知客户端的前缀,可以不设置,默认为user + registry.setUserDestinationPrefix(WebSocketConstant.USER_DESTINATION_PREFIX); + } + + /** + * 定义一个自己的权限验证类 + * + * @author rongchao + * @Date 19-10-27 + */ + class FastPrincipal implements Principal { + + private final String name; + + public FastPrincipal(String name) { + this.name = name; + } + + @Override + public String getName() { + return name; + } + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/constants/WebSocketConstant.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/constants/WebSocketConstant.java new file mode 100644 index 0000000..fb4ab21 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/constants/WebSocketConstant.java @@ -0,0 +1,20 @@ +package com.elink.esua.epdc.constants; + +/** + * @author rongchao + * @Description: websocket常量类 + * @date 18-9-14 + */ +public class WebSocketConstant { + + public final static String MENU_NOTICE_ENDPOINT = "/menuNoticeEndpoint"; + + public final static String APPLICATION_DESTINATION_PREFIXES = "/notice"; + + public final static String MENU_NOTICE_TOPIC = "/menuNoticeTopic"; + + public final static String MENU_NOTICE_QUEUE = "/menuNoticeQueue"; + + public final static String USER_DESTINATION_PREFIX = "/userMenuNotice/"; + +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/Demo2Controller.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/Demo2Controller.java new file mode 100644 index 0000000..de52d81 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/Demo2Controller.java @@ -0,0 +1,47 @@ +package com.elink.esua.epdc.controller; + +import com.elink.esua.epdc.constants.WebSocketConstant; +import com.elink.esua.epdc.dto.EventMenuNoticeDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.simp.SimpMessageSendingOperations; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author rongchao + * @Date 19-10-28 + */ +@RestController +public class Demo2Controller { + + @Autowired + private SimpMessageSendingOperations template; + + + /** + * @param message + * @return + * @throws Exception + */ + @GetMapping("/sendHello2") + public String say2(@RequestBody EventMenuNoticeDto message) { + template.convertAndSendToUser( + "1067246875800000001", + WebSocketConstant.MENU_NOTICE_QUEUE.concat("/menu/getResponse"), + message + ); + return "调用成功"; + } + + /** + * @param message + * @return + * @throws Exception + */ + @GetMapping("/sendHello") + public EventMenuNoticeDto say(@RequestBody EventMenuNoticeDto message) throws Exception { + template.convertAndSend(WebSocketConstant.MENU_NOTICE_TOPIC.concat("/getResponse"), message); + return message; + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/MenuNoticController.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/MenuNoticController.java new file mode 100644 index 0000000..5df9fc3 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/controller/MenuNoticController.java @@ -0,0 +1,44 @@ +package com.elink.esua.epdc.controller; + +import com.elink.esua.epdc.constants.WebSocketConstant; +import com.elink.esua.epdc.dto.EventMenuNoticeDto; +import com.google.common.collect.Maps; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.simp.SimpMessageSendingOperations; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * @author rongchao + * @Date 19-10-28 + */ +@RequestMapping("eventMenu") +@RestController +public class MenuNoticController { + + @Autowired + private SimpMessageSendingOperations template; + + + /** + * @param message + * @return + * @throws Exception + */ + @GetMapping("notice") + public String eventMenuNotice(@RequestBody EventMenuNoticeDto message) { + Map map = Maps.newHashMap(); + map.put("menuCode", message.getMenuCode()); + map.put("num", message.getNum()); + template.convertAndSendToUser( + message.getUserId(), + WebSocketConstant.MENU_NOTICE_QUEUE.concat("/menu/getResponse"), + map + ); + return "调用成功"; + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/exception/ModuleErrorCode.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/exception/ModuleErrorCode.java new file mode 100644 index 0000000..7136174 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/exception/ModuleErrorCode.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + *

+ * https://www.renren.io + *

+ * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.exception; + + +import com.elink.esua.epdc.commons.tools.exception.ErrorCode; + +/** + * 模块错误编码,由9位数字组成,前6位为模块编码,后3位为业务编码 + *

+ * 如:100001001(100001代表模块,001代表业务代码) + *

+ * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +public interface ModuleErrorCode extends ErrorCode { + +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/interceptor/MyHandShakeInterceptor.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/interceptor/MyHandShakeInterceptor.java new file mode 100644 index 0000000..cdaf083 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/interceptor/MyHandShakeInterceptor.java @@ -0,0 +1,58 @@ +package com.elink.esua.epdc.interceptor; + +import com.elink.esua.epdc.commons.tools.constant.Constant; +import com.elink.esua.epdc.jwt.JwtTokenUtils; +import io.jsonwebtoken.Claims; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.messaging.simp.stomp.StompHeaderAccessor; +import org.springframework.messaging.support.MessageHeaderAccessor; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.HandshakeInterceptor; + +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + +/** + * 拦截器 + * + * @author rongchao + * @Date 19-10-25 + */ +@Component +public class MyHandShakeInterceptor implements HandshakeInterceptor { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private JwtTokenUtils jwtUtil; + + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, + Map attributes) throws Exception { + HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); + + //获取用户token + String token = servletRequest.getHeader(Constant.TOKEN_HEADER); + if(StringUtils.isEmpty(token)) { + return true; + } + Claims claims = jwtUtil.getClaimByToken(token); + String userId = claims.getSubject(); + attributes.put(Constant.USER_KEY, userId); + return true; + } + + @Override + public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) { + + // System.out.println(this.getClass().getCanonicalName() + "握手成功后..."); + } + +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenProperties.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenProperties.java new file mode 100644 index 0000000..3e938f9 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenProperties.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.jwt; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * Jwt + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Configuration +@ConfigurationProperties(prefix = "jwt.token") +public class JwtTokenProperties { + private String secret; + private int expire; + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + public int getExpire() { + return expire; + } + + public void setExpire(int expire) { + this.expire = expire; + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenUtils.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenUtils.java new file mode 100644 index 0000000..736be6d --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/jwt/JwtTokenUtils.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + *

+ * https://www.renren.io + *

+ * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.jwt; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import org.joda.time.DateTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * Jwt工具类 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +@Component +public class JwtTokenUtils { + private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtils.class); + + @Autowired + private JwtTokenProperties jwtProperties; + + /** + * 生成jwt token + */ + public String generateToken(String userId) { + return Jwts.builder() + .setHeaderParam("typ", "JWT") + .setSubject(userId) + .setIssuedAt(new Date()) + .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) + .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) + .compact(); + } + + public Claims getClaimByToken(String token) { + try { + return Jwts.parser() + .setSigningKey(jwtProperties.getSecret()) + .parseClaimsJws(token) + .getBody(); + } catch (Exception e) { + logger.debug("validate is token error, token = " + token, e); + return null; + } + } + + /** + * token是否过期 + * + * @return true:过期 + */ + public boolean isTokenExpired(Date expiration) { + return expiration.before(new Date()); + } +} diff --git a/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/utils/ModuleConstant.java b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/utils/ModuleConstant.java new file mode 100644 index 0000000..29f31d5 --- /dev/null +++ b/epdc-cloud-websocket/src/main/java/com/elink/esua/epdc/utils/ModuleConstant.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + *

+ * https://www.renren.io + *

+ * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.utils; + + +import com.elink.esua.epdc.commons.tools.constant.Constant; + +/** + * 模块常量 + * + * @author Mark sunlightcs@gmail.com + * @since 1.1.0 + */ +public interface ModuleConstant extends Constant { + +} diff --git a/epdc-cloud-websocket/src/main/resources/application.yml b/epdc-cloud-websocket/src/main/resources/application.yml new file mode 100644 index 0000000..1898766 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/application.yml @@ -0,0 +1,62 @@ +server: + port: @server.port@ + servlet: + context-path: /ws + +spring: + application: + name: epdc-websocket-server + # 环境 dev|test|prod + profiles: + active: @spring.profiles.active@ + messages: + encoding: UTF-8 + basename: i18n/messages,i18n/messages_common + jackson: + time-zone: GMT+8 + date-format: yyyy-MM-dd HH:mm:ss + redis: + database: @spring.redis.index@ + host: @spring.redis.host@ + timeout: 30s + port: @spring.redis.port@ + password: @spring.redis.password@ + cloud: + nacos: + discovery: + server-addr: @nacos.server-addr@ + register-enabled: @nacos.register-enabled@ + ip: @nacos.ip@ + namespace: @nacos.namespace@ +feign: + hystrix: + enabled: true + httpclient: + enabled: true + +hystrix: + command: + default: + execution: + isolation: + thread: + timeoutInMilliseconds: 60000 #缺省为1000 + +ribbon: + ReadTimeout: 300000 + ConnectTimeout: 300000 + +management: + endpoints: + web: + exposure: + include: "*" + endpoint: + health: + show-details: ALWAYS +jwt: + token: + #秘钥 + secret: 7016867071f0ebf1c46f123eaaf4b9d6[elink.epdc] + #token有效时长,默认7天,单位秒 + expire: 604800 diff --git a/epdc-cloud-websocket/src/main/resources/i18n/messages.properties b/epdc-cloud-websocket/src/main/resources/i18n/messages.properties new file mode 100644 index 0000000..a7091db --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/messages.properties @@ -0,0 +1 @@ +#Default diff --git a/epdc-cloud-websocket/src/main/resources/i18n/messages_en_US.properties b/epdc-cloud-websocket/src/main/resources/i18n/messages_en_US.properties new file mode 100644 index 0000000..9e895e4 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/messages_en_US.properties @@ -0,0 +1 @@ +#English diff --git a/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_CN.properties b/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 0000000..b21fd22 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1 @@ +#\u7B80\u4F53\u4E2D\u6587 diff --git a/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_TW.properties b/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_TW.properties new file mode 100644 index 0000000..4433dba --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/messages_zh_TW.properties @@ -0,0 +1 @@ +#\u7E41\u4F53\u4E2D\u6587 diff --git a/epdc-cloud-websocket/src/main/resources/i18n/validation.properties b/epdc-cloud-websocket/src/main/resources/i18n/validation.properties new file mode 100644 index 0000000..56ca909 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/validation.properties @@ -0,0 +1 @@ +#Default \ No newline at end of file diff --git a/epdc-cloud-websocket/src/main/resources/i18n/validation_en_US.properties b/epdc-cloud-websocket/src/main/resources/i18n/validation_en_US.properties new file mode 100644 index 0000000..9e895e4 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/validation_en_US.properties @@ -0,0 +1 @@ +#English diff --git a/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_CN.properties b/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_CN.properties new file mode 100644 index 0000000..b21fd22 --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_CN.properties @@ -0,0 +1 @@ +#\u7B80\u4F53\u4E2D\u6587 diff --git a/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_TW.properties b/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_TW.properties new file mode 100644 index 0000000..4433dba --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/i18n/validation_zh_TW.properties @@ -0,0 +1 @@ +#\u7E41\u4F53\u4E2D\u6587 diff --git a/epdc-cloud-websocket/src/main/resources/logback-spring.xml b/epdc-cloud-websocket/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..255556f --- /dev/null +++ b/epdc-cloud-websocket/src/main/resources/logback-spring.xml @@ -0,0 +1,159 @@ + + + + + + + + + + + + + + debug + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + ${log.path}/debug.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${log.path}/debug-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + debug + ACCEPT + DENY + + + + + + + ${log.path}/info.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + + ${log.path}/info-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + info + ACCEPT + DENY + + + + + + + ${log.path}/warn.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${log.path}/warn-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + warn + ACCEPT + DENY + + + + + + + ${log.path}/error.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + UTF-8 + + + + ${log.path}/error-%d{yyyy-MM-dd}.%i.log + + 100MB + + + 15 + + + + ERROR + ACCEPT + DENY + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d02984f --- /dev/null +++ b/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + com.esua.epdc.yushan + epdc-cloud-websocket-yushan + 1.0.0 + + pom + 榆山党群e事通互帮互助模块 + + + epdc-cloud-websocket + + + + + + +