环境:springboot+themeleaf
首先创建两个页面,以供显示:
weibo.html页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<div style="padding: 40px">
<font color="black"><strong>微博扫码登录测试</strong></font>
<br/><br/>
<a href="https://api.weibo.com/oauth2/authorize?client_id=1694268747&response_
type=code&redirect_uri=http://127.0.0.1:8080/account/login">微博登录</a>
</a>
</div>
</body>
</html>
success.html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h1>授权成功</h1>
用户名:<span th:text="${userName}"></span><br>
个性签名:<span th:text="${description}"></span><br>
性别:<span th:text="${gender}"></span><br>
<br>
头像:<img th:src="${userImage}" alt="">
</body>
</html>
导入maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
#模板引擎配置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/html/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=utf-8
server.port=8080
编写工具类:weixinUtil.java
package kgc.tools;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
/**
* @Author AK47007
* @Date 2019/7/22 22:15
* Describe: 发送 Http请求
*/
public class weiXinUtil {
/**
* 发送POST请求
* @param url 请求的接口路径
* @param params 参数
* @return
* @throws IOException
*/
public static String post(String url, Map<String, String> params) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
StringBuilder stringBuilder = new StringBuilder(url);
//第一个参数
stringBuilder.append("?client_id=");
stringBuilder.append(params.get("client_id"));
//第二个参数
stringBuilder.append("&client_secret=");
stringBuilder.append(params.get("client_secret"));
//第三个参数
stringBuilder.append("&grant_type=");
stringBuilder.append(params.get("grant_type"));
//第四个参数
stringBuilder.append("&code=");
stringBuilder.append(params.get("code"));
//第五个参数
stringBuilder.append("&redirect_uri=");
stringBuilder.append(params.get("redirect_uri"));
HttpPost httpPost = new HttpPost(stringBuilder.toString());
//发送请求返回响应的信息
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}
return null;
}
/**
* GET请求
*
* @param url 请求的接口路径
* @param access_token 授权码
* @param uid 用户ID
* @return
* @throws IOException
*/
public static String get(String url, Object access_token, Object uid) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
StringBuilder stringBuilder = new StringBuilder(url);
//第一个参数
stringBuilder.append("?access_token=");
stringBuilder.append(access_token);
//第二个参数
stringBuilder.append("&uid=");
stringBuilder.append(uid);
HttpGet httpGet = new HttpGet(stringBuilder.toString());
//发送请求返回响应的信息
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}
return null;
}
}
编写控制层:AccountController.java
package kgc.controller;
import com.alibaba.fastjson.JSONObject;
import kgc.tools.weiXinUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RequestMapping(value = “account”)
@Controller
public class AccountController {
@RequestMapping("/index")
public String index() {
return “weibo”;
}
@RequestMapping(value = “login”)
public String callback(String code, Model model) throws Exception {
Map<String, String> params = new HashMap<>(5);
//不知道url填什么可以看文档:https://open.weibo.com/wiki/Oauth2/access_token
String url = “https://api.weibo.com/oauth2/access_token”;
//申请应用时分配的AppKey
params.put(“client_id”, “填写你自己的”);
//申请应用时分配的AppSecret
params.put(“client_secret”, “填写你自己的”);
//请求的类型,填写authorization_code
params.put(“grant_type”, “authorization_code”);
//调用authorize获得的code值
params.put(“code”, code);
//回调地址,需需与注册应用里的回调地址一致。
params.put(“redirect_uri”, “填写你自己的回调地址”);
String result = weiXinUtil.post(url, params);
System.out.println(“得到的结果为:” + result);
System.out.println(“得到的code为:” + code);
JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
url = “https://api.weibo.com/2/users/show.json”;
String getUserInfo = weiXinUtil.get(url, jsonObject.get(“access_token”), jsonObject.get(“uid”));
System.out.println(“得到的用户信息为:” + getUserInfo);
jsonObject = (JSONObject) JSONObject.parse(getUserInfo);
model.addAttribute(“userName”, jsonObject.get(“screen_name”));
model.addAttribute(“description”, jsonObject.get(“description”));
model.addAttribute(“gender”, jsonObject.get(“gender”));
model.addAttribute(“userImage”, jsonObject.get(“profile_image_url”));
return “success”;
}
}