1、微信扫描登录流程时序图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a9fe7a50942553318759a3bbfce5fe1a.png#pic_center)
2、注册微信开发者并且拿到appId、appSecret、redirectUrl
3、导入maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
4、在application.properties中添加wx相关的配置参数
# 微信开放平台配置参数
wx.open.appId=wxed9954c01bb89b47
wx.open.appSecret=a7482517235173ddb4083788de60b90e
wx.open.redirectUrl=http://localhost:8160/api/ucenter/wx/callback
5、创建常量类
package com.tuzhi.ucenter_service.constants;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
public class WxOpenConstants implements InitializingBean {
@Value("${wx.open.appId}")
private String appId;
@Value("${wx.open.appSecret}")
private String appSecret;
@Value("${wx.open.redirectUrl}")
private String redirectUrl;
public static String WX_OPEN_APP_ID;
public static String WX_OPEN_APP_SECRET;
public static String WX_OPEN_REDIRECT_URL;
@Override
public void afterPropertiesSet() throws Exception {
WX_OPEN_APP_ID = appId;
WX_OPEN_APP_SECRET = appSecret;
WX_OPEN_REDIRECT_URL = redirectUrl;
}
}
6、使用
package com.tuzhi.ucenter_service.controller;
import com.google.gson.Gson;
import com.tuzhi.base_service.exceptionhandler.GuiException;
import com.tuzhi.ucenter_service.constants.WxOpenConstants;
import com.tuzhi.ucenter_service.pojo.UcenterMember;
import com.tuzhi.ucenter_service.service.UcenterMemberService;
import com.tuzhi.ucenter_service.util.HttpClientUtils;
import com.tuzhi.utilcommon.JwtUtils;
import com.tuzhi.utilcommon.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Objects;
@Api(tags = "微信扫描接口")
@CrossOrigin
@Controller
@RequestMapping("/api/ucenter/wx")
public class WxApiController {
@Autowired
UcenterMemberService ucenterMemberService;
@ApiOperation("生成二维码")
@GetMapping("/login")
public String getWxCode(){
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 redirectUrl = WxOpenConstants.WX_OPEN_REDIRECT_URL;
try {
redirectUrl = URLEncoder.encode(redirectUrl, "utf-8");
}catch(Exception e) {
}
String url = String.format(
baseUrl,
WxOpenConstants.WX_OPEN_APP_ID,
redirectUrl,
"tuzhi"
);
return "redirect:"+url;
}
@GetMapping("/callback")
public String callBack(String code,String state){
try {
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,
WxOpenConstants.WX_OPEN_APP_ID,
WxOpenConstants.WX_OPEN_APP_SECRET,
code
);
String accessTokenInfo = HttpClientUtils.get(accessTokenUrl);
Gson gson = new Gson();
HashMap<String,String> accessTokenMap = gson.fromJson(accessTokenInfo, HashMap.class);
String accessToken = accessTokenMap.get("access_token");
String openid = accessTokenMap.get("openid");
UcenterMember member = ucenterMemberService.getMemberByOpenid(openid);
if(Objects.isNull(member)){
String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
"?access_token=%s" +
"&openid=%s";
String userInfoUrl = String.format(
baseUserInfoUrl,
accessToken,
openid
);
String userInfo = HttpClientUtils.get(userInfoUrl);
HashMap<String, String> userInfoMap = gson.fromJson(userInfo, HashMap.class);
String nickname = userInfoMap.get("nickname");
String headimgurl = userInfoMap.get("headimgurl");
member = new UcenterMember();
member.setOpenid(openid);
member.setNickname(nickname);
member.setAvatar(headimgurl);
ucenterMemberService.save(member);
}
String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
return "redirect:http://localhost:3000?token="+token;
} catch (Exception e) {
e.printStackTrace();
throw GuiException.from(ResultCode.LOGIN_ERROR);
}
}
}
返回首页面,通过路径传递token字符串
return "redirect:http://localhost:3000?token="+token;
} catch (Exception e) {
e.printStackTrace();
throw GuiException.from(ResultCode.LOGIN_ERROR);
}
}
}