公司做了个移动端 应用,在此应用中需要集成一个公共产品,集成方式为:链接到此产品中的首页,类似ifram加载第三方应用页面。难点在于此公共产品集成了单点登录,所以移动端用应用要集成公共产品的页面,应首先进行单点登录。 详细说明如下:
移动端是微信小程序通过web-view加载的vue前端项目[称作:项目1],此前端项目需要集成另一个**vue**项目[称作:项目2],类似iframe链接外部网站方式。项目2集成了cas单点登录,项目1集成项目2页面时需要先单点登录。以上需求分解为以下任务:
微信小程序登录、登出逻辑放到项目1中,将小程序相关配置(appId,secret,code)传递到项目1中
项目1中加载cas登录页,实现登录逻辑、登出逻辑、路由守卫逻辑、参数缓存,涉及以下页面:
2.1 登录页,负责跳转到cas登录页
2.2 首页,发送请求到接口项目1,认证(微信openId与平台账号绑定)、获取token并缓存、获取用户信息、缓存以上信息、跳转到业务首页
项目1后端项目[称作:接口项目1]配置shiro-cas,实现单点登录后跳转到接口项目1中指定action[称作: 跳转action],跳转action负责:
3.1 获取相关配置,如加载项目2的页面地址;项目1地址
3.2 跳转到项目1,并传递参数
cas实现子系统主题,也就是项目1访问cas时,应该显示移动端的登录页,pc端访问cas时保持原有的登录页不变,实现多主题,每个子系统可以显示对应的主题登录页
经过思考设计关键流程如下:
此流程就是加载vue登录页,并传递微信小程序相关参数,话不多说,关键方法如下:
toLogin: function(e){
var _this = this;
var p = '?appid=' + appid + '&d=' + new Date().getTime() + '&secret=' + secret + '&xcxVersion=' + app.globalData.xcxVersion + '&classification=' + app.globalData.classification + '&rootpath=' + rootpath_jk;
wx.login({
success: res => {
p = p + '&code=' + res.code;
var vueUrl = rootpath + 'casLogin' + p ;
console.log(vueUrl)
_this.setData({
webviewUrl_: vueUrl
});
}
})
}
实现:
- 读取接口项目地址
- 重定向到接口项目地址
let loginPath = data.apiLoginPath + '/front/login/toIndexPage.action'
// 不同域名cookie传参失效,改为通过后台传参
// this.$cookies.set('appid', this.$route.query.appid)
// this.$cookies.set('secret', this.$route.query.secret)
// this.$cookies.set('code', this.$route.query.code)
// this.$cookies.set('xcxVersion', this.$route.query.xcxVersion)
// this.$cookies.set('classification', this.$route.query.classification)
// this.$cookies.set('knowerPath', data.knowerPath)
var p = '?sys=xsscapi&login-at=true&appid=' + this.$route.query.appid + '&secret=' + this.$route.query.secret + '&xcxVersion=' + this.$route.query.xcxVersion + '&classification=' + this.$route.query.classification + '&code=' + this.$route.query.code
console.log(loginPath + p)
document.location.href = loginPath + p
注意这里的坑:
前端vue项目的后台接口,此项目和需要集成的项目都是cas的客户端系统。此项目使用shiro做权限管理,使用shiro-cas作为cas[^apereo]
客户端,实现:
- 当未登录时,过滤器拦截将重定向到cas登录页
- 配置单点成功后回调地址,applicationContext-shiro.xml配置:略
- 回调action:读取用户信息,传递参数,重定向到vue项目首页
public String toIndexPage(HttpServletRequest request) {
String prefix = GlobalContext.getProperty("xsscwirless.url");
User user = SystemSecurityUtils.getUser();
System.out.println(user.getLoginName());
StringBuffer buffer = new StringBuffer();
buffer.append("?appid=" + request.getParameter("appid"));
buffer.append("&secret=" + request.getParameter("secret"));
buffer.append("&xcxVersion=" + request.getParameter("xcxVersion"));
buffer.append("&classification=" + request.getParameter("classification"));
buffer.append("&code=" + request.getParameter("code"));
buffer.append("&loginName=" + user.getLoginName());
buffer.append("&knowerPath=" + GlobalContext.getProperty("xsscwireless.knower.path"));
buffer.append("&casPath=" + GlobalContext.getProperty("cas.casServerUrl"));
String entryPath = prefix + "/#/casIndex" + buffer.toString();
System.out.println("--------entryPath-------" + entryPath);
return "redirect:" + entryPath;
}
什么是cas?
本需求中,改造CAS,主要实现:不同的客户端可以有不同的登录页,移动端系统访问cas时跳转移动登录页,不应用其他系统访问cas时跳转的登录页。具体实现利用cas主题机制,步骤如下:
{
"@class": "org.apereo.cas.services.RegexRegisteredService",
"serviceId": "^(https|imaps|http)://.*xsscapi/login$",
"name": "移动端",
"id": 999,
"description": "移动端登录页",
"evaluationOrder": 1,
"theme": "mobile",
"attributeReleasePolicy" : {
"@class" : "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
}
}
这里面注意serviceId的配置,移动端前端访问后端api,在没有进行单点登录的时候,api中cas客户端过滤器拦截跳转到cas,路径类似:http://XXX/xsscapi/login,此时serviceId匹配成功,跳转到mobile主题的登录页。
这里的js、images、css等静态资源都是在mobile主题登录页中使用的,如下
同时添加mobile.properties,引用静态资源
mobile.css.style=/themes/mobile/css/style.css
mobile.js.jq=/themes/mobile/js/jquery.min.js
然后在登录中(casLoginView.html)引用:
<link rel="stylesheet" th:href="@{${#themes.code('cas.myself.css.classic')}}"/>
<link rel="stylesheet" th:href="@{${#themes.code('cas.myself.css.iconfont')}}"/>
<script th:src="@{${#themes.code('cas.javascript.file')}}"></script>
<script th:src="@{${#themes.code('cas.javascript.gyss')}}"></script>
当然也可以直接引用:
<link href="../../static/themes/mobile/css/style.css">
注意登录页名称不可更改。
经过以上三步,cas的需求就实现完了。
cas跳转到回调action,此action代码已在接口项目部分中贴出,主要是构造参数,跳转移动端vue首页。
mounted () {
// loginName
var loginName = this.$route.query.loginName
if (loginName) {
this.setEnv(loginName)
// authFlag=0,待认证;authFlag=1,已认证
if (storage.get('authFlag') !== '1') {
this.authUser()
} else {
this.readUser()
}
} else {
console.log('-----单点登录失败----')
this.$router.push({ path: 'casLogin' })
}
}
注意loginName,存入了缓存中,它是进一步获取用户信息的key。此首页后面的代码是获取用户完整信息,然后跳转到业务首页。此两部分涉及具体业务,不再赘述。还有一个问题,就是vue项目的路由守卫。
解决的问题:
逻辑很简单,就是判断缓存中是否有loginName,存在即登录,next(),不存在跳转到登录页。
这部分有些坑,得从小程序说起,小程序要求业务域名必须是https协议,其通过web-view集成了vue项目,此时要求vue项目使用https地址,vue项目首先访问(redirect)其后端api,代码已在vue登录页贴出,所以要求api项目使用https地址,同理要求cas使用https地址。
https使用配置:注意api的tomcat配置,host节点增加valve
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>
</Host>
文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99
文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效
文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是
文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件
文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件
文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码
文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware
文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停
文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待
文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析
文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code
文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象