-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
叶云轩.MacBookPro
committed
Dec 24, 2019
1 parent
7621f2d
commit c932313
Showing
46 changed files
with
569 additions
and
963 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 微信公众号对接验证模块 | ||
|
||
> 此模块主要用来验证微信公众号开发配置中 GET 请求验证。 | ||
## 使用方法 | ||
|
||
### 单独使用 | ||
|
||
- 在 pom 中引入 jar 包 | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.cjwy.wxframework</groupId> | ||
<artifactId>validate</artifactId> | ||
<version>自己选择</version> | ||
</dependency> | ||
``` | ||
|
||
- 在项目启动类添加注解 | ||
|
||
```java | ||
@ComponentScans({ | ||
@ComponentScan("com.cjwy.wxframework.validate") | ||
}) | ||
``` | ||
|
||
- 配置微信公众号开发信息,在项目的 application.yml 中配置以下内容 | ||
|
||
```yml | ||
wx: | ||
validate: | ||
token: 微信公众号后台->开发->基本配置->服务器配置->token | ||
``` | ||
## 目录结构 | ||
```txt | ||
--com.cjwy.wxframework.validate | ||
|-- | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.yyx.wx</groupId> | ||
<artifactId>yyx-wx</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<groupId>com.cjwy.wxframework</groupId> | ||
<artifactId>validate</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>validate</name> | ||
<description>wx public number validate</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
25 changes: 25 additions & 0 deletions
25
wx-validate/src/main/java/com/cjwy/wxframework/validate/ValidateApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.cjwy.wxframework.validate; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.ComponentScans; | ||
|
||
|
||
/** | ||
* 微信公众号对接时验证模块 | ||
* | ||
* @author 叶云轩 at [email protected] | ||
*/ | ||
@SpringBootApplication | ||
public class ValidateApplication { | ||
|
||
/** | ||
* 启动方法 | ||
* | ||
* @param args 启动参数 | ||
*/ | ||
public static void main(String[] args) { | ||
SpringApplication.run(ValidateApplication.class, args); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
wx-validate/src/main/java/com/cjwy/wxframework/validate/controller/WxValidateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cjwy.wxframework.validate.controller; | ||
|
||
import com.cjwy.wxframework.validate.domain.exception.WxValidateException; | ||
import com.cjwy.wxframework.validate.domain.factory.ExceptionFactory; | ||
import com.cjwy.wxframework.validate.domain.properties.BasePublicNumberProperties; | ||
import com.cjwy.wxframework.validate.utils.UtilValidateWeChat; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER; | ||
|
||
/** | ||
* 微信服务器验证控制器 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/25 7:00 上午 | ||
*/ | ||
@Slf4j | ||
@RestController | ||
public class WxValidateController { | ||
@Resource | ||
private BasePublicNumberProperties basePublicNumberProperties; | ||
|
||
/** | ||
* 验证消息来自微信服务器方法 | ||
* | ||
* @param signature 微信加密签名 | ||
* @param timestamp 时间戳 | ||
* @param nonce 随机数 | ||
* @param echostr 随机字符串 | ||
* @return 验证结果 | ||
*/ | ||
@GetMapping("wx/validate") | ||
public String accessGet(@RequestParam String signature, | ||
@RequestParam String timestamp, | ||
@RequestParam String nonce, | ||
@RequestParam String echostr) { | ||
String result = "wrong"; | ||
String token = basePublicNumberProperties.getToken(); | ||
if (StringUtils.isEmpty(token)) { | ||
throw ExceptionFactory.createValidateException(WxValidateException.class, "请检查 Token 是否配置正确"); | ||
} | ||
log.info("入参[signature]={},[timestamp]={},[nonce]={},[echostr]={}", signature, timestamp, nonce, echostr); | ||
boolean validate = UtilValidateWeChat.validate(signature, timestamp, nonce, token); | ||
if (validate) { | ||
LOGGER.info("验证成功..."); | ||
result = echostr; | ||
} else { | ||
log.info("验证失败..."); | ||
} | ||
return result; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
wx-validate/src/main/java/com/cjwy/wxframework/validate/domain/config/PropertiesConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.cjwy.wxframework.validate.domain.config; | ||
|
||
import com.cjwy.wxframework.validate.domain.properties.BasePublicNumberProperties; | ||
import lombok.Data; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* 用于配置系统参数配置项的配置类 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/20 10:57 上午 | ||
*/ | ||
@Data | ||
@Configuration | ||
@EnableConfigurationProperties(BasePublicNumberProperties.class) | ||
public class PropertiesConfig { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
wx-validate/src/main/java/com/cjwy/wxframework/validate/domain/constant/EncryptConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cjwy.wxframework.validate.domain.constant; | ||
|
||
/** | ||
* 加密常量 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2018/9/13-09:53 | ||
*/ | ||
public class EncryptConstant { | ||
/** | ||
* SHA-1加密方式 | ||
*/ | ||
public static final String SHA_1 = "SHA-1"; | ||
} |
18 changes: 18 additions & 0 deletions
18
...src/main/java/com/cjwy/wxframework/validate/domain/constant/PropertiesPrefixConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.cjwy.wxframework.validate.domain.constant; | ||
|
||
/** | ||
* 配置名常量 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/20 10:54 上午 | ||
*/ | ||
public class PropertiesPrefixConstant { | ||
/** | ||
* 配置 YML 文件中微信公众号开发配置项的 KEY 值 | ||
*/ | ||
public static final String WX_PUBLIC_NUMBER_YML_KEY = "wx.validate"; | ||
|
||
private PropertiesPrefixConstant() { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...idate/src/main/java/com/cjwy/wxframework/validate/domain/exception/ValidateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.cjwy.wxframework.validate.domain.exception; | ||
|
||
/** | ||
* 验证异常 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/25 7:08 上午 | ||
*/ | ||
public class ValidateException extends RuntimeException { | ||
|
||
public ValidateException(String message) { | ||
super(message); | ||
} | ||
|
||
public ValidateException() { | ||
super(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ate/src/main/java/com/cjwy/wxframework/validate/domain/exception/WxValidateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cjwy.wxframework.validate.domain.exception; | ||
|
||
/** | ||
* 微信验证异常 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/25 7:09 上午 | ||
*/ | ||
public class WxValidateException extends ValidateException { | ||
|
||
public WxValidateException(String message) { | ||
super(message); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
wx-validate/src/main/java/com/cjwy/wxframework/validate/domain/factory/ExceptionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.cjwy.wxframework.validate.domain.factory; | ||
|
||
import com.cjwy.wxframework.validate.domain.exception.ValidateException; | ||
import com.cjwy.wxframework.validate.domain.exception.WxValidateException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
/** | ||
* 异常工厂 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/25 7:10 上午 | ||
*/ | ||
@Slf4j | ||
public class ExceptionFactory<T> { | ||
|
||
private ExceptionFactory() { | ||
} | ||
|
||
/** | ||
* 验证异常 | ||
* | ||
* @param clazz 异常的类型 | ||
* @param msg 异常信息 | ||
* @param <T> 泛型 | ||
* @return 异常 | ||
*/ | ||
public static <T> ValidateException createValidateException(Class<T> clazz, String msg) { | ||
try { | ||
T t = clazz.getDeclaredConstructor(String.class).newInstance(msg); | ||
if (t instanceof WxValidateException) { | ||
return (WxValidateException) t; | ||
} else { | ||
return (ValidateException) t; | ||
} | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
return new ValidateException(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...main/java/com/cjwy/wxframework/validate/domain/properties/BasePublicNumberProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.cjwy.wxframework.validate.domain.properties; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import java.util.List; | ||
|
||
import static com.cjwy.wxframework.validate.domain.constant.PropertiesPrefixConstant.WX_PUBLIC_NUMBER_YML_KEY; | ||
|
||
/** | ||
* 基础的公众号配置属性获取 | ||
* <p> | ||
* | ||
* @author 叶云轩 at [email protected] | ||
* @date 2019/12/20 10:35 上午 | ||
*/ | ||
@Data | ||
@ConfigurationProperties(prefix = WX_PUBLIC_NUMBER_YML_KEY) | ||
public class BasePublicNumberProperties { | ||
/** | ||
* 公众号开发信息中的 appId 字段 | ||
*/ | ||
private String appId; | ||
/** | ||
* 公众号开发信息中的 appSecret 字段 | ||
*/ | ||
private String appSecret; | ||
/** | ||
* 服务器配置中的 token 字段 | ||
*/ | ||
private String token; | ||
/** | ||
* 消息加密 默认为明文模式 即 messageDecrypt=false | ||
*/ | ||
private boolean messageDecrypt = false; | ||
/** | ||
* 消息加解密密钥 | ||
* 如果 messageDecrypt = true 此值不能为空 | ||
* 允许填写两个密钥 | ||
*/ | ||
private List<String> encodingAESKey; | ||
} |
Oops, something went wrong.