当前位置: 首页 > news >正文

三点水网站建设2023免费网站推广大全

三点水网站建设,2023免费网站推广大全,做电信网站运营,建设网站涉及的技术实现过程 去第三方平台拿到client-id和client-secret,并配置一个能够外网访问回调地址redirect-uri供第三方服务回调搭建后端服务,引入justauth-spring-boot-starter直接在配置文件中定义好第一步的三个参数,并提供获取登录页面的接口和回调…

实现过程

  1. 去第三方平台拿到client-id和client-secret,并配置一个能够外网访问回调地址redirect-uri供第三方服务回调
  2. 搭建后端服务,引入justauth-spring-boot-starter直接在配置文件中定义好第一步的三个参数,并提供获取登录页面的接口和回调接口
  3. 前端项目中新建一个登录窗口和一个登录中转页面,登录窗口的url从第二步第一个接口获取,中转页面从第二步的第二个接口返回
  4. 中转页面从url中读取登录成功的用户信息并存放到pinia中,关闭登录窗口并刷新主窗口

1,必要信息获取

第三方平台的client-id和client-secret一般注册开发者平台都能获取。
回调地址需要外网,可以使用花生壳内网穿透随便搞一个,映射到本地的后台服务端口,当后天服务启动成功后确保连接成功
在这里插入图片描述
前端代理也可以直接代理到这个域名,前后端完全分离

2,后台服务搭建

2.1 后台如果使用springboot2.x可以从开源框架直接使用:

https://gitee.com/justauth/justauth-spring-boot-starter
只需将上一步获取的三个参数配置到yml文件中

2.2 AuthRequestFactory错误

如果使用的springboot3.x,可能会报错提示:

‘com.xkcoding.justauth.AuthRequestFactory’ that could not be found.

只需要将AuthRequestFactory、JustAuthProperties、AuthStateRedisCache从源码复制一份到项目中,补全@Configuration、@Component,然后补上一个Bean即可

    @Beanpublic AuthRequestFactory getAuthRequest(JustAuthProperties properties, AuthStateRedisCache authStateCache) {return new AuthRequestFactory(properties,authStateCache);}
2.3 redis错误

justauth-spring-boot-starter项目中的redis配置是springboot2.x的配置,
如果是3.x的项目需要将 spring:reids改为 spring:data:reids

2.4 代码案例
import com.alibaba.fastjson.JSONObject;
import io.geekidea.springboot.cache.AuthRequestFactory;
import io.geekidea.springboot.service.UserService;
import io.geekidea.springboot.vo.ResponseResult;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import io.geekidea.springboot.cache.JustAuthProperties;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthBaiduRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** @Description https://blog.csdn.net/weixin_46684099/article/details/118297276* @Date 2024/10/23 16:30* @Author 余乐**/
@Slf4j
@Controller
@RequestMapping("/oauth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class JustAuthController {private final UserService userService;private final AuthRequestFactory factory;private final JustAuthProperties properties;@GetMappingpublic List<String> list() {return factory.oauthList();}@RequestMapping("/render/{source}")@ResponseBodypublic ResponseResult renderAuth(@PathVariable("source") String source) {AuthRequest authRequest = null;//特定平台需要自定义参数的可以单独写AuthConfigif ("baidu".equals(source)) {//百度账号默认只有basic,需要网盘权限需要单独定义List<String> list = new ArrayList<>();list.add("basic");list.add("netdisk");Map<String,AuthConfig> configMap = properties.getType();AuthConfig authConfig = configMap.get("BAIDU");authConfig.setScopes(list);authRequest = new AuthBaiduRequest(authConfig);} else {//其他平台账号登录authRequest = factory.get(source);}String state = AuthStateUtils.createState();String authorizeUrl = authRequest.authorize(state);return ResponseResult.success(authorizeUrl);}/*** oauth平台中配置的授权回调地址,以本项目为例,在创建github授权应用时的回调地址应为:http://127.0.0.1:8444/oauth/callback/github*/@RequestMapping("/callback/{source}")public void login(@PathVariable("source") String source, AuthCallback callback, HttpServletResponse response2) throws IOException {log.info("进入callback:{},callback params:{}", source, JSONObject.toJSONString(callback));AuthRequest authRequest = null;//特定平台需要自定义参数的可以单独写AuthConfigif ("baidu".equals(source)) {//百度账号默认只有basic,需要网盘权限需要单独定义List<String> list = new ArrayList<>();list.add("basic");list.add("netdisk");Map<String,AuthConfig> configMap = properties.getType();AuthConfig authConfig = configMap.get("BAIDU");authConfig.setScopes(list);authRequest = new AuthBaiduRequest(authConfig);} else {//其他平台账号登录authRequest = factory.get(source);}AuthResponse<AuthUser> response = authRequest.login(callback);String userInfo = JSONObject.toJSONString(response.getData());log.info("回调用户信息:{}", userInfo);if (response.ok()) {userService.save(response.getData());String userInfoParam = URLEncoder.encode(userInfo, "UTF-8");//将用户信息放到中转页面的路由参数中,前端从路由参数获取登陆结果response2.sendRedirect("http://localhost:5173/loginback?data=" + userInfoParam);}}/*** 注销登录 (前端需要同步清理用户缓存)** @param source* @param uuid* @return* @throws IOException*/@RequestMapping("/revoke/{source}/{uuid}")@ResponseBodypublic ResponseResult revokeAuth(@PathVariable("source") String source, @PathVariable("uuid") String uuid) throws IOException {AuthRequest authRequest = factory.get(source.toLowerCase());AuthUser user = userService.getByUuid(uuid);if (null == user) {return ResponseResult.fail("用户不存在");}AuthResponse<AuthToken> response = null;try {response = authRequest.revoke(user.getToken());if (response.ok()) {userService.remove(user.getUuid());return ResponseResult.success("用户 [" + user.getUsername() + "] 的 授权状态 已收回!");}return ResponseResult.fail("用户 [" + user.getUsername() + "] 的 授权状态 收回失败!" + response.getMsg());} catch (AuthException e) {return ResponseResult.fail(e.getErrorMsg());}}/*** 刷新token** @param source* @param uuid* @return*/@RequestMapping("/refresh/{source}/{uuid}")@ResponseBodypublic ResponseResult<String> refreshAuth(@PathVariable("source") String source, @PathVariable("uuid") String uuid) {AuthRequest authRequest = factory.get(source.toLowerCase());AuthUser user = userService.getByUuid(uuid);if (null == user) {return ResponseResult.fail("用户不存在");}AuthResponse<AuthToken> response = null;try {response = authRequest.refresh(user.getToken());if (response.ok()) {user.setToken(response.getData());userService.save(user);return ResponseResult.success("用户 [" + user.getUsername() + "] 的 access token 已刷新!新的 accessToken: " + response.getData().getAccessToken());}return ResponseResult.fail("用户 [" + user.getUsername() + "] 的 access token 刷新失败!" + response.getMsg());} catch (AuthException e) {return ResponseResult.fail(e.getErrorMsg());}}
}

3 新建登录窗口和中转页面

3.1 在src/main/index.ts中新增登录窗口
let loginWindow//监听打开登录窗口的事件
ipcMain.on('openLoginWin', (event, url) => {console.log('打开登录窗口', url)createLoginWindow(url)
})// 创建登录窗口
function createLoginWindow(url: string) {loginWindow = new BrowserWindow({width: 800,height: 600,frame: false,titleBarStyle: 'hidden',   autoHideMenuBar: true,parent: mainWindow,   //父窗口为主窗口modal: true,show: false,webPreferences: {preload: join(__dirname, '../preload/index.js'),nodeIntegration: true,contextIsolation: true}})// 加载登录 URLloginWindow.loadURL(url)loginWindow.on('ready-to-show', () => {loginWindow.show()})
}// 关闭登录窗口并刷新主窗口
ipcMain.handle('close-login', () => {if (loginWindow) {loginWindow.close()}if (mainWindow) {mainWindow.reload() // 刷新主窗口 }}
})
3.2 新增中转页面并配置路由

@/views/setting/LoginBack.vue

<template><el-row justify="center"><cl-col :span="17"><h2>登陆结果</h2><el-icon style="color:#00d28c;font-size: 50px"><i-mdi-check-circle /></el-icon></cl-col></el-row>
</template><script setup lang="ts">
import { useRoute } from 'vue-router'
import { onMounted } from 'vue'
import { useThemeStore } from '@/store/themeStore'const route = useRoute()
const data = route.query.data
const themeStore = useThemeStore()
//登陆成功自动关闭窗口
onMounted(() => {console.log("登陆结果",data)themeStore.setCurrentUser(JSON.parse(data))setTimeout(() => {//关闭当前登录回调的窗口,并且刷新主窗口页面window.electron.ipcRenderer.invoke('close-login')}, 1000)
})
</script>
3.3 新增路由
{path: 'loginback', component: ()=>import("@/views/setting/LoginBack.vue"),},

这里的路由对应的就是后台/callback 接口重定向的地址

4.管理用户登录信息

后端用户登录信息保存在redis中,如果过期可以使用客户端中缓存的用户uuid刷新token

前端的一般是使用pinia做持久化维护,安装piniad 插件
pinia-plugin-persistedstate
新增用户themeStore.ts

import { defineStore } from 'pinia';export const useThemeStore = defineStore('userInfoStore', {state: () => {// 从 localStorage 获取主题,如果没有则使用默认值//const localTheme = localStorage.getItem('localTheme') || 'cool-black';return {currentTheme: 'cool-black',userInfo: {}};},actions: {setCurrentThemeId(theme: string) {console.log("修改主题", theme);this.currentTheme = theme; // 更新当前主题document.body.setAttribute('data-theme', theme); // 更新 data-theme},setCurrentUser(user: any) {console.log("修改账号", user);this.userInfo = user; // 更新当前账号},},//开启持久化 = 》 localStoragepersist: {key: 'userInfoStore',onstorage: localStorage,path: ['currentTheme','userInfo']}
});

5. 运行调试

5.1 在顶部登录页面
<div v-if="userInfo.avatar"><el-avatar :src="userInfo.avatar" :size="30"/><el-popover :width="300" trigger="click"><template #reference><p>{{userInfo.nickname}}</p></template><template #default><div  class="demo-rich-conent" style="display: flex; gap: 16px; flex-direction: column"><el-avatar:size="60"src="https://avatars.githubusercontent.com/u/72015883?v=4"style="margin-bottom: 8px"/><el-divider /><h5 @click="logout(userInfo.uuid)">退出登录</h5></div></template></el-popover></div><div v-else @click.stop="openLoginCard"><el-avatar :icon="UserFilled" :size="30"/><p>未登录</p></div><script lang="ts" setup>
import {ref} from 'vue'
import { LoginOut } from '@/api/baidu'
import {useThemeStore} from "@/store/themeStore";
import { UserFilled } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { getLoginPageUrl } from '../../api/baidu'
const themeStore = useThemeStore();
const router = useRouter()
let searchVal = ref('')
let userInfo=ref({})if (themeStore.userInfo){userInfo.value = themeStore.userInfo
}
//打开登录弹窗
function openLoginCard(){getLoginPageUrl().then(resp => {console.log("获取登陆地址",resp.data)window.electron.ipcRenderer.send('openLoginWin',resp.data.data)});
}
//退出登录
function logout(uuid:string){LoginOut(uuid).then(resp => {console.log("注销登录",resp.data)themeStore.setCurrentUser({})window.location.reload()});
}
</script>

在这里插入图片描述
在这里插入图片描述

http://www.hrbkazy.com/news/18438.html

相关文章:

  • 新手学做网站 视频百度网盘如何网络推广新产品
  • 做网站广告网页个人网站该怎么打广告
  • 自己做视频网站线上推广渠道有哪些方式
  • 长沙网站建设0731网站推广模式
  • 深圳网站建设制作设计平台seo基础入门教程
  • 多多进宝怎么做自己网站百度指数在线查询
  • app网站公司网站优化seo培训
  • 高端建站模版seo综合查询是什么
  • 企业网站banner素材关键词歌词任然
  • b2c网站开发文档竞价排名名词解释
  • 拉销智能模板建站系统链接推广
  • lovephoto wordpress优化大师tv版
  • 建设银行网站卡死海南网站制作
  • 品牌网站如何做网络营销课程设计
  • 做网站销售门窗怎么做公司想做个网站怎么办
  • 重庆做网站外包公司哪里可以买链接网站
  • php网站开发视频教程下载专业的seo搜索引擎优化培训
  • 政府网站成为建设今日头条权重查询
  • 服务外包网站seo技术培训东莞
  • 做交易网站蚌埠网络推广
  • 企业做网站系统网络培训心得体会
  • 网页制作公司网站怎么注册自己公司的网址
  • 网站分析与优化seo排名查询
  • 建立网站时服务器的基本配置有哪些营销软文网站
  • 湖北网站建设多少钱360推广怎么收费
  • 长沙做网站最好的公司有哪些外链代发公司
  • 服装网站建设费用分析百度怎么打广告
  • 邹城网站设计网站域名ip查询
  • 政务网站开发网站软文推广范文
  • 社交网站推广怎么做安徽百度关键词优化