一 准备
1 httpclient工具类
package com.atguigu.guli.common.base.util;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @className: HttpClientUtils
* @description: http请求客户端
* @date: 2021/1/2
* @author: cakin
*/
public class HttpClientUtils {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public HttpClientUtils(String url, Map<String, String> param) {
this.url = url;
this.param = param;
}
public HttpClientUtils(String url) {
this.url = url;
}
public void setParameter(Map<String, String> map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap<String, String>();
param.put(key, value);
}
public void post() throws ClientProtocolException, IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws ClientProtocolException, IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws ClientProtocolException, IOException {
if (param != null) {
StringBuilder url = new StringBuilder(this.url);
boolean isFirst = true;
for (String key : param.keySet()) {
if (isFirst) {
url.append("?");
isFirst = false;
}else {
url.append("&");
}
url.append(key).append("=").append(param.get(key));
}
this.url = url.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
for (String key : param.keySet())
nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
}
if (xmlParam != null) {
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http) throws ClientProtocolException,
IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null)
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// 响应内容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
}
2 pom依赖
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
二 获取access_token
/**
* 功能描述:阿里云对本地的回调
*
* @param code 认证code
* @param state 认证状态
* @param session session
* @return
* @author cakin
* @date 2021/1/2
* @description:
*/
@GetMapping("callback")
public String callback(String code, String state, HttpSession session) {
System.out.println("callback被调用");
System.out.println("code:" + code);
System.out.println("state:" + state);
if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
log.error("非法回调请求");
throw new GuliException(ResultCodeEnum.ILLEGAL_CALLBACK_REQUEST_ERROR);
}
String sessionState = (String) session.getAttribute("wx_open_state");
if (!state.equals(sessionState)) {
log.error("非法回调请求");
throw new GuliException(ResultCodeEnum.ILLEGAL_CALLBACK_REQUEST_ERROR);
}
// 携带code临时票据,和appid以及appsecret请求access_token和openid(微信唯一标识)
String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
//组装参数:?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
Map<String, String> accessTokenParam = new HashMap<>();
accessTokenParam.put("appid", ucenterProperties.getAppId());
accessTokenParam.put("secret", ucenterProperties.getAppSecret());
accessTokenParam.put("code", code);
accessTokenParam.put("grant_type", "authorization_code");
HttpClientUtils client = new HttpClientUtils(accessTokenUrl, accessTokenParam);
String result;
try {
// 发送请求:组装完整的url字符串、发送请求
client.get();
// 得到响应
result = client.getContent();
System.out.println("result = " + result);
} catch (Exception e) {
log.error("获取access_token失败");
throw new GuliException(ResultCodeEnum.FETCH_ACCESSTOKEN_FAILD);
}
Gson gson = new Gson();
HashMap<String, Object> resultMap = gson.fromJson(result, HashMap.class);
// 失败的响应结果
Object errcodeObj = resultMap.get("errcode");
if (errcodeObj != null) {
Double errcode = (Double) errcodeObj;
String errmsg = (String) resultMap.get("errmsg");
log.error("获取access_token失败:" + "code:" + errcode + ", message:" + errmsg);
throw new GuliException(ResultCodeEnum.FETCH_ACCESSTOKEN_FAILD);
}
// 成功响应:解析出结果中的access_token和openid
String accessToken = (String) resultMap.get("access_token");
String openid = (String) resultMap.get("openid");
System.out.println("accessToken:" + accessToken);
System.out.println("openid:" + openid);
return null;
}
三 获取用户信息
1 根据openid查询用户是否已注册
接口
/**
* 功能描述:根据openid查询用户是否已注册
*
* @author cakin
* @date 2021/1/2
* @param openid openid
* @return Member 用户信息
*/
Member getByOpenid(String openid);
实现
/**
* 功能描述:根据openid查询用户是否已注册
*
* @param openid openid
* @return Member 用户信息
* @author cakin
* @date 2021/1/2
*/
@Override
public Member getByOpenid(String openid) {
QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("openid", openid);
return baseMapper.selectOne(queryWrapper);
}
2 根据access_token获取用户信息
/**
* 功能描述:阿里云对本地的回调
*
* @param code 认证code
* @param state 认证状态
* @param session session
* @return
* @author cakin
* @date 2021/1/2
* @description:
*/
@GetMapping("callback")
public String callback(String code, String state, HttpSession session) {
......
// 在本地数据库中查找当前微信用户的信息
Member member = memberService.getByOpenid(openid);
// 没有注册情况
if (member == null) {
// 如果当前用户不存在,则去微信的资源服务器获取用户个人信息(携带access_token)
String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
// 组装参数:?access_token=ACCESS_TOKEN&openid=OPENID
Map<String, String> baseUserInfoParam = new HashMap<>();
baseUserInfoParam.put("access_token", accessToken);
baseUserInfoParam.put("openid", openid);
client = new HttpClientUtils(baseUserInfoUrl, baseUserInfoParam);
String resultUserInfo = "";
try {
client.get();
resultUserInfo = client.getContent();
} catch (Exception e) {
log.error(ExceptionUtils.getMessage(e));
throw new GuliException(ResultCodeEnum.FETCH_USERINFO_ERROR);
}
HashMap<String, Object> resultUserInfoMap = gson.fromJson(resultUserInfo, HashMap.class);
// 失败的响应结果
errcodeObj = resultUserInfoMap.get("errcode");
if (errcodeObj != null) {
Double errcode = (Double) errcodeObj;
String errmsg = (String) resultMap.get("errmsg");
log.error("获取用户信息失败:" + "code:" + errcode + ", message:" + errmsg);
throw new GuliException(ResultCodeEnum.FETCH_USERINFO_ERROR);
}
// 解析出结果中的用户个人信息
String nickname = (String) resultUserInfoMap.get("nickname");
String avatar = (String) resultUserInfoMap.get("headimgurl");
Double sex = (Double) resultUserInfoMap.get("sex");
// 在本地数据库中插入当前微信用户的信息(使用微信账号在本地服务器注册新用户)
member = new Member();
member.setOpenid(openid);
member.setNickname(nickname);
member.setAvatar(avatar);
member.setSex(sex.intValue());
memberService.save(member);
}
// 则直接使用当前用户的信息登录(生成jwt)
// member =>Jwt
JwtInfo jwtInfo = new JwtInfo();
jwtInfo.setId(member.getId());
jwtInfo.setNickname(member.getNickname());
jwtInfo.setAvatar(member.getAvatar());
String jwtToken = JwtUtils.getJwtToken(jwtInfo, 1800);
return "redirect:http://localhost:3000?token=" + jwtToken;
}
四 前端整合
// 页面渲染之后执行:可以有window对象
mounted() {
const token = this.$route.query.token
if (token) {
// 将jwt写入cookie
cookie.set('guli_jwt_token', token, { domain: 'localhost' })
window.location.href = '/'
}
},