springboot项目
1.引入pom依赖
dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.7.0</version>
</dependency>
2.创建配置类,引入参数
package com.imooc.sell.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**微信支付与网页授权的配置对象类
* @Author:
* @Date: 2019/3/15 17:50
* @Version 1.0
*/
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
/*公众号appid*/
private String mpAppId;
/*公众号secret*/
private String mpAppSecret;
}
在application.properties里面配置mpAppid和mpAppsercert ,这两个参数在测试号的最上面有
#测试号的相关的appid和appsecret wechat.mpAppId =xxxxxx wechat.mpAppSecret =xxxxxxxx
3.创建配置类返回一个WxMpService,为神魔要这个对象可以参考sdk
package com.imooc.sell.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:
* @Date: 2019/3/15 17:41
* @Version 1.0
*/
@Component//注解为配置类
public class WechatMPConfig {
@Autowired
WechatAccountConfig wechatAccountConfig;
@Bean
public WxMpService wxMpService()
{
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage()
{
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
//在这里我们要设置appid 和 appsecret 需要在配置文件里面设置两个变量,这样全局都可以用
//然后设置一个WechatAccountConfig类,来注入这两个参数,这样在使用的时候就可以直接调用这两个类
wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
4.创建WechatController
package com.imooc.sell.controller;
import com.imooc.sell.enums.ResultEnum;
import com.imooc.sell.exception.ResultException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import net.bytebuddy.asm.Advice;
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 org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
/**
* @Author:
* @Date: 2019/3/15 17:38
* @Version 1.0
*/
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl)
{
//1.配置
//2.调用方法
String url = 外网地址+"/sell/wechat/userinfo";
//返回之后从定向的url,调用方法
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));
log.info("【微信网页授权】返回redirectUrl = {}",redirectUrl );
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("【微信网页授权】获取accessToken失败");
}
String openId = wxMpOAuth2AccessToken.getOpenId();
log.info("【微信网页授权】openid={}",openId);
return "redirect:"+returnUrl+"?openid="+ openId;
}
}
这样我们就可以通过访问http://域名/项目名/wechat/authorize?return_url=XXXXXXX在微信里面获取到授权了