Skip to content

Commit

Permalink
独立了微信验证模块
Browse files Browse the repository at this point in the history
  • Loading branch information
叶云轩.MacBookPro committed Dec 24, 2019
1 parent 7621f2d commit c932313
Show file tree
Hide file tree
Showing 46 changed files with 569 additions and 963 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@
</dependencyManagement>

<modules>
<module>wx-validate</module>
<module>yyx-wx-commons</module>
<module>yyx-wx-modules</module>
<module>yyx-wx-demo</module>
<module>yyx-wx-butt</module>
<module>yyx-wx-manage</module>
</modules>

<developers>
Expand Down
12 changes: 7 additions & 5 deletions yyx-wx-manage/.gitignore → wx-validate/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/target/
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
Expand All @@ -18,12 +21,11 @@

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### java ###
.class
.jar
### VS Code ###
.vscode/
41 changes: 41 additions & 0 deletions wx-validate/README.md
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
|--
```

40 changes: 40 additions & 0 deletions wx-validate/pom.xml
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>
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);
}
}
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;
}
}
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 {

}
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";
}
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() {
}
}
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();
}
}
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);
}
}
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();
}
}
}
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;
}
Loading

0 comments on commit c932313

Please sign in to comment.