解决网址微信扫码登录
文章目录
一、添加配置
application.properties。
wx.open.app_id=微信开发平台APPid
wx.open.app_secret=微信开发平台APPsecret
wx.open.redirect_url=回调地址
二、代码
1.ConstantWxUtils
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties") //读取配置文件
@ConfigurationProperties(prefix="wx.open") //读取wx.open节点
@Data
public class ConstantWxUtils{
private String appId;
private String appSecret;
private String redirectUrl;
}
2.WxApiController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@Controller //这里不能用RestController,会重定向失败
@RequestMapping("/api/wx")
public class WxApiController {
@Resource
private ConstantWxUtils utils;
@GetMapping("login")
public String getWxCode(){
//使用 urlEncode 对链接进行处理
String redirectUri = utils.getRedirectUrl();
try {
redirectUri = URLEncoder.encode(redirectUri, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//根据%s来替换参数
String baseUrl = "https://open.weixin.qq.com/connect/qrconnect?appId=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect";
String url = String.format(
baseUrl,
utils.getAppId(),
redirectUri,
"随便你填的数"
);
//redirect重定向到指定地址
return "redirect:"+url;
}
//这个就是回调地址
@GetMapping("callback")
public String callback(String code,String state){
//拼接请求URL
String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
"?appid=%s" +
"&secret=%s" +
"&code=%s" +
"&grant_type=authorization_code";
String accessTokenUrl = String.format(
baseAccessTokenUrl,
utils.getAppId(),
utils.getAppSecret(),
code
);
//使用httpclient发送请求,获取accsess_token和openid
String accessTokenInfo = HttpClientUtils.get(accessTokenUrl);
Gson gson = new Gson();
HashMap mapAccessToken = gson.fromJson(accessTokenInfo, HashMap.class);
String access_token = mapAccessToken.get("access_token").toString();
String openid = mapAccessToken.get("openid").toString();
//判断用户是否存在
QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
wrapper.eq("openid",openid);
UcenterMember member = memberService.getOne(wrapper);
if(ObjectUtils.isEmpty(member)){
//不存在,则拼接URL,请求用户信息
String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
"?access_token=%s" +
"&openid=%s";
String userInfoUrl = String.format(
baseUserInfoUrl,
access_token,
openid
);
String userInfo = HttpClientUtils.get(userInfoUrl);
HashMap userInfoMap = gson.fromJson(userInfo, HashMap.class);
String nickname = userInfoMap.get("nickname").toString();//昵称
String headimgurl = userInfoMap.get("headimgurl").toString();//头像
Integer sex = (Integer) userInfoMap.get("sex");//性别
//保存用户信息
member = new UcenterMember();
member.setOpenid(openid);
member.setNickname(nickname);
member.setAvatar(headimgurl);
member.setSex(sex);
memberService.save(member);
}
//把用户信息加密,你们自己写一个加密和解密的接口
String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
//这边改成你的前端地址就好,把加密的信息发送的前端页面的路径上,我这里是前端获取到加密信息调用我的解密接口解密,再得到用户信息
return "redirect:https://baidu.com?token="+jwtToken;
} catch (Exception e) {
e.printStackTrace();
throw new PKException(500,"登录失败");
}
}
}
总结
里面使用jwt的部分根据自己验证用户的流程修改即可。