集成第三方登录并部署

集成第三方登录并部署

一、部署项目到云服务器
二、接入微博登录

1、购买一台云服务器(阿里云)
云服务器
2、使用宝塔面板绑定云服务器(宝塔确实好用)
宝塔面板
3、使用宝塔面板给云服务器下载Apache等
下载
4、登录云服务器下载个JDK(第一种挺快的)
Linux系统下安装jdk及环境配置(两种方法)

5、配置云服务器的安全组,并吧云服务器加入到配置好的安全组
安全组
安全组配置
加入安全组

6、创建一个SpringBoot项目放到云服务器(使用宝塔面板部署项目更快)

部署项目
7、打开云服务器防火墙的端口对应的端口(我部署的项目端口为8080)

查询已开放的端口(已开放的端口号集合):firewall-cmd --zone=public --list-ports
查询指定端口是否已开:firewall-cmd --query-port=8080/tcp
打开端口指定端口:firewall-cmd --zone=public --add-port=8080/tcp --permanent
重新载入防火墙(开放端口之后必须重新载入,不然无效):firewall-cmd --reload

打开端口

8、访问部署好的项目(项目部署成功)
访问项目

使用宝塔面板安装的Mysql(8.0.24)的配置文件在:/etc/my.cnf

二、接入微博登录

1、注册成为微博的开发者(跟着填就行):https://open.weibo.com

主要是 App KeyApp Secret 以及 授权回调页取消授权回调页

信息

2、开始开发

1、在前端页面放置跳转到微博授权页面的超链接(点击登录就会跳转大授权页面):
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

登录
在这里插入图片描述

2、用户授权之后就会跳转第一步填写的回调地址并携带code,然后继续发送请求获取access_token值

@RequestMapping("/success")
    public String success(HttpServletRequest request) throws IOException {
        String code = request.getParameter("code");
        String url = "https://api.weibo.com/oauth2/access_token?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri=" + REDIRECT_URI + "&code=" + code;
        System.out.println(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpGet = new HttpPost(url);
        String content = null;
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            //响应体内容
            content = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
        System.out.println(content);
        WeiBoTokenDTO weiBoTokenDTO = null;
        if (content == null) {
            return "fail";
        }
        weiBoTokenDTO = JSON.parseObject(content, WeiBoTokenDTO.class);
        if (weiBoTokenDTO == null) {
            return "fail";
        }
        request.getSession().setAttribute("weiBoToken", weiBoTokenDTO);
        return "success";
    }

3、最后,有了access_token之后就可以调用微博的其他接口了:

例如:查询用户access_token的授权相关信息 (oauth2/get_token_info)

@ResponseBody
    @RequestMapping(value = "/get_token_info")
    public String get_token_info(HttpServletRequest request) throws IOException {
        WeiBoTokenDTO weiBoTokenDTO = (WeiBoTokenDTO) request.getSession().getAttribute("weiBoToken");
        System.out.println(weiBoTokenDTO);
        if (weiBoTokenDTO == null){
            return "weiBoToken为空";
        }
        String url = "https://api.weibo.com/oauth2/get_token_info";
        //模拟普通表单提交时使用
        MultiValueMap<String, String> parameter = new LinkedMultiValueMap<>();
        parameter.add("access_token",weiBoTokenDTO.getAccess_token());
        JSONObject param = new JSONObject();
        param.put("access_token",weiBoTokenDTO.getAccess_token());
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/x-www-form-urlencoded;charset=UTF-8"));
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(parameter, headers);
        // 执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        String content;
        if (response.getStatusCodeValue() == 200) {
            //响应体内容
            content = response.getBody();
        }else {
            return response.toString();
        }
        System.out.println(content);
        return content;
    }

官方文档:https://open.weibo.com/wiki/微博API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值