Micro-channel in order to obtain the user authorization is openId
Micro-channel public number:
https://mp.weixin.qq.com/
micro-channel pay :( only qualified enterprises can apply for micro-channel payment)
https://pay.weixin.qq.com/
micro-channel payment process documentation:
HTTPS: / /pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_4
micro letter public, authorized the document:
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
Purchase a domain name
if the domain name has been and is filed can ignore this step
the following steps of the method can access the local domain name has been configured for the purchase of test:
URL: https://natapp.cn/login
1: buy a year of tunnel:
2: Buy a can support micro-domain authorization letter:
3: binding domain just purchased and the local port can access
4: Download natapp client, edit the file confing.ini authtoken fill:
5: Double-click natapp.exe start natapp
6: you can access the application through the local port 80 applenst.natapp4.cc
Micro-letter domain name Configuration
1: micro-channel public account Log: Development - Interface Permissions - Web Services - Web Account - Web authorization to obtain information about users' configuration options, modify the authorization callback domain:
2: Download the micro-channel to be certified MP_verify_WKBRuq9wmTSbo3Ep.txt into static directory project
3: In order to make micro-channel authentication, temporary access path of the project to remove and restart the project
4: Let the micro-channel certification
5: Successful certification
6: The project path to come back and restart the project
Functional development
documentation
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
Dependent instructions
in pom file to add third-party sdk relies
<! - micro-channel authorization dependent ->
<dependency>
<the groupId> com.github.binarywang </ the groupId>
<the artifactId> Weixin-Java-MP </ the artifactId>
<Version> 2.7.0 </ Version>
</ dependency >
code implements
1: configure public number in the configuration file and Secret appid
wechat:
mpAppId: wxXXXXXXXXXXXXXXXXXXX83c0577b
mpAppSecret: b6XXXXXXXXXXXXXXXff886d71
domainUrl: http://applenst.natapp4.cc # micro-channel callback domain name (domain name of Project Access)
2: Create a profile JavaBean
COM \ imooc \ config \ WechatAccountConfig.java
Data
Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
/*公众号微信授权appid*/
private String mpAppId;
/*公众号微信授权app Secret*/
private String mpAppSecret;
/*微信回调域名*/
private String domainUrl;
}
3: Create authorized micro-channel configuration class
com \ imooc \ config \ WeChatMpConfig.java
package com.imooc.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;
/**
- @author: menghaibin
- @create: 2020-02-22 22:05
-
@description: micro-channel authorization configuration br /> ** /
@Component
public class WeChatMpConfig {@Autowired
private WechatAccountConfig wechatAccountConfig;br/>@Bean
public WxMpService wxMpService(){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
4:创建微信授权controller
com\imooc\controller\WechatController.java
@Controllerbr/>@RequestMapping("/wechat")
@Slf4j
public class WechatController {
/*注解微信授权sdk的业务接口*/
@Autowired
private WxMpService wxMpService;
/*注解微信授权配置javabean*/
@Autowired
private WechatAccountConfig accountConfig;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl){
/*url:授权成功后要跳转的路径*/
String url = accountConfig.getDomainUrl() + "/sell/wechat/userInfo";
/*进行微信授权,授权成功后会在userinfo方法中得到code,并得到一个微信返回的url 通过这个url可以跳转到上面定义的url*/
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
log.info(redirectUrl);
/*进行跳转*/
return "redirect:"+redirectUrl;
}
@GetMapping("/userInfo")
public String userInfo(@RequestParam("code") String code,
@RequestParam("state") String returnUrl){
/*code:授权成功后会返回一个code
* state:授权成功后的原始参数*/
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try{
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
}catch (WxErrorException e){
log.error("微信授权失败");
}
/*得到openid*/
String openId = wxMpOAuth2AccessToken.getOpenId();
/*跳转到系统*/
return "redirect:"+ returnUrl + "?openid="+openId;
}
}
5: test micro-channel authorization (mobile access, normal to jump to Baidu)
http://applenst.natapp4.cc/sell/wechat/authorize?returnUrl=http://www.baidu.com
6: Build the project and the FBI
1> configure virtual machines authorized access path:
/opt/code/sell_fe_buyer/config/index.js
Enter / opt / code / sell_fe_buyer directory rebuild command execution
npm run build
Copy the file to take effect
cp -r dist/* /opt/data/wwwrot/sell/
2> configure the phone proxy can access computer terminal
to download and install fiddler set
https://www.telerik.com/fiddler
Iphone mobile terminal configuration:
Settings - Wireless Network - Configure proxy - Manual
Micro-channel access project: sell.com (domain name nginx configuration, pc's hosts file to perform virtual machine ip):