1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Spring Boot + 微信公众号授权登录获取用户信息

Spring Boot + 微信公众号授权登录获取用户信息

时间:2024-04-18 09:13:02

相关推荐

Spring Boot + 微信公众号授权登录获取用户信息

通过微信公众平台的官方文档,总结出网页授权流程分为:

1、引导用户进入授权页面同意授权,获取code

2、通过code换取网页授权access_token(与基础支持中的access_token不同)

3、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

简单总结成代码就是:

1、controller

@RestController@RequestMapping("/wxAuth")public class WxLoginController {@RequestMapping("/login")public void wxLogin(HttpServletResponse response) throws IOException {//请求获取code的回调地址//用线上环境的域名或者用内网穿透,不能用ipString callBack = "http://你的域名/wxAuth/callBack";//请求地址String url = "https://open./connect/oauth2/authorize" +"?appid=" + 你的appid +"&redirect_uri=" + URLEncoder.encode(callBack) +"&response_type=code" +"&scope=snsapi_userinfo" +"&state=STATE#wechat_redirect";//重定向response.sendRedirect(url);}//回调方法@RequestMapping("/callBack")public void wxCallBack(HttpServletRequest request,HttpServletResponse response) throws IOException {String code = request.getParameter("code");//获取access_tokenString url = "https://api./sns/oauth2/access_token" +"?appid=" + appId +"&secret=" + appSecret +"&code=" + code +"&grant_type=authorization_code";String result = HttpClientUtil.doGet(url);System.out.println("请求获取access_token:" + result);//返回结果的json对象JSONObject resultObject = JSON.parseObject(result);//请求获取userInfoString infoUrl = "https://api./sns/userinfo" +"?access_token=" + resultObject.getString("access_token") +"&openid=" + resultObject.getString("openid") +"&lang=zh_CN";String resultInfo = HttpClientUtil.doGet(infoUrl);//此时已获取到userInfo,再根据业务进行处理System.out.println("请求获取userInfo:" + resultInfo);}}

二、用到的HttpClientUtil

public class HttpClientUtil {public static String doGet(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}public static String doGet(String url) {return doGet(url, null);}public static String doPost(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (param != null) {List<NameValuePair> paramList = new ArrayList<>();for (String key : param.keySet()) {paramList.add(new BasicNameValuePair(key, param.get(key)));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}public static String doPost(String url) {return doPost(url, null);}public static String doPostJson(String url, String json) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建请求内容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}}

三、引入的pom

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.7</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.5</version></dependency>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。