微博实现第三方登录

环境:springboot+themeleaf

首先创建两个页面,以供显示:
weibo.html页面




    


微博扫码登录测试

微博登录

success.html页面




    
    title


授权成功

用户名:
个性签名:
性别:

头像:

导入maven依赖

      
            org.apache.httpcomponents
            httpclient
            4.5.6
        
        
      
        com.alibaba
        fastjson
        1.2.58
    

#模板引擎配置

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 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 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”;
}
}

你可能感兴趣的:(技术)