微信登录
1.所需maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
2.yml文件
XF:
# 微信授权登录配置
# 凭证获取GET
wx_token_url: https://open.weixin.qq.com/connect/oauth2/authorize?
# 用户同意授权,回调url----你的域名后面再加上/getcode
wx_redirect_url: 回调地址
# 获取openid
wx_openid_url: https://api.weixin.qq.com/sns/oauth2/access_token?
# 拉取用户信息
wx_userinfo_url: https://api.weixin.qq.com/sns/userinfo?
# 微信appid-----换成自己的
wx_appid: 你的appid
# 微信appSecret-----换成自己的
wx_secret: appSecret
3.controller层
// An highlighted block
@Value("${XF.wx_token_url}")
private String wx_token_url;
@Value("${XF.wx_appid}")
private String wx_appid;
@Value("${XF.wx_redirect_url}")
private String wx_redirect_url;
@Value("${XF.wx_openid_url}")
private String wx_openid_url;
@Value("${XF.wx_secret}")
private String wx_secret;
@Value("${XF.wx_userinfo_url}")
private String wx_userinfo_url;
/**
* 获取Code
* @param code Code
* @return
*/
@CrossOrigin(origins = "*",maxAge = 3600)//跨域
@PostMapping(value = "/getCode")
@ResponseBody
public Map<String,Object> getCode(String code, HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "*");
Map<String,Object> p=new HashMap<String,Object>();
// 根据Code获取Openid
String openidUrl = wx_openid_url + "appid=" + wx_appid + "&secret=" + wx_secret + "&code=" + code + "&grant_type=authorization_code";
String openidMsg = MyHttpUtils.doPost