diff --git a/README.md b/README.md index a9d43d185b62d5365fc4f0a9464e7e84d69d5787..ebc6a6e8c76c77288ac709ace96f86005eb801f8 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ dream: appSecret: 11ed9e2b8e3e3c131e7be320a42b2b5a token: 123456 wxa-config: - appid: wx4f53594f9a6b3dcb - appSecret: eec6482ba3804df05bd10895bace0579 + app-id: wx4f53594f9a6b3dcb + app-secret: eec6482ba3804df05bd10895bace0579 ``` - cache使用spring的cache,需要`@EnableCaching`开启。 diff --git a/spring-boot-starter-weixin/build.gradle b/spring-boot-starter-weixin/build.gradle index 6c98ac99b4904df78162cf9229b25274fb9debd4..6177ffc4d522f05f14cf07c36f499b30f5ed790e 100644 --- a/spring-boot-starter-weixin/build.gradle +++ b/spring-boot-starter-weixin/build.gradle @@ -12,15 +12,15 @@ apply plugin: 'maven' apply plugin: 'signing' apply plugin: 'propdeps' apply plugin: 'propdeps-maven' +apply plugin: 'maven-publish' apply plugin: 'io.spring.dependency-management' ext { javaVersion = JavaVersion.VERSION_1_8 - springVersion = "4.3.14.RELEASE" - springBootVersion = "1.5.10.RELEASE" - jfinalVersion = "3.3" - jfinalWeixinVersion = "1.9" - junitVersion = "4.12" + springVersion = "4.3.18.RELEASE" + springBootVersion = "1.5.15.RELEASE" + jfinalVersion = "3.4" + jfinalWeixinVersion = "2.1" lombokVersion = '1.16.20' } @@ -39,7 +39,7 @@ dependencies { provided "org.springframework.boot:spring-boot-starter-aop" optional "org.springframework.boot:spring-boot-configuration-processor" provided "org.projectlombok:lombok:${lombokVersion}" - testCompile "junit:junit:${junitVersion}" + testCompile "org.springframework.boot:spring-boot-starter-test" } dependencyManagement { @@ -53,6 +53,21 @@ task sourcesJar(type: Jar, dependsOn: classes) { from sourceSets.main.allSource } +jar { + into("META-INF/maven/$project.group/$project.name") { + from { generatePomFileForMavenJavaPublication } + rename ".*", "pom.xml" + } +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } +} + javadoc { options { encoding "UTF-8" diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/aspect/WxApiAspect.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/aspect/WxApiAspect.java index 90ed0cdd6a8b71c71ca394e57ba9a985026f641d..a6a56d561a3d4e05dcb87ff0630ad1b51130ecbb 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/aspect/WxApiAspect.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/aspect/WxApiAspect.java @@ -1,7 +1,7 @@ package net.dreamlu.weixin.aspect; import com.jfinal.weixin.sdk.api.ApiConfigKit; -import lombok.extern.slf4j.Slf4j; +import lombok.AllArgsConstructor; import net.dreamlu.weixin.annotation.WxApi; import net.dreamlu.weixin.properties.DreamWeixinProperties; import org.aspectj.lang.ProceedingJoinPoint; @@ -15,15 +15,10 @@ import javax.servlet.http.HttpServletRequest; @Aspect @Order -@Slf4j +@AllArgsConstructor public class WxApiAspect { - private final DreamWeixinProperties weixinProperties; - public WxApiAspect(DreamWeixinProperties weixinProperties) { - this.weixinProperties = weixinProperties; - } - @Around("@annotation(wxApi)") public Object aroundWxApi(ProceedingJoinPoint point, WxApi wxApi) throws Throwable { // 目前不支持多小程序,所以不用判断是否为小程序 diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/cache/SpringAccessTokenCache.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/cache/SpringAccessTokenCache.java index 887c685ad3e4625a27802ff097519b908a9c87ee..dc047334e3f1e7f4225f270599ba55aafde032cb 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/cache/SpringAccessTokenCache.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/cache/SpringAccessTokenCache.java @@ -1,16 +1,14 @@ package net.dreamlu.weixin.cache; import com.jfinal.weixin.sdk.cache.IAccessTokenCache; +import lombok.AllArgsConstructor; import org.springframework.cache.Cache; +@AllArgsConstructor public class SpringAccessTokenCache implements IAccessTokenCache { - private final String ACCESS_TOKEN_PREFIX = "dream-weixin:token:"; + private final static String ACCESS_TOKEN_PREFIX = "dream-weixin:token:"; private final Cache cache; - public SpringAccessTokenCache(Cache cache) { - this.cache = cache; - } - @Override public String get(String key) { return cache.get(ACCESS_TOKEN_PREFIX + key, String.class); diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/DreamWeixinAutoConfiguration.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/DreamWeixinAutoConfiguration.java index a6bbe4c49df4175e9327250b0cd9b90dba2d5beb..06c7fcebb08eda6e3a86fac30562618b88e50759 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/DreamWeixinAutoConfiguration.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/DreamWeixinAutoConfiguration.java @@ -1,10 +1,11 @@ package net.dreamlu.weixin.config; +import lombok.AllArgsConstructor; import net.dreamlu.weixin.aspect.WxApiAspect; import net.dreamlu.weixin.cache.SpringAccessTokenCache; import net.dreamlu.weixin.properties.DreamWeixinProperties; import net.dreamlu.weixin.spring.MsgInterceptor; -import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; @@ -13,34 +14,24 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration -@EnableConfigurationProperties(DreamWeixinProperties.class) +@AutoConfigureAfter(DreamWeixinProperties.class) +@AllArgsConstructor public class DreamWeixinAutoConfiguration { private final CacheManager cacheManager; - private final DreamWeixinProperties weixinProperties; - - public DreamWeixinAutoConfiguration(CacheManager cacheManager, DreamWeixinProperties weixinProperties) { - this.cacheManager = cacheManager; - this.weixinProperties = weixinProperties; - } - - @Bean - public WeixinAppConfig weixinAppConfig() { - return new WeixinAppConfig(weixinProperties); - } @Bean - public SpringAccessTokenCache springAccessTokenCache() { - Cache cache = cacheManager.getCache(weixinProperties.getAccessTokenCache()); + public SpringAccessTokenCache springAccessTokenCache(DreamWeixinProperties properties) { + Cache cache = cacheManager.getCache(properties.getAccessTokenCache()); return new SpringAccessTokenCache(cache); } @Bean - public WxApiAspect wxApiAspect() { - return new WxApiAspect(weixinProperties); + public WxApiAspect wxApiAspect(DreamWeixinProperties properties) { + return new WxApiAspect(properties); } @Configuration - public class MsgConfiguration extends WebMvcConfigurerAdapter { + public static class MsgConfiguration extends WebMvcConfigurerAdapter { private final DreamWeixinProperties properties; public MsgConfiguration(DreamWeixinProperties properties) { diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/WeixinAppConfig.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/WeixinAppConfig.java index 229e51d5f919c3e102ea0e816b1a81c836036f1c..cd7d78c9e6b160d7d4fef64aec464c331afa1ea7 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/WeixinAppConfig.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/config/WeixinAppConfig.java @@ -4,31 +4,46 @@ import com.jfinal.weixin.sdk.api.ApiConfig; import com.jfinal.weixin.sdk.api.ApiConfigKit; import com.jfinal.wxaapp.WxaConfig; import com.jfinal.wxaapp.WxaConfigKit; +import lombok.AllArgsConstructor; import net.dreamlu.weixin.properties.DreamWeixinProperties; import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.context.annotation.Configuration; import java.util.List; +@Configuration +@AutoConfigureAfter(DreamWeixinAutoConfiguration.class) +@AllArgsConstructor public class WeixinAppConfig implements InitializingBean { - private final DreamWeixinProperties weixinProperties; + private final DreamWeixinProperties weixinProperties; - public WeixinAppConfig(DreamWeixinProperties weixinProperties) { - this.weixinProperties = weixinProperties; - } + @Override + public void afterPropertiesSet() throws Exception { + boolean isdev = weixinProperties.isDevMode(); + ApiConfigKit.setDevMode(isdev); + List list = weixinProperties.getWxConfigs(); + for (DreamWeixinProperties.ApiConfig apiConfig : list) { + ApiConfig config = new ApiConfig(); + config.setAppId(apiConfig.getAppId()); + config.setAppSecret(apiConfig.getAppSecret()); + config.setToken(apiConfig.getToken()); + config.setEncodingAesKey(apiConfig.getEncodingAesKey()); + config.setEncryptMessage(apiConfig.isMessageEncrypt()); + ApiConfigKit.putApiConfig(config); + } + DreamWeixinProperties.WxaConfig wxaConfig = weixinProperties.getWxaConfig(); + WxaConfig config = new WxaConfig(); + config.setAppId(wxaConfig.getAppId()); + config.setAppSecret(wxaConfig.getAppSecret()); + config.setToken(wxaConfig.getToken()); + config.setEncodingAesKey(wxaConfig.getEncodingAesKey()); + config.setMessageEncrypt(wxaConfig.isMessageEncrypt()); + WxaConfigKit.setDevMode(isdev); + WxaConfigKit.setWxaConfig(config); + if (WxaMsgParser.JSON == weixinProperties.getWxaMsgParser()) { + WxaConfigKit.useJsonMsgParser(); + } + } - @Override - public void afterPropertiesSet() throws Exception { - boolean isdev = weixinProperties.isDevMode(); - ApiConfigKit.setDevMode(isdev); - List list = weixinProperties.getWxConfigs(); - for (ApiConfig apiConfig : list) { - ApiConfigKit.putApiConfig(apiConfig); - } - WxaConfig wxaConfig = weixinProperties.getWxaConfig(); - WxaConfigKit.setDevMode(isdev); - WxaConfigKit.setWxaConfig(wxaConfig); - if (WxaMsgParser.JSON == weixinProperties.getWxaMsgParser()) { - WxaConfigKit.useJsonMsgParser(); - } - } } diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/properties/DreamWeixinProperties.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/properties/DreamWeixinProperties.java index 32aa61c2b7af7cded3938293ef2c7501626ae3d5..09c4b1c4414670b870278bd31a89d048af7a010d 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/properties/DreamWeixinProperties.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/properties/DreamWeixinProperties.java @@ -1,7 +1,5 @@ package net.dreamlu.weixin.properties; -import com.jfinal.weixin.sdk.api.ApiConfig; -import com.jfinal.wxaapp.WxaConfig; import lombok.Getter; import lombok.Setter; import net.dreamlu.weixin.config.WxaMsgParser; @@ -10,55 +8,63 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.ArrayList; import java.util.List; +@Getter +@Setter @ConfigurationProperties("dream.weixin") public class DreamWeixinProperties { - /** - * JFinal filter拦截的路由,默认:/weixin/* - */ - @Getter - @Setter - private String urlPatterns = "/weixin/*"; + /** + * 拦截的路由,默认:/weixin/* + */ + private String urlPatterns = "/weixin/*"; - /** - * 是否开发模式,默认:false - */ - @Getter - @Setter - private boolean devMode = false; + /** + * 是否开发模式,默认:false + */ + private boolean devMode = false; - /** - * Spring cache name,需要开启spring cache,默认:dreamWeixinCache - */ - @Getter - @Setter - private String accessTokenCache = "dreamWeixinCache"; + /** + * Spring cache name,需要开启spring cache,默认:dreamWeixinCache + */ + private String accessTokenCache = "dreamWeixinCache"; - /** - * 多公众号url挂参,默认:appId - */ - @Getter - @Setter - private String appIdKey = "appId"; + /** + * 多公众号url挂参,默认:appId + */ + private String appIdKey = "appId"; - /** - * 多公众号配置 - */ - @Getter - private List wxConfigs = new ArrayList(); + /** + * 多公众号配置 + */ + private List wxConfigs = new ArrayList<>(); - /** - * 小程序配置 - */ - @Getter - @Setter - private WxaConfig wxaConfig = new WxaConfig(); + /** + * 小程序配置 + */ + private WxaConfig wxaConfig = new WxaConfig(); - /** - * 小程序消息解析,默认xml,支持json和xml - */ - @Getter - @Setter - private WxaMsgParser wxaMsgParser = WxaMsgParser.XML; + /** + * 小程序消息解析,默认xml,支持json和xml + */ + private WxaMsgParser wxaMsgParser = WxaMsgParser.XML; + @Getter + @Setter + public static class ApiConfig { + private String token; + private String appId; + private String appSecret; + private String encodingAesKey; + private boolean messageEncrypt = false; // 消息加密与否 + } + + @Getter + @Setter + public static class WxaConfig { + private String appId; + private String appSecret; + private String token; + private String encodingAesKey; + private boolean messageEncrypt = false; // 消息加密与否 + } } diff --git a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/spring/MsgController.java b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/spring/MsgController.java index a3186c4e9d31092bbea68fd326bccfbf9c31fa96..3e75efacbf37a7d1972affc02e5f576da7af62c3 100644 --- a/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/spring/MsgController.java +++ b/spring-boot-starter-weixin/src/main/java/net/dreamlu/weixin/spring/MsgController.java @@ -16,6 +16,7 @@ import com.jfinal.weixin.sdk.msg.out.OutTextMsg; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -34,9 +35,10 @@ public abstract class MsgController { /** * weixin 公众号服务器调用唯一入口,即在开发者中心输入的 URL 必须要指向此 action * @param imXmlMsg imXmlMsg + * @return {ResponseEntity} */ @RequestMapping("") - public void index(@RequestBody String imXmlMsg) { + public ResponseEntity index(@RequestBody String imXmlMsg) { // 开发模式输出微信服务发送过来的 xml 消息 if (ApiConfigKit.isDevMode()) { System.out.println("接收消息:"); @@ -133,6 +135,7 @@ public abstract class MsgController { logger.error("未能识别的消息类型。 消息 xml 内容为:\n" + imXmlMsg); processIsNotDefinedMsg((InNotDefinedMsg) msg); } + return ResponseEntity.ok().build(); } /** diff --git a/spring-boot-starter-weixin/src/main/resources/META-INF/spring.factories b/spring-boot-starter-weixin/src/main/resources/META-INF/spring.factories index 95ec429c52e770b7a999ad586229162c78409c20..e66dbdf80cd58c318322158da41e40e14b5ed7df 100644 --- a/spring-boot-starter-weixin/src/main/resources/META-INF/spring.factories +++ b/spring-boot-starter-weixin/src/main/resources/META-INF/spring.factories @@ -1,2 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + net.dreamlu.weixin.properties.DreamWeixinProperties,\ + net.dreamlu.weixin.config.WeixinAppConfig,\ net.dreamlu.weixin.config.DreamWeixinAutoConfiguration diff --git a/spring-boot-starter-weixin/src/main/resources/META-INF/spring.provides b/spring-boot-starter-weixin/src/main/resources/META-INF/spring.provides index 3f06a210f18a3f968efc663f7f02e68fca0c1933..59daa36ea84da98e7e83a2956b196bf91d5eff02 100644 --- a/spring-boot-starter-weixin/src/main/resources/META-INF/spring.provides +++ b/spring-boot-starter-weixin/src/main/resources/META-INF/spring.provides @@ -1 +1 @@ -provides: jfinal, jfinal-weixin, cglib +provides: jfinal, jfinal-weixin diff --git a/spring-boot-weixin-demo/src/main/java/com/example/demo/WeixinController.java b/spring-boot-weixin-demo/src/main/java/com/example/demo/WeixinController.java index bc52b087d8fcd80504f600de0bb920f67b90f074..b1caaa5f7fec97c4553a96503017937ebc09ee6f 100644 --- a/spring-boot-weixin-demo/src/main/java/com/example/demo/WeixinController.java +++ b/spring-boot-weixin-demo/src/main/java/com/example/demo/WeixinController.java @@ -1,6 +1,5 @@ package com.example.demo; -import com.jfinal.weixin.sdk.jfinal.MsgControllerAdapter; import com.jfinal.weixin.sdk.msg.in.InTextMsg; import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent; import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent; diff --git a/spring-boot-weixin-demo/src/main/resources/application.yml b/spring-boot-weixin-demo/src/main/resources/application.yml index e906d060b5f123a32184c5e3d488a926056d435a..b3c9eb45f642ab2e43bf920c7467e1846bb49b01 100644 --- a/spring-boot-weixin-demo/src/main/resources/application.yml +++ b/spring-boot-weixin-demo/src/main/resources/application.yml @@ -8,5 +8,5 @@ dream: appSecret: 11ed9e2b8e3e3c131e7be320a42b2b5a token: 123456 wxa-config: - appid: wx4f53594f9a6b3dcb - appSecret: eec6482ba3804df05bd10895bace0579 \ No newline at end of file + app-id: wx4f53594f9a6b3dcb + app-secret: eec6482ba3804df05bd10895bace0579 \ No newline at end of file