USVT-132 读取 token 后完成校验
This commit is contained in:
parent
806eb395fa
commit
03fafd8107
|
@ -1,21 +1,29 @@
|
|||
package com.northtecom.visatrack.api.controller;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.northtecom.visatrack.api.model.request.auth.WeChatVerificationRequest;
|
||||
import com.northtecom.visatrack.api.service.impl.WeChatService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* HomeController Endpoint to check service running and wechat verification response
|
||||
* HomeController Endpoint to check service running and weChat verification response
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/home")
|
||||
public class HomeController {
|
||||
private final WeChatService weChatService;
|
||||
|
||||
@Autowired
|
||||
public HomeController(WeChatService weChatService) {
|
||||
this.weChatService = weChatService;
|
||||
}
|
||||
|
||||
@GetMapping("/index")
|
||||
public String index() {
|
||||
|
@ -25,13 +33,16 @@ public class HomeController {
|
|||
|
||||
@GetMapping("/wechat/verification")
|
||||
@Operation(summary = "Wechat Verification API", description = "Make sure the calling to this API come from WeChat")
|
||||
public String weChatVerification(WeChatVerificationRequest weChatVerificationRequest) throws JsonProcessingException {
|
||||
public String weChatVerification(WeChatVerificationRequest weChatVerificationRequest) {
|
||||
log.debug("Doing WeChat Verification");
|
||||
log.debug("signature - [{}]", weChatVerificationRequest.getSignature());
|
||||
log.debug("timestamp - [{}]", weChatVerificationRequest.getTimestamp());
|
||||
log.debug("nonce - [{}]", weChatVerificationRequest.getNonce());
|
||||
|
||||
return weChatVerificationRequest.getEchostr();
|
||||
if (weChatService.verificationWeChat(weChatVerificationRequest.getSignature(), weChatVerificationRequest.getTimestamp(), weChatVerificationRequest.getNonce()))
|
||||
return weChatVerificationRequest.getEchostr();
|
||||
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ import java.util.UUID;
|
|||
public class WeChatVerificationRequest {
|
||||
|
||||
private String signature;
|
||||
private Long timestamp;
|
||||
private String timestamp;
|
||||
private String nonce;
|
||||
private String echostr;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
package com.northtecom.visatrack.api.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.northtecom.visatrack.api.data.entity.VisaCase;
|
||||
import com.northtecom.visatrack.api.data.entity.WeChatCallState;
|
||||
import com.northtecom.visatrack.api.data.repository.*;
|
||||
import com.northtecom.visatrack.api.model.entity.wechat.WeChatAccessToken;
|
||||
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser;
|
||||
import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest;
|
||||
import com.northtecom.visatrack.api.util.WeChatUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -28,6 +27,8 @@ import java.util.Optional;
|
|||
@Service
|
||||
@Slf4j
|
||||
public class WeChatService {
|
||||
|
||||
private final WeChatUtils weChatUtils;
|
||||
private final VisaCaseRepository visaCaseRepository;
|
||||
|
||||
private final WeChatCallStateRepository weChatCallStateRepository;
|
||||
|
@ -39,7 +40,13 @@ public class WeChatService {
|
|||
|
||||
|
||||
@Autowired
|
||||
public WeChatService(VisaCaseRepository visaCaseRepository, WeChatCallStateRepository weChatCallStateRepository, UserRepository userRepository, CrawlService crawlService, VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository, ObjectMapper objectMapper) {
|
||||
public WeChatService(WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository,
|
||||
WeChatCallStateRepository weChatCallStateRepository,
|
||||
UserRepository userRepository,
|
||||
CrawlService crawlService,
|
||||
VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository,
|
||||
ObjectMapper objectMapper) {
|
||||
this.weChatUtils = weChatUtils;
|
||||
this.visaCaseRepository = visaCaseRepository;
|
||||
this.weChatCallStateRepository = weChatCallStateRepository;
|
||||
this.userRepository = userRepository;
|
||||
|
@ -91,6 +98,12 @@ public class WeChatService {
|
|||
return weChatCallStateRepository.findByWeChatStateEqualsAndWeChatCodeEquals(weChatState, weChatCode);
|
||||
}
|
||||
|
||||
public Boolean verificationWeChat(String signature, String timestamp, String nonce) {
|
||||
if (StringUtils.equals(signature, weChatUtils.getWechatVerificationSignature(timestamp, nonce))) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
private String callWeChatAccessTokenAPI(OkHttpClient client, String weChatCode, String weChatState) throws IOException {
|
||||
String responseStr;
|
||||
|
|
|
@ -48,7 +48,19 @@ public class AwsUtils {
|
|||
}
|
||||
|
||||
public Map<String, String> getDiscourseConfigValueFromAWS(Map<String, String> discourseApiConfMap) {
|
||||
return queryAwsKVMap(discourseApiConfMap, DiscourseUtils.DISCOURSE_PATH);
|
||||
}
|
||||
|
||||
public Map<String, String> getWechatParameterConfValueFromAWS(Map<String, String> wechatParameterConfMap) {
|
||||
return queryAwsKVMap(wechatParameterConfMap, WeChatUtils.WECHAT_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parameterConfMap
|
||||
* @param parameterStorePath
|
||||
* @return
|
||||
*/
|
||||
private Map<String, String> queryAwsKVMap(Map<String, String> parameterConfMap, String parameterStorePath) {
|
||||
Map<String, Parameter> awsKVMap = new HashMap<>();
|
||||
|
||||
try {
|
||||
|
@ -56,20 +68,20 @@ public class AwsUtils {
|
|||
GetParametersByPathRequest request = new GetParametersByPathRequest();
|
||||
request.setWithDecryption(false);
|
||||
request.setRecursive(true);
|
||||
request.setPath(DiscourseUtils.DISCOURSE_PATH);
|
||||
request.setPath(parameterStorePath);
|
||||
|
||||
GetParametersByPathResult result = this.ssmClient.getParametersByPath(request);
|
||||
awsKVMap = Maps.uniqueIndex(result.getParameters(), Parameter::getName);
|
||||
|
||||
} catch (Exception ex) {
|
||||
log.error("Get AWS Value for Key - [{}] Error", DiscourseUtils.DISCOURSE_PATH, ex);
|
||||
log.error("Get AWS Value for Key - [{}] Error", parameterStorePath, ex);
|
||||
}
|
||||
|
||||
// Update the map
|
||||
for (Map.Entry<String, String> entry : discourseApiConfMap.entrySet()) {
|
||||
discourseApiConfMap.put(entry.getKey(), awsKVMap.get(DiscourseUtils.DISCOURSE_PATH + "/" + entry.getKey()).getValue());
|
||||
for (Map.Entry<String, String> entry : parameterConfMap.entrySet()) {
|
||||
parameterConfMap.put(entry.getKey(), awsKVMap.get(parameterStorePath + "/" + entry.getKey()).getValue());
|
||||
}
|
||||
|
||||
return discourseApiConfMap;
|
||||
return parameterConfMap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,13 @@ package com.northtecom.visatrack.api.util;
|
|||
import com.northtecom.visatrack.api.data.entity.WeChatCallState;
|
||||
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Utilities for Email sending
|
||||
|
@ -15,9 +22,35 @@ import lombok.extern.slf4j.Slf4j;
|
|||
* @author YuCheng Hu
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WeChatUtils {
|
||||
|
||||
|
||||
public static final String WECHAT_PATH = "/wechat";
|
||||
public static final String WECHAT_API_KEY = "appid";
|
||||
public static final String WECHAT_API_USERNAME = "secret";
|
||||
|
||||
public static final String WECHAT_VERIFICATION_TOKEN = "verification_token";
|
||||
|
||||
private Map<String, String> wechatParameterConfMap = new HashMap<String, String>();
|
||||
|
||||
@Autowired
|
||||
public WeChatUtils(AwsUtils awsUtils) {
|
||||
wechatParameterConfMap.put(WECHAT_API_KEY, StringUtils.EMPTY);
|
||||
wechatParameterConfMap.put(WECHAT_API_USERNAME, StringUtils.EMPTY);
|
||||
|
||||
wechatParameterConfMap = awsUtils.getWechatParameterConfValueFromAWS(wechatParameterConfMap);
|
||||
|
||||
}
|
||||
|
||||
public String getWechatVerificationSignature(String timestamp, String nonce) {
|
||||
StringBuffer strToSHA1 = new StringBuffer();
|
||||
strToSHA1.append(wechatParameterConfMap.get(WECHAT_VERIFICATION_TOKEN));
|
||||
strToSHA1.append(timestamp);
|
||||
strToSHA1.append(nonce);
|
||||
|
||||
return DigestUtils.sha1Hex(strToSHA1.toString());
|
||||
}
|
||||
/**
|
||||
* Send Test Email to check config and email sending API
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue