You must already know SpringBoot and configure the callback interface authorized by WeChat.
SDK address: https://github.com/Pay-Group/best-pay-sdk/blob/master/doc/use.md
pow dependency
<!-- If you need to add eclipse plugin, you can use @Data annotation, etc. If you don't use this tool, you need to add set get and other methods --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
<!-- WeChat public account sdk --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.7.0</version> </dependency>
Project url [This project url uses a domain name, how to bind a domain name, please refer to the official WeChat documentation]
package cn.edu.jxnu.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Wechat public platform url prefix configuration injects the value of springboot property file * * @author Dream Blur. * @version V1.0 * @time April 13, 2018 */ @Data @ConfigurationProperties(prefix = "projectUrl") @Component public class ProjectUrlConfig { /** * WeChat public platform authorization url */ public String wechatMpAuthorize; /** * WeChat open platform authorization url, authorization is not used */ //public String wechatOpenAuthorize; }
Again:
projectUrl, wechat is the prefix of SpringBoot's application.properties such as projectUrl=openid
Account attribute value injection
package cn.edu.jxnu.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; /** * Wechat account configuration read property file value * * @author Dream Blur. * @version V1.0 * @time April 13, 2018 */ @Data @Component @ConfigurationProperties(prefix = "wechat") public class WechatAccountConfig { /** * Public platform id */ private String mpAppId; /** * Public platform key */ private String mpAppSecret; }
WeChat public account configuration
package cn.edu.jxnu.config; import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; /** * WeChat public platform configuration * * @author dream blur * @version V1.0 * @time April 13, 2018 */ @Component public class WechatMpConfig { @Autowired private WechatAccountConfig accountConfig; /** * WeChat public account service layer bean registration * * @time 6:08:13 PM * @version V1.0 * @return WxMpService */ @Bean public WxMpService wxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } /** * WeChat public account configuration bean registration * * @time 6:08:41 PM * @version V1.0 * @return WxMpConfigStorage */ @Bean public WxMpConfigStorage wxMpConfigStorage() { WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); // Set the developer's id and key wxMpConfigStorage.setAppId(accountConfig.getMpAppId()); wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret()); return wxMpConfigStorage; } }
controller
package cn.edu.jxnu.controller; import java.net.URLEncoder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import cn.edu.jxnu.config.ProjectUrlConfig; import cn.edu.jxnu.enums.ResultEnum; import cn.edu.jxnu.exception.SellException; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; /** * WeChat authorized, use github's WeChat sdk * * @author Dream Blur. * @version V1.0 * @time April 17, 2018 */ @Controller // RestController cannot be used when redirection is required @RequestMapping("/wechat") @ Slf4j public class WechatController { @Autowired private WxMpService wxMpService; //@Autowired //private WxMpService wxOpenService; @Autowired private ProjectUrlConfig projectUrlConfig; /**The first step: request CODE, the necessary parameters return * This method implements request authorization * @time 6:17:37 PM * @version V1.0 * @param returnUrl * @return redirect string */ @SuppressWarnings("deprecation") @GetMapping("/authorize") public String authorize(@RequestParam("returnUrl") String returnUrl) { // 1. Configuration // 2. Call the method String url = projectUrlConfig.getWechatMpAuthorize() + "/project name or/ /wechat/userInfo"; // OAUTH2_SCOPE_BASE direct authorization by default String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));// Redirect to callback interface address redirectUrl, url must be encoded log.info("Authorization: {}", redirectUrl); return "redirect:" + redirectUrl; } /**Step 2: Get access_token through code * The above authorize method redirects to this method to get user information * After the user allows authorization, it will be redirected to the URL of redirect_uri with code and state parameters * @time 6:17:59 PM * @version V1.0 * @param code * @param returnUrl * @return redirect string */ @GetMapping("/userInfo") public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) { WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { //Get access_token through code wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); } catch (WxErrorException e) { log.error("[WeChat webpage authorization]{}", e); // keep throwing throw Exception(); } // Get the openid to this step, the point has been completed String openId = wxMpOAuth2AccessToken.getOpenId(); log.info("获得openid:{}", openId); // The front-end and back-end development documents of this interface are specified, depending on the situation return "redirect:" + returnUrl + "?openid=" + openId;}}
It can be seen that only the code is unknown and needs to be carried
The WeChat side uses the domain name /wechat/authorize?returnUrl=callback address [configured by WeChat]
Summarize:
1. Configure the developer id and key
2. Set the WeChat callback interface, and set and inject in the project
3. Register WeChat authorization bean [WxMpConfigStorage, WxMpService] The former is to set the configuration file, the latter is a service, which has authorization encapsulation
4. Write the controller,
Step 1: Request CODE [authoeize method]
Step 2: Get access_token through code [userInfo method]
The third step: call the interface through access_token [the specific situation of this step depends on the project]