CAS 实现站内单点登录及实现第三方 OAuth、OpenId 登录(一)

一、CAS 介绍

    CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目。CAS 具有以下特点:

开源的企业级单点登录解决方案
CAS Server 为需要独立部署的 Web 应用
CAS Client 支持非常多的客户端(这里指单点登录系统中的各个 Web 应用),包括官方和非官方提供的 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等 CAS Client 包
方便集成第三方OpenID、oAuth等等登录,只需进行简单配置和扩展
内置RememberMe 功能,当认证会话过期后可记录上一次登录用户账户

二、原理

    从结构上看,CAS 包含两个部分: CAS Server 和 CAS Client。CAS Server 需要独立部署,主要负责对用户的认证工作;CAS Client 负责处理对客户端受保护资源的访问请求,需要登录时,重定向到 CAS Server。图2 是 CAS 最基本的协议过程:

    CAS Client 与受保护的客户端应用部署在一起,以 Filter 方式保护受保护的资源。对于访问受保护资源的每个 Web 请求,CAS Client 会分析该请求的 Http 请求中是否包含 Service Ticket,如果没有,则说明当前用户尚未登录,于是将请求重定向到指定好的 CAS Server 登录地址,并传递 Service (也就是要访问的目的资源地址),以便登录成功过后转回该地址。用户在第 3 步中输入认证信息,如果登录成功,CAS Server 随机产生一个相当长度、唯一、不可伪造的 Service Ticket,并缓存以待将来验证,之后系统自动重定向到 Service 所在地址,并为客户端浏览器设置一个 Ticket Granted Cookie(TGC),CAS Client 在拿到 Service 和新产生的 Ticket 过后,在第 5,6 步中与 CAS Server 进行身份合适,以确保 Service Ticket 的合法性。
    在该协议中,所有与 CAS 的交互均采用 SSL 协议,确保,ST 和 TGC 的安全性。协议工作过程中会有 2 次重定向的过程,但是 CAS Client 与 CAS Server 之间进行 Ticket 验证的过程对于用户是透明的。
    另外,CAS 协议中还提供了 Proxy (代理)模式,以适应更加高级、复杂的应用场景。

三、基本概念描述

  • Principal

  • Credentials:身份凭证,指在系统用用于身份验证的唯一依据

  • ticket

三、pom.xml 配置

配置所需 jar 包,和编译参数

1<!-- springframework start --> 2... ... 3<dependency> 4    <groupId>org.springframework.webflow</groupId> 5    <artifactId>spring-webflow</artifactId> 6</dependency> 7<!-- springframework end --> 8 9<dependency> 10    <groupId>mysql</groupId> 11    <artifactId>mysql-connector-java</artifactId> 12</dependency> 13 14<!-- cas start --> 15<dependency> 16    <groupId>org.jasig.cas</groupId> 17    <artifactId>cas-server-core</artifactId> 18</dependency> 19<dependency> 20    <groupId>org.jasig.cas</groupId> 21    <artifactId>cas-server-support-openid</artifactId> 22</dependency> 23<dependency> 24    <groupId>org.jasig.cas</groupId> 25    <artifactId>cas-server-support-oauth</artifactId> 26</dependency> 27<dependency> 28    <groupId>org.jasig.cas</groupId> 29    <artifactId>cas-server-integration-memcached</artifactId> 30</dependency> 31<dependency> 32    <groupId>org.jasig.cas.client</groupId> 33    <artifactId>cas-client-core</artifactId> 34</dependency> 35<!-- cas end --> 36 37<dependency> 38    <groupId>org.scribe</groupId> 39    <artifactId>scribe-up</artifactId> 40</dependency> 41 42<dependency> 43    <groupId>com.buession</groupId> 44    <artifactId>cas-server-support</artifactId> 45</dependency> 46<dependency> 47    <groupId>com.buession</groupId> 48    <artifactId>open</artifactId> 49</dependency> 50<dependency> 51    <groupId>com.buession</groupId> 52    <artifactId>oauth-client</artifactId> 53</dependency> 54<dependency> 55    <groupId>com.buession</groupId> 56    <artifactId>mcrypt</artifactId> 57</dependency>

四、com.buession jar 包说明

  1. cas-server-support
    cas server 的补充,重写了部分 cas server API

    git clone git@github.com:eduosi/cas-server-support.git
    
  2. open

    git clone git@github.com:eduosi/open.git
    
  3. oauth-client

    scribe-up 的扩展,增加了 Weibo、QQ、Alipay Provider

    git clone git@github.com:eduosi/oauth-client.git
    
  4. mcrypt:
    对象加密工具,支持除 null 以外的且能转换字符串的任意对象多次加密

    git clone git@github.com:eduosi/Mcrypt.git
    

五、properties 文件加载

修改 WEB-INF/spring-configuration/propertyFileConfigurer.xml

1<bean id="propertyPlaceholderConfigurer" 2    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 3    p:location="/WEB-INF/cas.properties" />

1<bean id="propertyPlaceholderConfigurer" 2    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 3    <property name="locations"> 4        <list> 5            ... ... 6        </list> 7    </property> 8    <property name="ignoreResourceNotFound" value="true" /> 9</bean>

定义不同环境的 properties 文件

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

【实践篇】基于CAS的单点登录实践之路

上个月我负责的系统SSO升级,对接京东ERP系统,这也让我想起了之前我做过一个单点登录的项目。想来单点登录有很多实现方案,不过最主流的还是基于CAS的方案,所以我也就分享一下我的CAS实践之路。