微信登录
微信配置文件
# 微信开放平台 appid
wx.open.app-id=你的appid
# 微信开放平台 appsecret
wx.open.app-secret=你的secret
# 微信开放平台 重定向url
wx.open.redirect-uri=重定向url/api/user/wx/callback
配置类
package com.atguigu.yygh.user.utils; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:wx.properties") @ConfigurationProperties(prefix = "wx.open") @Data public class ConstantProperties { private String appId; private String appSecret; private String redirectUri; }
前端请求方法
//切换微信登录 weixinLogin() { this.dialogAtrr.showLoginType = 'weixin' user.getQRCodeParams().then((response) =>{ new WxLogin({ //true:手机单击确认后可以在iframe内跳转到redirecturl //false :手机单击确认登录后可以在top window 跳转到redirecturl self_redirect: false, id: 'weixinLogin', // 显示二维码的容器id appid: response.data.appid, scope: response.data.scope, redirect_uri: response.data.redirectUri, state: response.data.state, style: 'black', // 提供"black"、"white"可选。二维码的样式 href: '', // 外部css文件url,需要https }) }) },
前端api
//获得微信登录二维码的相关参数 getQRCodeParams(){ return request({ url : `/api/user/wx/getQRCodeParams`, method : `get` }) },
生成二维码以及回调函数
api文档:网站应用微信登录开发指南
package com.atguigu.yygh.user.controller.api; import com.atguigu.yygh.common.exception.YyghException; import com.atguigu.yygh.common.result.R; import com.atguigu.yygh.common.result.ResultCode; import com.atguigu.yygh.common.utils.JwtHelper; import com.atguigu.yygh.model.user.UserInfo; import com.atguigu.yygh.user.service.UserInfoService; import com.atguigu.yygh.user.utils.ConstantProperties; import com.atguigu.yygh.user.utils.HttpClientUtils; import com.google.gson.Gson; import com.sun.org.apache.regexp.internal.RE; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.apache.http.client.utils.URLEncodedUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import sun.net.util.URLUtil; import javax.net.ssl.HttpsURLConnection; import javax.servlet.http.HttpSession; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.HashMap; import java.util.UUID; @Api(tags = "微信扫码登录") @Controller //注意这里没有配置@restcontroller @RequestMapping("/api/user/wx") @Slf4j public class ApiWxController { @Autowired private ConstantProperties constantProperties; @Autowired private UserInfoService userInfoService; /** * 方法一: 在新的页面打开显示二维码 */ @GetMapping("/getQRCodeUrl") public String getQRCodeUrl(HttpSession session){ try { //处理回调url String redirectUrl = URLEncoder.encode(constantProperties.getRedirectUri(), "UTF-8"); //处理state :生成随机数,存入session String state = UUID.randomUUID().toString(); log.info("生成的state = "+ state); session.setAttribute("wx_open_state",state); //要重定向的地址 String qrcUrl = "https://open.weixin.qq.com/connect/qrconnect" + "?appid=" + constantProperties.getAppId() + "&redirect_uri=" + redirectUrl + "&response_type=code" + "&scope=snsapi_login" + "&state=" + state + "#wechat_redirect"; return "redirect:" + qrcUrl; } catch (UnsupportedEncodingException e) { throw new YyghException(ResultCode.ERROR,"生成二维码错误"); } } /** * 在内嵌窗口打开 * @param session * @return */ @GetMapping("/getQRCodeParams") @ResponseBody public R getQRCodeParams(HttpSession session){ try { String redirectUrl = URLEncoder.encode(constantProperties.getRedirectUri(), "UTF-8"); String state = UUID.randomUUID().toString(); log.info("生成的state : "+ state); session.setAttribute("wx_open_state",state); //组装好返回给前端的数据 HashMap<String, Object> map = new HashMap<>(); map.put("appid",constantProperties.getAppId()); map.put("redirectUri",redirectUrl); map.put("scope","snsapi_login"); map.put("state",state); return R.ok().data(map); } catch (UnsupportedEncodingException e) { throw new YyghException(ResultCode.ERROR,"生成二维码错误"); } }
回调
在配置文件中设置回调函数
# 微信开放平台 重定向url
wx.open.redirect_uri=http://localhost:9001/api/user/wx/callback
#在下文中背景颜色为红色
/** * 回调函数 */ @GetMapping("/callback") public String callback(String code, String state, HttpSession session) { try { String sessionState = (String)session.getAttribute("wx_open_state"); //判空操作 if(StringUtils.isEmpty(code) || StringUtils.isEmpty(state) || !state.equals(sessionState)){ throw new YyghException(ResultCode.ERROR,"回调参数错误"); } //向微信发送请求,请求获得access_tokens String accessTokenUrl ="https://api.weixin.qq.com/sns/oauth2/access_token" + "?appid=" + constantProperties.getAppId() + "&secret=" + constantProperties.getAppSecret() + "&code=" + code + "&grant_type=authorization_code"; //使用httpclient发送请求 String accessTokenInfo = HttpClientUtils.get(accessTokenUrl); //将json'转换成map 获取其中的errcode键值 ,来判断相应的成功与否 Gson gson = new Gson(); HashMap<String,Object> accessTokenInfoMap = gson.fromJson(accessTokenInfo, HashMap.class); //通过错误码是否存在来判断响应是否成功 if(accessTokenInfoMap.get("errcode") !=null ){ //errcode只要存在就说明有错误 throw new YyghException(ResultCode.ERROR,"获取access_token失败"); } //微信获取access_token成功 String openid = (String) accessTokenInfoMap.get("openid"); String accessToken = (String) accessTokenInfoMap.get("access_token"); //根据openid判断数据库中的数据是否存在 UserInfo userInfo= userInfoService.selectWxInfoByOpenId(openid); if(userInfo==null){ //用户不存在则进行注册操作 //向微信的资源服务器发送请求,获得当前用户的信息 String userInfoUrl ="https://api.weixin.qq.com/sns/userinfo" + "?access_token=" + accessToken + "&openid=" + openid; String userInfResult = HttpClientUtils.get(userInfoUrl); HashMap<String,Object> userInfResultMap = gson.fromJson(userInfResult, HashMap.class); //判断响应是否失败,同上 //通过错误码是否存在来判断响应是否成功 if(accessTokenInfoMap.get("errcode") !=null ){ //errcode只要存在就说明有错误 throw new YyghException(ResultCode.ERROR,"获取用户信息失败"); } //解析用户信息 String nickname =(String) userInfResultMap.get("nickname"); //用户注册(添加) userInfo = new UserInfo(); userInfo.setName(nickname); userInfo.setNickName(nickname); userInfo.setOpenid(openid); userInfoService.save(userInfo); }else { //用户存在则只需判断用户状态是否可用 if(userInfo.getStatus() == 0){ throw new YyghException(ResultCode.ERROR,"用户已被锁定"); } } //生成jwt字符串 String token = JwtHelper.createToken(userInfo.getId(), userInfo.getName()); //跳转到前端页面 return "redirect:http://localhost:3000/" + "?token=" + token + "&name=" + URLEncoder.encode(userInfo.getName(), "utf-8") + "&openid=" + openid + "&phone=" + userInfo.getPhone() ; } catch (Exception e) { throw new YyghException(ResultCode.ERROR,"微信登录失败",e); } } }
微信用户可获得的数据
工具类
package com.atguigu.yygh.user.utils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; 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.conn.ssl.X509HostnameVerifier; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import java.io.IOException; import java.net.SocketTimeoutException; import java.security.GeneralSecurityException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 依赖的jar包有:commons-lang-2.6.jar、httpclient-4.3.2.jar、httpcore-4.3.1.jar、commons-io-2.4.jar * @author zhaoyb * */ public class HttpClientUtils { public static final int connTimeout=10000; public static final int readTimeout=10000; public static final String charset="UTF-8"; private static HttpClient client = null; static { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(128); cm.setDefaultMaxPerRoute(128); client = HttpClients.custom().setConnectionManager(cm).build(); } public static String postParameters(String url, String parameterStr) throws ConnectTimeoutException, SocketTimeoutException, Exception{ return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout); } public static String postParameters(String url, String parameterStr,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception{ return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout); } public static String postParameters(String url, Map<String, String> params) throws ConnectTimeoutException, SocketTimeoutException, Exception { return postForm(url, params, null, connTimeout, readTimeout); } public static String postParameters(String url, Map<String, String> params, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { return postForm(url, params, null, connTimeout, readTimeout); } public static String get(String url) throws Exception { return get(url, charset, null, null); } public static String get(String url, String charset) throws Exception { return get(url, charset, connTimeout, readTimeout); } /** * 发送一个 Post 请求, 使用指定的字符集编码. * * @param url * @param body RequestBody * @param mimeType 例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3 * @param charset 编码 * @param connTimeout 建立链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒. * @return ResponseBody, 使用指定的字符集编码. * @throws ConnectTimeoutException 建立链接超时异常 * @throws SocketTimeoutException 响应超时 * @throws Exception */ public static String post(String url, String body, String mimeType,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { HttpClient client = null; HttpPost post = new HttpPost(url); String result = ""; try { if (StringUtils.isNotBlank(body)) { HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset)); post.setEntity(entity); } // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { customReqConf.setSocketTimeout(readTimeout); } post.setConfig(customReqConf.build()); HttpResponse res; if (url.startsWith("https")) { // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(post); } else { // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(post); } result = IOUtils.toString(res.getEntity().getContent(), charset); } finally { post.releaseConnection(); if (url.startsWith("https") && client != null&& client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } } return result; } /** * 提交form表单 * * @param url * @param params * @param connTimeout * @param readTimeout * @return * @throws ConnectTimeoutException * @throws SocketTimeoutException * @throws Exception */ public static String postForm(String url, Map<String, String> params, Map<String, String> headers, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { HttpClient client = null; HttpPost post = new HttpPost(url); try { if (params != null && !params.isEmpty()) { List<NameValuePair> formParams = new ArrayList<NameValuePair>(); Set<Entry<String, String>> entrySet = params.entrySet(); for (Entry<String, String> entry : entrySet) { formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); post.setEntity(entity); } if (headers != null && !headers.isEmpty()) { for (Entry<String, String> entry : headers.entrySet()) { post.addHeader(entry.getKey(), entry.getValue()); } } // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { customReqConf.setSocketTimeout(readTimeout); } post.setConfig(customReqConf.build()); HttpResponse res = null; if (url.startsWith("https")) { // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(post); } else { // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(post); } return IOUtils.toString(res.getEntity().getContent(), "UTF-8"); } finally { post.releaseConnection(); if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } } } /** * 发送一个 GET 请求 * * @param url * @param charset * @param connTimeout 建立链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒. * @return * @throws ConnectTimeoutException 建立链接超时 * @throws SocketTimeoutException 响应超时 * @throws Exception */ public static String get(String url, String charset, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,SocketTimeoutException, Exception { HttpClient client = null; HttpGet get = new HttpGet(url); String result = ""; try { // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { customReqConf.setSocketTimeout(readTimeout); } get.setConfig(customReqConf.build()); HttpResponse res = null; if (url.startsWith("https")) { // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(get); } else { // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(get); } result = IOUtils.toString(res.getEntity().getContent(), charset); } finally { get.releaseConnection(); if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } } return result; } /** * 从 response 里获取 charset * * @param ressponse * @return */ @SuppressWarnings("unused") private static String getCharsetFromResponse(HttpResponse ressponse) { // Content-Type:text/html; charset=GBK if (ressponse.getEntity() != null && ressponse.getEntity().getContentType() != null && ressponse.getEntity().getContentType().getValue() != null) { String contentType = ressponse.getEntity().getContentType().getValue(); if (contentType.contains("charset=")) { return contentType.substring(contentType.indexOf("charset=") + 8); } } return null; } /** * 创建 SSL连接 * @return * @throws GeneralSecurityException */ private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException { try { 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, new X509HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } @Override public void verify(String host, SSLSocket ssl) throws IOException { } @Override public void verify(String host, X509Certificate cert) throws SSLException { } @Override public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { } }); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (GeneralSecurityException e) { throw e; } } public static void main(String[] args) { try { String str= post("https://localhost:443/ssl/test.shtml","name=12&page=34","application/x-www-form-urlencoded", "UTF-8", 10000, 10000); //String str= get("https://localhost:443/ssl/test.shtml?name=12&page=34","GBK"); /*Map<String,String> map = new HashMap<String,String>(); map.put("name", "111"); map.put("page", "222"); String str= postForm("https://localhost:443/ssl/test.shtml",map,null, 10000, 10000);*/ System.out.println(str); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }