100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JAVA-快速接入第三方应用登录(QQ 微信 微博)

JAVA-快速接入第三方应用登录(QQ 微信 微博)

时间:2020-04-06 23:28:51

相关推荐

JAVA-快速接入第三方应用登录(QQ 微信 微博)

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

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

1.申请接入应用

/

按需选择接入的类型

填写应用信息

完成应用创建后

2.前端配置

引入js

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

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

上面的src就是你应用的此处地址

在页面中编写此代码

//引入组件 相当于 引入<script> 引用js<scriptLink></scriptLink>//第三方应用图标渲染的位置<span id="hzy_fast_login"></span>

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

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

打开F12找到QQ的logo 查看此链接 放在你自己的图标上即可

3.后端配置

先写一下接入第三方配置

读取配置类

package com.github.ucenter.config;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Value;import org.ponent;/*** @author: lujie* @create: /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;@Overridepublic void afterPropertiesSet() throws Exception {OAUTH_APPID = oauthAppId;OAUTH_TOKEN = oauthToken;OAUTH_TYPE ="get_user_info";}}

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

**

package monutils.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .URL;import .URLConnection;import java.util.List;import java.util.Map;/*** @author: lujie* @create: /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<String, List<String>> 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 monConstant;import monutils.utils.HttpRequest;import monutils.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: /4/19* @description: WeChatCallbackController 微信回调接口函数**/@Controller@Slf4jpublic class WeChatCallbackController {@Resourceprivate UcenterMemberService ucenterMemberService;@GetMappingpublic void WeChatCallback(@RequestParam("code") String code, HttpServletResponse response) throws IOException {log.info("=================================第三方登录唯一标识:"+code);String baseURL = "/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) ;}}

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

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