本文用项目框架为spring boot
本文旨在简介微信第三方登陆的主要步骤,
网站微信第三方登陆需要几个条件
1.拥有微信开放平台
如果没有的话需要到https://open.weixin.qq.com/申请
2.进入开放平台,并在网站应用下创建相应的应用
整个过程有以下步骤
1.生成微信二维码页面扫码登陆,并返回至回掉URL
2.拿到浏览器url的code,通过该code获取access_token和openid
3.通过access_token和openid获取登陆用户信息
1.通过
https://open.weixin.qq.com/connect/qrconnect?appid=appid&redirect_uri=跳转链接&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
生成页面,扫码登陆后地址栏会附带code参数
2.拿到地址栏的code,通过code获取登陆用户的信息
代码如下:(这里将第二步和第三部结合到了一起)
/**
* 获取微信用户信息
* @param code
* @return
*/
@GetMapping("/weixin/userInfo")
@ResponseBody
public ResponseEntity<Result> getWeixinUserInfo(@RequestParam(value="code") String code){
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8)
.body(Result.build().content(weixinService.getUserInfo(code)));
}
/**
* @author Lichenyi
* @date 2017-7-5
*/
@SuppressWarnings("all")
@Service("IWeixinService")
public class WeixinService implements IWeixinService {
private Logger logger = LogManager.getLogger(getClass());
@Autowired
private RestTemplate restTemplate;
@Value("${weixin.open.connect}")
private String weixin_open_connect;//网站跳转
@Value("${weixin.open.sns.oauth2}")
private String weixin_open_sns_oauth2;//获取access token
@Value("${weixin.open.sns.userinfo}")
private String weixin_open_sns_userinfo;//根据access token 获取用户信息
/**
* 获取access_token
*
* @param code
* @return
*/
private JSONObject getAccessToken(String code) {
String tokenJson = null;
String url = String.format(weixin_open_sns_oauth2, code);
String resultStr = restTemplate.getForObject(url, String.class);
JSONObject result = JSONObject.parseObject(resultStr);
if (result.getString("access_token") != null) {
return result;
}
logger.error(String.format("获取accesstoken错误 %s", result.toJSONString()));
return result;
}
/**
* 获取用户信息
*
* @param code
* @return
*/
@Override
public JSONObject getUserInfo(String code) {
try {
JSONObject accessToken = getAccessToken(code);
if (accessToken.getString("access_token") == null) {
return accessToken;
}
String token = accessToken.getString("access_token");
String openid = accessToken.getString("openid");
String url = String.format(weixin_open_sns_userinfo, token, openid);
String resultStr = restTemplate.getForObject(url, String.class);
resultStr = new String(resultStr.getBytes("ISO-8859-1"), "UTF-8");
JSONObject result = JSONObject.parseObject(resultStr);
if (result.getString("openid") != null) {
return result;
}
logger.error(String.format("获取用户信息错误 %s", result.toJSONString()));
return result;
}catch (UnsupportedEncodingException e){
logger.error(String.format("转码错误 %s", e.getMessage()));
e.printStackTrace();
}
return null;
}
}
application.properties文件
server.port=8888
##微信相关接口
weixin.open.appid=
weixin.open.secret=
weixin.open.redirect_uri=http://m.mjiahome.com/index.html
#生成二维码的页面
weixin.open.connect=https://open.weixin.qq.com/connect/qrconnect?appid=${weixin.open.appid}&redirect_uri=${weixin.open.redirect_uri}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
weixin.open.sns.oauth2=https://api.weixin.qq.com/sns/oauth2/access_token?appid=${weixin.open.appid}&secret=${weixin.open.secret}&code=%s&grant_type=authorization_code
weixin.open.sns.userinfo=https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s
项目最后返回结果:
{
"errorCode": 0,
"errorDescription": "success",
"requestId": "a9026ed2-2986-434c-8762-88deb14cb54d",
"result": {
"country": "CN",
"unionid": "oV6Xy0rmk13eJp_TCOHKOKcbzGFI",
"province": "Henan",
"city": "Luoyang",
"openid": "oe9Uv01iSHLbUR439JUY-nT3Rpbg",
"sex": 1,
"nickname": "一壶酒",
"headimgurl": "http://wx.qlogo.cn/mmopen/muJQmcibTZam2heIDTXUseWtwyxiarxFXtACucoib1w5PibiaDun7EJibw6ibfC0z1XxSpmjmickKK0Ms2nwCOezy9WYLp14rH1RhjEib/0",
"language": "zh_CN",
"privilege": []
}
}