涨停复盘:这家公司因布洛芬遭爆炒,H股更是飙涨逾50%!不光药抢手,N95口罩再获关注-世界播资讯
周一共45股涨停(不含ST股),12股封板未遂,连板股总数7只。人人乐走出5连板;新华制药4连板;宁波远洋(新股上市)3连板。 一、板块异动
《使用 IdentityServer 保护 Web 应用(AntD Pro 前端 + SpringBoot 后端)》中记录了使用 IdentityServer 保护前后端的过程,其中的前端工程是以 UMI Js 为例。今天,再来记录一下使用 IdentityServer 保护 Vue 前端的过程,和 UMI Js 项目使用 umi plugin 的方式不同,本文没有使用 Vue 相关的插件,而是直接使用了 oidc-client js。
(资料图片)
另外,我对 Vue 这个框架非常不熟,在 vue-router 这里稍微卡住了一段时间,后来瞎试居然又成功了。针对这个问题,我还去 StackOverflow 上问了,但并没有收到有效的回复:https://stackoverflow.com/questions/74769607/how-to-access-vues-methods-from-navigation-guard
准备工作首先,需要在 IdentityServer 服务器端注册该 Vue 前端应用,仍然以代码写死这个客户端为例:
new Client{ClientId = "vue-client",ClientSecrets = { new Secret("vue-client".Sha256()) },ClientName = "vue client",AllowedGrantTypes = GrantTypes.Implicit,AllowAccessTokensViaBrowser = true,RequireClientSecret = false,RequirePkce = true,RedirectUris ={"http://localhost:8080/callback","http://localhost:8080/static/silent-renew.html",},AllowedCorsOrigins = { "http://localhost:8080" },AllowedScopes = { "openid", "profile", "email" },AllowOfflineAccess = true,AccessTokenLifetime = 90,AbsoluteRefreshTokenLifetime = 0,RefreshTokenUsage = TokenUsage.OneTimeOnly,RefreshTokenExpiration = TokenExpiration.Sliding,UpdateAccessTokenClaimsOnRefresh = true,RequireConsent = false,};在 Vue 工程里安装 oidc-clientyarn add oidc-client在 Vue 里配置 IdentityServer 服务器信息
在项目里添加一个 src/security/security.js文件:
import Oidc from "oidc-client"function getIdPUrl() {return "https://id6.azurewebsites.net";}Oidc.Log.logger = console;Oidc.Log.level = Oidc.Log.DEBUG;const mgr = new Oidc.UserManager({authority: getIdPUrl(),client_id: "vue-client",redirect_uri: window.location.origin + "/callback",response_type: "id_token token",scope: "openid profile email",post_logout_redirect_uri: window.location.origin + "/logout",userStore: new Oidc.WebStorageStateStore({store: window.localStorage}),automaticSilentRenew: true,silent_redirect_uri: window.location.origin + "/silent-renew.html",accessTokenExpiringNotificationTime: 10,})export default mgr在 main.js 里注入登录相关的数据和方法数据不借助任何状态管理包,直接将相关的数据添加到 Vue 的 app 对象上:
import mgr from "@/security/security";const globalData = {isAuthenticated: false,user: "",mgr: mgr}方法const globalMethods = {async authenticate(returnPath) {console.log("authenticate")const user = await this.$root.getUser();if (user) {this.isAuthenticated = true;this.user = user} else {await this.$root.signIn(returnPath)}},async getUser() {try {return await this.mgr.getUser();} catch (err) {console.error(err);}},signIn(returnPath) {returnPath ? this.mgr.signinRedirect({state: returnPath}) : this.mgr.signinRedirect();}}修改 Vue 的实例化代码new Vue({router,data: globalData,methods: globalMethods,render: h => h(App),}).$mount("#app")修改 router在 src/router/index.js中,给需要登录的路由添加 meta 字段:
Vue.use(VueRouter)const router = new VueRouter({{path: "/private",name: "private page",component: resolve => require(["@/pages/private.vue"], resolve),meta: {requiresAuth: true}}});export default router接着,正如在配置中体现出来的,需要一个回调页面来接收登录后的授权信息,这可以通过添加一个 src/views/CallbackPage.vue文件来实现:
<script>export default {async created() {try {const result = await this.$root.mgr.signinRedirectCallback();const returnUrl = result.state ?? "/";await this.$router.push({path: returnUrl})}catch(e){await this.$router.push({name: "Unauthorized"})}}}</script>Sign-in in progress... 正在登录中……
然后,需要在路由里配置好这个回调页面:
import CallbackPage from "@/views/CallbackPage.vue";Vue.use(VueRouter)const router = new VueRouter({routes: {path: "/private",name: "private page",component: resolve => require(["@/pages/private.vue"], resolve),meta: {requiresAuth: true}},{path: "/callback",name: "callback",component: CallbackPage}});export default router同时,在这个 router 里添加一个所谓的“全局前置守卫”(https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E5%85%A8%E5%B1%80%E5%89%8D%E7%BD%AE%E5%AE%88%E5%8D%AB),注意就是这里,我碰到了问题,并且在 StackOverflow 上提了这个问题。在需要调用前面定义的认证方法时,不能使用 router.app.authenticate,而要使用 router.apps[1].authenticate,这是我通过 inspect router发现的:
...router.beforeEach(async function (to, from, next) {let app = router.app.$data || {isAuthenticated: false}if(app.isAuthenticated) {next()} else if (to.matched.some(record => record.meta.requiresAuth)) {router.apps[1].authenticate(to.path).then(()=>{next()})}else {next()}})export default router到了这一步,应用就可以跑起来了,在访问 /private 时,浏览器会跳转到 IdentityServer 服务器的登录页面,在登录完成后再跳转回来。
添加 silent-renew.html注意 security.js,我们启用了 automaticSilentRenew,并且配置了 silent_redirect_uri的路径为 silent-renew.html。它是一个独立的引用了 oidc-client js 的 html 文件,不依赖 Vue,这样方便移植到任何前端项目。
oidc-client.min.js首先,将我们安装好的 oidc-client 包下的 node_modules/oidc-client/dist/oidc-client.min.js文件,复制粘贴到 public/static目录下。
然后,在这个目录下添加 public/static/silent-renew.html文件。
给 API 请求添加认证头Silent Renew Token <script src="oidc-client.min.js"></script><script>console.log("renewing tokens");new Oidc.UserManager({userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })}).signinSilentCallback();</script>
最后,给 API 请求添加上认证头。前提是,后端接口也使用同样的 IdentityServer 来保护(如果是 SpringBoot 项目,可以参考《[使用 IdentityServer 保护 Web 应用(AntD Pro 前端 + SpringBoot 后端) - Jeff Tian的文章 - 知乎](https://zhuanlan.zhihu.com/p/533197284) 》);否则,如果 API 是公开的,就不需要这一步了。
对于使用 axios 的 API 客户端,可以利用其 request interceptors,来统一添加这个认证头,比如:
import router from "../router"import Vue from "vue";const v = new Vue({router})const service = axios.create({// 公共接口--这里注意后面会讲baseURL: process.env.BASE_API,// 超时时间 单位是ms,这里设置了3s的超时时间timeout: 20 * 1000});service.interceptors.request.use(config => {const user = v.$root.user;if(user) {const authToken = user.access_token;if(authToken){config.headers.Authorization = `Bearer ${authToken}`;}}return config;}, Promise.reject)export default service关键词:
周一共45股涨停(不含ST股),12股封板未遂,连板股总数7只。人人乐走出5连板;新华制药4连板;宁波远洋(新股上市)3连板。 一、板块异动
根据最新消息,AMD三款新处理器锐龙97900、锐龙77700、锐龙57600将于CES2023上正式发布,据悉,热设计功耗仅有65W。具体细节上面,锐龙97900将
(邹立杨)12月8日,川南片区联盟基层统战工作联席会现场调研走进泸州市江阳区龙驰领秀小区,实地考察社区统战工作及统战赋能城市社区“微治理”
智通财经APP讯,双成药业发布公告,截至本公告日,公司董事、总经理JIANMINGLI先生,副总经理袁剑琳先生,副总经理张巍女士,财务总监王旭光先
截至2022年12月7日收盘,维科技术(600152)报收于17 25元,下跌6 61%,换手率15 31%,成交量75 11万手,成交额13 19亿元。12月
兴齐眼药(300573)12月01日在投资者关系平台上答复了投资者关心的问题。投资者:董秘你好,别的医药公司每个临床实验数据都会定期披露。兴齐阿
英飞拓(002528 SZ)近日走出大涨大跌的过山车行情,此前走出3连涨停创下阶段新高,今日一字板跌停,录得连续2日跌停,大幅回吐此前涨幅,现
昨日跌停的京基智农(000048 SZ)再度跌超6%,盘中低见16 5元创5个月新低,总市值失守百亿元大关。公司控股股东京基集团7月4日通过大宗交易方
党的十九届六中全会通过的《中共中央关于党的百年奋斗重大成就和历史经验的决议》(以下简称《决议》),是一篇光
深一脚浅一脚,走进一座绝壁合围的村子,探寻当地因为公路通达、蹚出了电商致富之路的奋斗轨迹;伴着汽笛轰鸣,走
图①至图⑧依次为本报重庆分社记者常碧罗(左)、青海分社记者贾丰丰(右)、湖南分社记者王云娜(右)、内蒙古分
有质量才能有分量,观众对新主流电影普遍给予较高评价,充分说明“叫好”与“叫座”之间的紧密关联“好燃!”“向
基层治理是国家治理的最末端,而基层党组织是贯彻落实党中央决策部署的“最后一公里”。《中共中央国务院关于加强
【现象】在北京冬奥会开幕式上,1 1万平方米巨幅LED地屏与场地中部的LED竖屏联动演绎,数字科技与美学理念创新融
北京信守申办时提出的节俭办赛之约,把承诺化为实践,为奥林匹克运动的可持续发展贡献了中国智慧、中国方案没有腾
Copyright 2015-2022 北方双创网 版权所有 备案号:京ICP备2021034106号-50 联系邮箱: 55 16 53 8@qq.com
英飞拓(002528.SZ)近日走出大涨大跌的过山车行情 今日一字板跌停
英飞拓(002528 SZ)近日走出大涨大跌的过山车行情,此前走出3连涨停创下阶段新高,今日一字板跌停,录得连续2日跌停,大幅回吐此前涨幅,现
京基智农(000048.SZ)再度跌超6% 总市值失守百亿元大关
昨日跌停的京基智农(000048 SZ)再度跌超6%,盘中低见16 5元创5个月新低,总市值失守百亿元大关。公司控股股东京基集团7月4日通过大宗交易方
随着全光网络越织越密 如何进一步增强消费者和产业界的“获得感”?
近年来,在5G和宽带双千兆牵引下,新项目、新试点、新应用层出不穷。随着全光网络越织越密,如何进一步增强消费者和产业界的获得感?湖北日