文章目录
一、域名购买与配置
1、进行域名购买:
2、购买成功后进入配置选项,绑定二级域名:
3、绑定域名前需要注册一个二级域名,点击立即注册:
4、下面进行域名注册:
5、注册完以后进行绑定:
6、启动tomcat以后就进行域名访问:
报错:
如果报这种错首先检查:127.0.0.1::8080端口能不能访问正常,在静态资源文件夹下面写一个index.html文件,然后进行访问,要加上项路径/sell:
其次,别忘记启动下载的natapp客户端,natapp.exe,这里需要说明的是,下载的解压文件不要放在桌面上,否则启动后悔一闪而过,启动命令:natapp -authtoken=xxx
启动后的界面:
启动以后不要关闭这个界面。
再次进行域名访问:
二、网页授权回调域名(手工方式获取openid)
1. 网页授权(微信平台配置白名单)
1、申请接口测试号:
2、中间的信息都不用填,也不用更改,直接来到网页服务–》网页帐号 --》网页授权获取用户基本信息–》修改:
3、点击修改后将之前买好的域名填进去确认即可:
2. 用户同意授权获取code
参数说明:将请求连接中的三个参数改一下:
请求连接:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd0430ca3dde79d52&redirect_uri=http://heng.nat300.top/sell/weixin/auth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
这个链接要在微信客户端发送才行,也就是说登录微信,发送给微信传输助手或者微信团队都行,发送之后点击这个连接,获取我们想要的code:
这个程序就是redirect_uri的重定向地址:
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {
@RequestMapping("/auth")
public void auth(@RequestParam("code")String code){
log.info("进入auth方法。。。");
log.info("code={}",code);
}
}
3. 使用code换取access_token
请求连接,这个链接放在请求参数中换取access_token:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd0430ca3dde79d52&secret=46088f58a30ffb26b12d921735d26691&code=071M3T0K17iDs10CBK2K1yWL0K1M3T0S&grant_type=authorization_code
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {
@RequestMapping("/auth")
public void auth(@RequestParam("code")String code){
log.info("进入auth方法。。。");
log.info("code={}",code);
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd0430ca3dde79d52&secret=46088f58a30ffb26b12d921735d26691&code=071M3T0K17iDs10CBK2K1yWL0K1M3T0S&grant_type=authorization_code";
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class);
log.info("response={}",response);
}
}
在response中就有我们想要的openid:
response={"access_token":"31_9myTyH8mVTgWpWS-Y4BE8My9HzGOKylH1lUt7GiAztKxhyNayYOOZGd1DTy3sXS6CHl13J-hrXv81y3ZpiDo-A",
"expires_in":7200,
"refresh_token":"31_MAVjbCU2d1gcw20G9KuoWbWd9AlpIxqTNNdckYyIVe4F5bKLYvi-MHPJIfq4JRrrr_o5ug2HKubl1hr2yUAxNQ",
"openid":"oahahaxEYH7kpwRVfBCc9hych-CE","scope":"snsapi_userinfo"}
三、使用第三方SDK获取openid
1、引入依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.7.0</version>
</dependency>
2、在application.properties中配置微信账号信息:
wechat.mpAppId=wxd0430ca3dde79d52
wechat.mpAppSecret=46088f58a30ffb26b12d921735d26691
3、编写与微信账号相关的配置类:
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String mpAppId;
private String mpAppSecret;
}
由于使用了@ConfigurationProperties(prefix = “wechat”)注解,引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
4、编写微信公众号相关的配置类:
@Configuration
public class WechatMpConfig {
@Autowired
private WechatAccountConfig accountConfig;
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
5、根据API编写Controller层:
下面为API文档:
下面为Controller层代码:
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WeChatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl")String returnUrl){
//1.配置
//2.调用方法
String url="http://heng.nat300.top/sell/wechat/userInfo";
String redirectUrl
= wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
return "redirect:"+redirectUrl;
}
@GetMapping("/userInfo")
public String userInfo(@RequestParam("code")String code,@RequestParam("state")String returnUrl){
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try {
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
log.error("【微信网页授权】{}",e);
throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(),e.getError().getErrorMsg());
}
String openId = wxMpOAuth2AccessToken.getOpenId();
return "redirect:" + returnUrl + "?openid=" + openId;
}
}
6、获取openid
在后端打上断点启动SpringBoot:
在网页输入localhost:8080/sell/wechat/authorize?returnUrl=http:www.imooc.com
会弹出到微信客户端访问的页面,粘贴这个连接发送给微信传输助手到微信客户端访问,通过后端debug结果可以看出,可以获得openid了: