├── .gitignore ├── README.md ├── pom.xml ├── spring-boot-starter-wechatmp ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── wechat │ │ └── mp │ │ ├── WechatMpAutoConfiguration.java │ │ └── WechatMpProperties.java │ └── resources │ └── META-INF │ ├── spring.factories │ └── spring.provides └── spring-boot-wechatmp-demo ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── mvnsearch │ │ └── wechat │ │ ├── SpringBootWechatmpDemoApplication.java │ │ └── WechatController.java └── resources │ └── application.properties └── test └── java └── org └── mvnsearch └── wechat └── SpringBootWechatmpDemoApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | *.class 4 | 5 | # Mobile Tools for Java (J2ME) 6 | .mtj.tmp/ 7 | 8 | # Package Files # 9 | *.jar 10 | *.war 11 | *.ear 12 | 13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 14 | hs_err_pid* 15 | ### JetBrains template 16 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 17 | 18 | *.iml 19 | 20 | ## Directory-based project format: 21 | .idea/ 22 | # if you remove the above rule, at least ignore the following: 23 | 24 | # User-specific stuff: 25 | # .idea/workspace.xml 26 | # .idea/tasks.xml 27 | # .idea/dictionaries 28 | 29 | # Sensitive or high-churn files: 30 | # .idea/dataSources.ids 31 | # .idea/dataSources.xml 32 | # .idea/sqlDataSources.xml 33 | # .idea/dynamic.xml 34 | # .idea/uiDesigner.xml 35 | 36 | # Gradle: 37 | # .idea/gradle.xml 38 | # .idea/libraries 39 | 40 | # Mongo Explorer plugin: 41 | # .idea/mongoSettings.xml 42 | 43 | ## File-based project format: 44 | *.ipr 45 | *.iws 46 | 47 | ## Plugin-specific files: 48 | 49 | # IntelliJ 50 | /out/ 51 | 52 | # mpeltonen/sbt-idea plugin 53 | .idea_modules/ 54 | 55 | # JIRA plugin 56 | atlassian-ide-plugin.xml 57 | 58 | # Crashlytics plugin (for Android Studio and IntelliJ) 59 | com_crashlytics_export_strings.xml 60 | crashlytics.properties 61 | crashlytics-build.properties 62 | 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-boot-start-wechatmp 2 | =================================== 3 | Spring Boot对微信公众号(订阅号、服务号)的支持 4 | 5 | ### usage 6 | in pom.xml add following dependency: 7 | 8 | 9 | org.mvnsearch.wechat 10 | spring-boot-starter-wechatmp 11 | 1.0.0-SNAPSHOT 12 | 13 | 14 | in application.properties file, please add following keys: 15 | 16 | spring.wechatmp.appId=xxxx 17 | spring.wechatmp.secret=yyyy 18 | If you set token and aesKey in wechat backend, please add following settings: 19 | 20 | spring.wechatmp.token=your-token 21 | spring.wechatmp.aesKey=your-aeskey 22 | If you want to implement wechat payment features, please add following settings: 23 | 24 | spring.wechatmp.partnerId=your-partnerId 25 | spring.wechatmp.partnerKey=your-partnerKey 26 | in your code you can use OssClient directly: 27 | 28 | @Autowired 29 | WxMpService wxMpService; 30 | 31 | ### spring-boot-start-wechatmp service List 32 | 33 | * me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage: config storage 34 | * me.chanjar.weixin.mp.api.WxMpService: wechat MP service 35 | 36 | ### 参考 37 | 38 | * 微信开放平台: https://open.weixin.qq.com/ 39 | * Wechat接入开发文档: https://github.com/wechat-group/weixin-java-tools -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | org.mvnsearch.wechat 6 | spring-boot-wechatmp-parent 7 | pom 8 | 1.0.0-SNAPSHOT 9 | Spring Boot with Wechat MP 10 | 11 | 12 | spring-boot-starter-wechatmp 13 | spring-boot-wechatmp-demo 14 | 15 | 16 | 17 | UTF-8 18 | 1.8 19 | 4.2.5.RELEASE 20 | 1.3.3.RELEASE 21 | 22 | 23 | 24 | 25 | 26 | org.springframework 27 | spring-framework-bom 28 | ${spring.version} 29 | pom 30 | import 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-dependencies 35 | ${spring-boot.version} 36 | pom 37 | import 38 | 39 | 40 | io.spring.platform 41 | platform-bom 42 | 2.0.3.RELEASE 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /spring-boot-starter-wechatmp/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.mvnsearch.wechat 7 | spring-boot-starter-wechatmp 8 | 1.0.0-SNAPSHOT 9 | jar 10 | 11 | spring-boot-starter-wechatmp 12 | Demo project for Spring Boot 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-actuator 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-configuration-processor 28 | true 29 | 30 | 31 | com.github.binarywang 32 | weixin-java-mp 33 | 2.1.0 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-dependencies 47 | ${spring-boot.version} 48 | pom 49 | import 50 | 51 | 52 | 53 | 54 | 55 | 56 | mvnsearch_nexus 57 | mvnsearch nexus Releases 58 | http://nexus.mvnsearch.org/content/repositories/releases/ 59 | 60 | 61 | mvnsearch_nexus 62 | mvnsearch nexus Snapshots 63 | http://nexus.mvnsearch.org/content/repositories/snapshots/ 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /spring-boot-starter-wechatmp/src/main/java/org/mvnsearch/wechat/mp/WechatMpAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.wechat.mp; 2 | 3 | import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; 4 | import me.chanjar.weixin.mp.api.WxMpService; 5 | import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 8 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 9 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | 13 | /** 14 | * wechat map auto configuration 15 | * 16 | * @author linux_china 17 | */ 18 | @Configuration 19 | @ConditionalOnClass(WxMpService.class) 20 | @EnableConfigurationProperties(WechatMpProperties.class) 21 | public class WechatMpAutoConfiguration { 22 | @SuppressWarnings("SpringJavaAutowiringInspection") 23 | @Autowired 24 | private WechatMpProperties properties; 25 | 26 | @Bean 27 | @ConditionalOnMissingBean 28 | public WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage() { 29 | WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage(); 30 | configStorage.setAppId(properties.getAppId()); 31 | configStorage.setSecret(properties.getSecret()); 32 | configStorage.setToken(properties.getToken()); 33 | configStorage.setAesKey(properties.getAesKey()); 34 | configStorage.setPartnerId(properties.getPartnerId()); 35 | configStorage.setPartnerKey(properties.getPartnerKey()); 36 | return configStorage; 37 | } 38 | 39 | @Bean 40 | @ConditionalOnMissingBean 41 | public WxMpService wxMpService(WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage) { 42 | WxMpService wxMpService = new WxMpServiceImpl(); 43 | wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage); 44 | return wxMpService; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /spring-boot-starter-wechatmp/src/main/java/org/mvnsearch/wechat/mp/WechatMpProperties.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.wechat.mp; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | * wechat mp properties 7 | * 8 | * @author linux_china 9 | */ 10 | @ConfigurationProperties( 11 | prefix = "spring.wechatmp" 12 | ) 13 | public class WechatMpProperties { 14 | /** 15 | * 设置微信公众号的appid 16 | */ 17 | private String appId; 18 | /** 19 | * 设置微信公众号的app secret 20 | */ 21 | private String secret; 22 | /** 23 | * 微信支付partner id 24 | */ 25 | private String partnerId; 26 | /** 27 | * 微信支付partner key 28 | */ 29 | private String partnerKey; 30 | /** 31 | * 设置微信公众号的token 32 | */ 33 | private String token; 34 | /** 35 | * 设置微信公众号的EncodingAESKey 36 | */ 37 | private String aesKey; 38 | 39 | public String getAppId() { 40 | return appId; 41 | } 42 | 43 | public void setAppId(String appId) { 44 | this.appId = appId; 45 | } 46 | 47 | public String getSecret() { 48 | return secret; 49 | } 50 | 51 | public void setSecret(String secret) { 52 | this.secret = secret; 53 | } 54 | 55 | public String getPartnerId() { 56 | return partnerId; 57 | } 58 | 59 | public void setPartnerId(String partnerId) { 60 | this.partnerId = partnerId; 61 | } 62 | 63 | public String getPartnerKey() { 64 | return partnerKey; 65 | } 66 | 67 | public void setPartnerKey(String partnerKey) { 68 | this.partnerKey = partnerKey; 69 | } 70 | 71 | public String getToken() { 72 | return token; 73 | } 74 | 75 | public void setToken(String token) { 76 | this.token = token; 77 | } 78 | 79 | public String getAesKey() { 80 | return aesKey; 81 | } 82 | 83 | public void setAesKey(String aesKey) { 84 | this.aesKey = aesKey; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /spring-boot-starter-wechatmp/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | org.mvnsearch.wechat.mp.WechatMpAutoConfiguration 3 | -------------------------------------------------------------------------------- /spring-boot-starter-wechatmp/src/main/resources/META-INF/spring.provides: -------------------------------------------------------------------------------- 1 | provides: spring-boot-starter-wechatmp -------------------------------------------------------------------------------- /spring-boot-wechatmp-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch.wechat 8 | spring-boot-wechatmp-parent 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | spring-boot-wechatmp-demo 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | spring-boot-wechatmp-demo 17 | Spring Boot with wechat MP Demo 18 | 19 | 20 | UTF-8 21 | 1.8 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-actuator 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | org.mvnsearch.wechat 35 | spring-boot-starter-wechatmp 36 | 1.0.0-SNAPSHOT 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-boot-wechatmp-demo/src/main/java/org/mvnsearch/wechat/SpringBootWechatmpDemoApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.wechat; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * spring boot wechatMP Demo application 8 | * 9 | * @author linux_china 10 | */ 11 | @SpringBootApplication 12 | public class SpringBootWechatmpDemoApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(SpringBootWechatmpDemoApplication.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-wechatmp-demo/src/main/java/org/mvnsearch/wechat/WechatController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.wechat; 2 | 3 | import me.chanjar.weixin.mp.api.WxMpConfigStorage; 4 | import me.chanjar.weixin.mp.api.WxMpService; 5 | import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; 6 | import me.chanjar.weixin.mp.bean.result.WxMpUser; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.actuate.metrics.CounterService; 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestParam; 14 | 15 | import java.util.UUID; 16 | 17 | /** 18 | * wechat controller 19 | * 20 | * @author linux_china 21 | */ 22 | @Controller 23 | @RequestMapping("/wechat") 24 | public class WechatController { 25 | private Logger log = LoggerFactory.getLogger(WechatController.class); 26 | @Autowired 27 | private WxMpService wxMpService; 28 | @Autowired 29 | private WxMpConfigStorage wxMpConfigStorage; 30 | @Autowired 31 | private CounterService counterService; 32 | 33 | @RequestMapping("/login") 34 | public String login() { 35 | String redirectUrl = "http://www.foobar.com/wechat/callback.action"; 36 | String state = UUID.randomUUID().toString(); 37 | String snsapiLogin = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, "snsapi_login", state); 38 | return "redirect:" + snsapiLogin; 39 | } 40 | 41 | @RequestMapping("/weblogin") 42 | public String webLogin() { 43 | String state = UUID.randomUUID().toString(); 44 | String callbackUrl = "http%3A%2F%2Fwww.xxxx.com%2Fwechat%2Fcallback.action"; 45 | return "redirect:https://open.weixin.qq.com/connect/qrconnect?appid=" + wxMpConfigStorage.getAppId() + "&redirect_uri=" + callbackUrl + "&response_type=code&scope=snsapi_login&state=" + state + "#wechat_redirect"; 46 | } 47 | 48 | @RequestMapping("/callback") 49 | public String callback(@RequestParam String code) { 50 | try { 51 | WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code); 52 | WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, "zh_CN"); 53 | if (wxMpUser != null) { 54 | //.... 55 | } 56 | } catch (Exception e) { 57 | counterService.increment("error.wechat.callback"); 58 | log.error("error.wechat.callback", e); 59 | } 60 | return "redirect:/home"; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /spring-boot-wechatmp-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.wechatmp.app-id=your-app-id 2 | spring.wechatmp.secret=your-secret -------------------------------------------------------------------------------- /spring-boot-wechatmp-demo/src/test/java/org/mvnsearch/wechat/SpringBootWechatmpDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.wechat; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.web.WebAppConfiguration; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | 9 | /** 10 | * wechat mp demo tests 11 | * 12 | * @author linux_china 13 | */ 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @SpringApplicationConfiguration(classes = SpringBootWechatmpDemoApplication.class) 16 | @WebAppConfiguration 17 | public class SpringBootWechatmpDemoApplicationTests { 18 | 19 | @Test 20 | public void contextLoads() { 21 | } 22 | 23 | } 24 | --------------------------------------------------------------------------------