博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA-快速接入第三方应用登录(QQ、微信、微博)
阅读量:3932 次
发布时间:2019-05-23

本文共 7568 字,大约阅读时间需要 25 分钟。

前言:如何快速接入第三方登录?无需第三方审核认证授权,一次性接入多个平台的解决方案,

此处实现使用SpringBoot+vue 实现此效果

1.申请接入应用

在这里插入图片描述

按需选择接入的类型
在这里插入图片描述
填写应用信息

在这里插入图片描述

完成应用创建后
在这里插入图片描述

2.前端配置

引入js

因为.vue文件中 只能存在一个<script>标签,那咋办?

使用编码声明的方式将js配置成一个组件

在这里插入图片描述
上面的src就是你应用的此处地址
在这里插入图片描述
在页面中编写此代码

//引入组件 相当于 引入

出现此图标你就成功渲染成功了

但是。。。你会不会觉得这图标有点丑。想使用自己的样式

在这里插入图片描述

打开F12找到QQ的logo 查看此链接 放在你自己的图标上即可
在这里插入图片描述
在这里插入图片描述

3.后端配置

先写一下接入第三方配置

x
读取配置类
在这里插入图片描述

package com.github.ucenter.config;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/*** @author: lujie* @create: 2020/3/30* @description: ConstantPropertiesUtil**/@Componentpublic class ConstantPropertiesUtil implements InitializingBean {
@Value("${oauth.appid}") private String oauthAppId; @Value("${oauth.token}") private String oauthToken; public static String OAUTH_APPID; public static String OAUTH_TOKEN; public static String OAUTH_TYPE; @Override public void afterPropertiesSet() throws Exception {
OAUTH_APPID = oauthAppId; OAUTH_TOKEN = oauthToken; OAUTH_TYPE ="get_user_info"; }}

**创建HTTP 调用工具类 ,方便后面获取回调code 进行获取用户登录信息

**

package com.github.commonutils.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;/*** @author: lujie* @create: 2020/4/19* @description: HttpRequest**/public class HttpRequest {
/** * 向指定URL发送GET方法的请求 * * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url, String param) {
String result = ""; BufferedReader in = null; try {
String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map
> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) {
result += line; } } catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally {
try {
if (in != null) {
in.close(); } } catch (Exception e2) {
e2.printStackTrace(); } } return result; } /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) {
PrintWriter out = null; BufferedReader in = null; String result = ""; try {
URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) {
result += line; } } catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally {
try {
if (out != null) {
out.close(); } if (in != null) {
in.close(); } } catch (IOException ex) {
ex.printStackTrace(); } } return result; }}

创建微信回调接口

在这里插入图片描述

要保证你填写的这个地址能访问到你的接口
在这里插入图片描述
**

package com.github.ucenter.controller;import cn.hutool.core.util.StrUtil;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.github.commonutils.constants.CommonConstant;import com.github.commonutils.utils.HttpRequest;import com.github.commonutils.utils.JwtUtils;import com.github.ucenter.config.ConstantPropertiesUtil;import com.github.ucenter.entity.UcenterMember;import com.github.ucenter.service.UcenterMemberService;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*** @author: lujie* @create: 2020/4/19* @description: WeChatCallbackController 微信回调接口函数**/@Controller@Slf4jpublic class WeChatCallbackController {
@Resource private UcenterMemberService ucenterMemberService; @GetMapping public void WeChatCallback(@RequestParam("code") String code, HttpServletResponse response) throws IOException {
log.info("=================================第三方登录唯一标识:"+code); String baseURL = "http://open.51094.com/user/auth.html"; String params = "type=get_user_info&code="+code+"&appid="+ConstantPropertiesUtil.OAUTH_APPID+"&token="+ConstantPropertiesUtil.OAUTH_TOKEN; String result = HttpRequest.sendPost(baseURL, params); JSONObject jsonObject = JSONObject.parseObject(result); String name = jsonObject.getString("name"); String img = jsonObject.getString("img"); Integer sex = jsonObject.getInteger("sex"); String from = jsonObject.getString("from"); String openId = jsonObject.getString("uniq"); UcenterMember user =null == ucenterMemberService.getByOpenId(openId)? new UcenterMember():ucenterMemberService.getByOpenId(openId); user.setNickname(name); user.setOpenid(openId); user.setAvatar(img); user.setSex(sex); /** *新用户注册 */ if(StrUtil.isBlank(user.getId())){
user.setAge(CommonConstant.DEFAULT_AGE); ucenterMemberService.save(user); //登录 }else {
ucenterMemberService.updateById(user); } //此处跳转首页 String token = JwtUtils.getJwtToken(user.getId(),user.getNickname()); response.sendRedirect("http://127.0.0.1:3000?token="+token) ; }}

有问题在底下留言!!!啾咪~

转载地址:http://tuqgn.baihongyu.com/

你可能感兴趣的文章
HTML5认知
查看>>
Ubuntu nginx 配置https
查看>>
Thrift 操作 Hbase
查看>>
前端开发概要
查看>>
静态方法应用
查看>>
学习资料
查看>>
技术 +市场必须两手抓
查看>>
服务器抓包
查看>>
vim应用
查看>>
SOA性能管理现状
查看>>
Linux服务器常用命令
查看>>
Linux 检索
查看>>
防止机器注册
查看>>
git操作杂记
查看>>
thrift应用
查看>>
php 按元素值获取最佳元素组合
查看>>
支付服务集成-支付宝
查看>>
使用openssl生成RSA公钥和私钥对
查看>>
Linux常用命令
查看>>
Linux 定时任务应用
查看>>