feat: 微信公众号扫码登录
This commit is contained in:
@@ -124,6 +124,11 @@
|
||||
<artifactId>schisandra-cloud-storage-auth-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
<version>2.4.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package com.schisandra.wechat.config;
|
||||
package com.schisandra.wechat.config.job;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import org.slf4j.Logger;
|
@@ -0,0 +1,15 @@
|
||||
package com.schisandra.wechat.config.websocket;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.schisandra.wechat.config.websocket;
|
||||
|
||||
import com.alibaba.nacos.common.utils.CollectionUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.HandshakeResponse;
|
||||
import javax.websocket.server.HandshakeRequest;
|
||||
import javax.websocket.server.ServerEndpointConfig;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class WebSocketServerConfig extends ServerEndpointConfig.Configurator {
|
||||
|
||||
@Override
|
||||
public boolean checkOrigin(String originHeaderValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
|
||||
Map<String, List<String>> parameterMap = request.getParameterMap();
|
||||
List<String> erpList = parameterMap.get("clientId");
|
||||
if(!CollectionUtils.isEmpty(erpList)){
|
||||
sec.getUserProperties().put("clientId", erpList.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package com.schisandra.wechat.config;
|
||||
package com.schisandra.wechat.config.wx;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
@@ -1,4 +1,4 @@
|
||||
package com.schisandra.wechat.config;
|
||||
package com.schisandra.wechat.config.wx;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
@@ -1,4 +1,4 @@
|
||||
package com.schisandra.wechat.config;
|
||||
package com.schisandra.wechat.config.wx;
|
||||
|
||||
import lombok.Data;
|
||||
|
@@ -1,11 +1,12 @@
|
||||
package com.schisandra.wechat.controller;
|
||||
|
||||
import com.schisandra.wechat.common.entity.Result;
|
||||
import com.schisandra.wechat.config.WxConfig;
|
||||
import com.schisandra.wechat.config.wx.WxConfig;
|
||||
import com.schisandra.wechat.dto.AccessTokenResult;
|
||||
import com.schisandra.wechat.dto.MpQrCodeCreateRequest;
|
||||
import com.schisandra.wechat.dto.MpQrCodeCreateResult;
|
||||
import com.schisandra.wechat.service.WXService;
|
||||
import com.schisandra.wechat.websocket.WeChatSocket;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -14,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Classname WxController
|
||||
@@ -32,8 +32,9 @@ public class WxController {
|
||||
@Resource
|
||||
private WXService wxService;
|
||||
@Resource
|
||||
WxConfig wxConfig;
|
||||
|
||||
private WxConfig wxConfig;
|
||||
@Resource
|
||||
private WeChatSocket weChatSocket;
|
||||
/**
|
||||
* @description: 获取微信access_token
|
||||
* @param: []
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package com.schisandra.wechat.job;
|
||||
|
||||
import com.schisandra.wechat.config.WxConfig;
|
||||
import com.schisandra.wechat.config.wx.WxConfig;
|
||||
import com.schisandra.wechat.service.WXService;
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
|
@@ -5,21 +5,23 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.schisandra.wechat.aes.AesException;
|
||||
import com.schisandra.wechat.common.exception.ParameterException;
|
||||
import com.schisandra.wechat.config.WxConfig;
|
||||
import com.schisandra.wechat.common.redis.RedisUtil;
|
||||
import com.schisandra.wechat.config.wx.WxConfig;
|
||||
import com.schisandra.wechat.dto.MpBaseEventRequest;
|
||||
import com.schisandra.wechat.dto.MpCommonRequest;
|
||||
import com.schisandra.wechat.dto.MpSubscribeEventRequest;
|
||||
import com.schisandra.wechat.dto.MpTextEventRequest;
|
||||
import com.schisandra.wechat.rpc.AuthUserRpc;
|
||||
import com.schisandra.wechat.service.WxMpEventService;
|
||||
import com.schisandra.wechat.websocket.WeChatSocket;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
@@ -30,9 +32,7 @@ import java.util.Objects;
|
||||
@Slf4j
|
||||
public class WxMpEventServiceImpl implements WxMpEventService {
|
||||
@Resource
|
||||
ApplicationContext applicationContext;
|
||||
@Resource
|
||||
WxConfig wxConfig;
|
||||
private WxConfig wxConfig;
|
||||
|
||||
@Resource
|
||||
private AuthUserRpc authUserRpc;
|
||||
@@ -68,20 +68,33 @@ public class WxMpEventServiceImpl implements WxMpEventService {
|
||||
MpTextEventRequest mpTextEventRequest = mapper.convertValue(object, MpTextEventRequest.class);
|
||||
log.info(mpTextEventRequest.toString());
|
||||
log.info("推送消息:MpTextEventRequest...");
|
||||
// applicationContext.publishEvent(mpTextEventRequest);
|
||||
//...
|
||||
}
|
||||
if ("event".equals(mpBaseEventRequest.getMsgType())) {
|
||||
MpSubscribeEventRequest mpSubscribeEventRequest = mapper.convertValue(object, MpSubscribeEventRequest.class);
|
||||
log.info(mpSubscribeEventRequest.toString());
|
||||
log.info("推送消息:MpSubscribeEventRequest...");
|
||||
// applicationContext.publishEvent(mpSubscribeEventRequest);
|
||||
// ...
|
||||
if ("subscribe".equals(mpSubscribeEventRequest.getEvent())
|
||||
&& Strings.isNotBlank(mpSubscribeEventRequest.getEventKey())) {
|
||||
String[] keys = mpSubscribeEventRequest.getEventKey().split("_");
|
||||
if ("qrscene".equals(keys[0]) && "ScanReg".equals(keys[1])) {
|
||||
log.info("AppId:{},ClientId:{}", keys[2], keys[3]);
|
||||
authUserRpc.wechatRegister(keys[2], mpSubscribeEventRequest.getFromUserName(), keys[3]);
|
||||
return null;
|
||||
Boolean result = authUserRpc.wechatRegister(keys[2], mpSubscribeEventRequest.getFromUserName(), keys[3]);
|
||||
String replyContent;
|
||||
if (result){
|
||||
replyContent = "登录成功!";
|
||||
}else{
|
||||
replyContent = "登录失败!";
|
||||
}
|
||||
String content = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + mpSubscribeEventRequest.getFromUserName() + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + mpSubscribeEventRequest.getToUserName() + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + replyContent + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,9 +103,21 @@ public class WxMpEventServiceImpl implements WxMpEventService {
|
||||
String[] keys = mpSubscribeEventRequest.getEventKey().split("_");
|
||||
if ("ScanReg".equals(keys[0])) {
|
||||
log.info("AppId:{},ClientId:{}", keys[1], keys[2]);
|
||||
authUserRpc.wechatRegister(keys[1], mpSubscribeEventRequest.getFromUserName(), keys[2]);
|
||||
|
||||
return null;
|
||||
Boolean result = authUserRpc.wechatRegister(keys[1], mpSubscribeEventRequest.getFromUserName(), keys[2]);
|
||||
String replyContent;
|
||||
if (result){
|
||||
replyContent = "登录成功!";
|
||||
}else{
|
||||
replyContent = "登录失败!";
|
||||
}
|
||||
String content = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[" + mpSubscribeEventRequest.getFromUserName() + "]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[" + mpSubscribeEventRequest.getToUserName() + "]]></FromUserName>\n" +
|
||||
" <CreateTime>12345678</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[text]]></MsgType>\n" +
|
||||
" <Content><![CDATA[" + replyContent + "]]></Content>\n" +
|
||||
"</xml>";
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,139 @@
|
||||
package com.schisandra.wechat.websocket;
|
||||
|
||||
|
||||
import com.schisandra.wechat.config.websocket.WebSocketServerConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@ServerEndpoint(value = "/wx/socket", configurator = WebSocketServerConfig.class)
|
||||
@Component
|
||||
public class WeChatSocket {
|
||||
|
||||
/**
|
||||
* 记录当前在线连接数
|
||||
*/
|
||||
private static final AtomicInteger onlineCount = new AtomicInteger(0);
|
||||
|
||||
/**
|
||||
* 存放所有在线的客户端
|
||||
*/
|
||||
private static final Map<String, WeChatSocket> clients = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||
*/
|
||||
@Getter
|
||||
private Session session;
|
||||
|
||||
/**
|
||||
* clientId唯一标识
|
||||
*/
|
||||
private String clientId = "";
|
||||
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session, EndpointConfig conf) throws IOException {
|
||||
//获取用户信息
|
||||
try {
|
||||
Map<String, Object> usclientIdroperties = conf.getUserProperties();
|
||||
String clientId = (String) usclientIdroperties.get("clientId");
|
||||
this.clientId = clientId;
|
||||
this.session = session;
|
||||
if (clients.containsKey(this.clientId)) {
|
||||
clients.get(this.clientId).session.close();
|
||||
clients.remove(this.clientId);
|
||||
onlineCount.decrementAndGet();
|
||||
}
|
||||
clients.put(this.clientId, this);
|
||||
onlineCount.incrementAndGet();
|
||||
log.info("有新连接加入:{},当前在线人数为:{}", clientId, onlineCount.get());
|
||||
sendMessage("连接成功", this.session);
|
||||
} catch (Exception e) {
|
||||
log.error("建立链接错误{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭调用的方法
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose() {
|
||||
try {
|
||||
if (clients.containsKey(clientId)) {
|
||||
clients.get(clientId).session.close();
|
||||
clients.remove(clientId);
|
||||
onlineCount.decrementAndGet();
|
||||
}
|
||||
log.info("有一连接关闭:{},当前在线人数为:{}", this.clientId, onlineCount.get());
|
||||
} catch (Exception e) {
|
||||
log.error("连接关闭错误,错误原因{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message, Session session) {
|
||||
log.info("服务端收到客户端[{}]的消息:{}", this.clientId, message);
|
||||
//心跳机制
|
||||
if (message.equals("ping")) {
|
||||
this.sendMessage("pong", session);
|
||||
}
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error) {
|
||||
log.error("Socket:{},发生错误,错误原因{}", clientId, error.getMessage(), error);
|
||||
try {
|
||||
session.close();
|
||||
} catch (Exception e) {
|
||||
log.error("onError.Exception{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定发送消息
|
||||
*/
|
||||
public void sendMessage(String message, Session session) {
|
||||
log.info("服务端给客户端[{}]发送消息{}", this.clientId, message);
|
||||
try {
|
||||
session.getBasicRemote().sendText(message);
|
||||
} catch (IOException e) {
|
||||
log.error("{}发送消息发生异常,异常原因{}", this.clientId, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发消息
|
||||
*/
|
||||
public void sendMessage(String message) {
|
||||
for (Map.Entry<String, WeChatSocket> sessionEntry : clients.entrySet()) {
|
||||
String clientId = sessionEntry.getKey();
|
||||
WeChatSocket socket = sessionEntry.getValue();
|
||||
Session session = socket.session;
|
||||
log.info("服务端给客户端[{}]发送消息{}", clientId, message);
|
||||
try {
|
||||
session.getBasicRemote().sendText(message);
|
||||
} catch (IOException e) {
|
||||
log.error("{}发送消息发生异常,异常原因{}", this.clientId, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public WeChatSocket getSocket(String userName) {
|
||||
return clients.get(userName);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user