cas 3.5.3服务器搭建+spring boot集成+shiro模拟登录(不修改现有shiro认证架构)
因为现有系统外部接入需要,需要支持三方单点登录。由于系统本身已经是微服务架构,由多个业务独立的子系统组成,所以有自己的用户认证微服务(不是cas,我们基础设施已经够多了,现在能不增加就不增加)。但是因为客户和其他接入(公有云网络)原因,无法通过token+redis实现,所以还需要支持外部的cas。
现有认证系统采用shiro实现,业务子系统采用shiro+token假登录实现。现在要支持通过配置设置系统自身的认证子系统是否启用三方cas登录。这样无论是使用自己的认证实现、还是三方CAS,整体流程就完全一样。
CAS服务器搭建
从cas 4开始,官方就已经不再提供release war,转而需要自行下载源码打包,网上很多,这里不再阐述(下载依赖有点慢)。4.x以及之前的war可以从https://mvnrepository.com/artifact/org.jasig.cas/cas-server-webapp下载。下载后,解压到tomcat webapp目录:

启动:

修改下列配置:
去除https认证:
在tomcat\webapps\cas\WEB-INF\deployerConfigContext.xml文件
的p:httpClient-ref="httpClient"后面添加p:requireSecure="false"
把tomcat\webapps\cas\WEB-INF\spring-configuration的
ticketGrantingTicketCookieGenerator.xml文件里面把p:cookieSecure="true"改为false;
p:cookieMaxAge="-1"改为3600(-1是不保存cookie,3600秒是一个小时,保存登录信息)
把tomcat\webapps\cas\WEB-INF\spring-configuration的
warnCookieGenerator.xml的p:cookieSecure="true"改为false
p:cookieMaxAge="-1"改为3600
配置单点登出:
将tomcat\webapps\cas\WEB-INF\cas-servlet.xml中${cas.logout.followServiceRedirects:false}括号里的值改为true
可以配置简单测试认证(去掉<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />注释)或基于数据源认证(可以参考https://www.cnblogs.com/wlwl/p/10056067.html)。
spring boot cas客户端集成
库依赖:
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.5.0-GA</version>
</dependency>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.2.1</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
application.properties中加上下列配置:
cas.server-url-prefix=http://localhost:8080/cas
cas.server-login-url=http://localhost:8080/cas/login
cas.client-host-url=http://localhost:18080/
cas.use-session=true
cas.validation-type=cas
casClientLogoutUrl=http://localhost:8080/cas/logout?service=http://localhost:18080/tabase/logout.html
import net.unicon.cas.client.configuration.EnableCasClient; @EnableCasClient //启用cas client
@CloudApplication
public class ConsumerStarter {
public static void main(String[] args) {
CloudBootstrap.run(ConsumerStarter.class, args);
}
} package com.hs; import java.util.HashMap;
import java.util.Map; import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class CASAutoConfig {
@Value("${cas.server-url-prefix}")
private String serverUrlPrefix;
@Value("${cas.server-login-url}")
private String serverLoginUrl;
@Value("${cas.client-host-url}")
private String clientHostUrl; /**
* 授权过滤器
* @return
*/
@Bean
public FilterRegistrationBean filterAuthenticationRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new AuthenticationFilter());
// 设定匹配的路径
registration.addUrlPatterns("/*");
Map<String,String> initParameters = new HashMap<String, String>();
initParameters.put("casServerLoginUrl", serverUrlPrefix);
initParameters.put("serverName", clientHostUrl);
//忽略的url,"|"分隔多个url
initParameters.put("ignorePattern", "/logout/success|/index");
registration.setInitParameters(initParameters);
// 设定加载的顺序
registration.setOrder(1);
return registration;
}
}
shiro模拟登录(不修改现有shiro认证架构)
cas登录成功后,会给应用返回ticket,其中包含了用户名。此时应用主页会被shiro过滤器UserFilter拦截,在其中可以判断HttpServletRequest上是否有ticket的用户名,有、且为有效用户名,就可以用该用户名模拟登录了。这样整个shiro认证流程几乎没有变化,也不需要修改FormAuthenticationFilter。
cas在2.x和3.x版本获取用户名不一样,这一点需要注意。
2.x版本client获取CAS传递过来的用户名的方法:
// 以下三者都可以
session.getAttribute(CASFilter.CAS_FILTER_USER);
session.getAttribute("edu.yale.its.tp.cas.client.filter.user"); CASFilterRequestWrapper reqWrapper=new CASFilterRequestWrapper(request);
reqWrapper.getRemoteUser());
3.x版本client获取CAS传递过来的用户名的方法:
HttpServletRequest request = ServletActionContext.getRequest();
AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
String username = principal.getName();
Long orgnId = Long.parseLong(principal.getAttributes().get("orgnId").toString());
拿到后,就可以模拟登陆了。
boolean isAllowed = super.isAccessAllowed(request, response, mappedValue);
//
if (isAllowed) { } else if (Boolean.valueOf(BaseConfig.getConfig("ta.casEnable","false"))) {
AttributePrincipal principal = (AttributePrincipal)((HttpServletRequest) request).getUserPrincipal();
String username = principal.getName();
Subject subject = getSubject(request, response);
AuthenticationToken token = new UsernamePasswordToken();
((UsernamePasswordToken) token).setUsername(username);
((UsernamePasswordToken) token).setPassword(new char[] {'1'});
subject.login(token);
isAllowed = super.isAccessAllowed(request, response, mappedValue);
}
shiro登出
在shiro的logoutFilter中调用session.invalidate()即可。然后返回casClientLogoutUrl配置的地址退出cas即可。
/**
* CAS添加开始
*/
if (StringUtils.isNotBlank(BaseConfig.getConfig("casClientLogoutUrl"))) {
((HttpServletRequest)request).getSession().invalidate();
redirectUrl = BaseConfig.getConfig("casClientLogoutUrl");
}
// CAS添加结束
return redirectUrl;
此致,就完整的实现了cas+shiro模拟登录。
CAS ticket过期策略,参考:https://www.cnblogs.com/gao241/p/3367869.html
CAS流程介绍,参考:https://www.cnblogs.com/xiatian0721/p/8136305.html
CAS官方架构参考:https://apereo.github.io/cas/5.3.x/planning/Architecture.html
cas 3.5.3服务器搭建+spring boot集成+shiro模拟登录(不修改现有shiro认证架构)的更多相关文章
- Spring Boot 集成 Elasticsearch 实战
最近有读者问我能不能写下如何使用 Spring Boot 开发 Elasticsearch(以下简称 ES) 相关应用,今天就讲解下如何使用 Spring Boot 结合 ES. 可以在 ES 官方文 ...
- Spring boot 集成 Dubbo 快速搭建
架构: 1.ZooKeeper:服务注册中心 2.api工程:提供对外暴露的服务API 3.provider:服务提供者 4.consumer:服务消费者 示例如下: (一)新建 Maven 项目 a ...
- 使用IDEA搭建Spring Boot入门项目
简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
- Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目
前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Spring Boot 集成Shiro和CAS
Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报 分类: Spring(42) 版 ...
- Myeclipse下使用Maven搭建spring boot项目(第二篇)
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...
- Myeclipse下使用Maven搭建spring boot项目
开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...
随机推荐
- SAP成都研究院的小伙伴们庆祝公司再次获得2019年最佳雇主的场景
日前,怡安集团旗下全球领先的人力资本管理咨询机构怡安翰威特与全球高管寻聘和领导力顾问公司史宾沙旗下Kincentric共同揭晓2019年中国最佳雇主榜单.SAP中国研究院凭借企业的创新文化和多元环境, ...
- nginx服务器除了更目录可以访问,其他都出现404
配置如下: listen 80; server_name www.hongtaofei.com; location / { root /home/www/shop/public; index inde ...
- 日志 logback-spring.xml配置
文章转载自: https://blog.csdn.net/xu_san_duo/article/details/80364600 logback-spring.xml配置文件 1. 自己改下value ...
- 给 linux redis 设置密码
在redis.conf 找到 下面添加一行: 这样的话,密码就可以设置成123456,然后重启redis就可以了.
- thrift简单示例 (基于C++)
这个thrift的简单示例, 来源于官网 (http://thrift.apache.org/tutorial/cpp), 因为我觉得官网的例子已经很简单了, 所以没有写新的示例, 关于安装的教程, ...
- 为安卓项目添加FileProvider
简单记录 android7.0开始出现FileProvider.从一个小白角度看需要注意这几点: Manifest.xml中添加provider节点 添加xml文件 模块的build.gradle中添 ...
- 微信小程序转义解析渲染html
今天开发小程序时,想调用商品详情字段,发现大部分是用编辑器编辑的html原生标签,无法在小程序直接使用. 后面自己使用正则和字符串替换,效果也不佳. 最后在网上找到了wx-mina-html-view ...
- Python入门篇-文件操作
Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...
- C#模拟鼠标、键盘操作
C语言 在程序中打开网页,模拟鼠标点击.键盘输入 一.简述 记--使用C语言 打开指定网页,并模拟鼠标点击.键盘输入.实现半自动填写账号密码,并登录网站(当然现在的大部分网站都有验证码 ...
- re正则match、search、findall、finditer函数方法使用
match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...