[原]CAS和Shiro在spring中集成
shiro是权限管理框架,现在已经会利用它如何控制权限。为了能够为多个系统提供统一认证入口,又研究了单点登录框架cas。因为二者都会涉及到对session的管理,所以需要进行集成。
Shiro在1.2.0的时候提供了对cas的集成。因此在项目中添加shiro-cas的依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>${shiro.version}</version>
</dependency>
Shiro对cas集成后,cas
client的配置更加简单了。原理就是将casFilter添加到到shiroFilter的filterChain中。 shiroFilter是在web.xml中定义的,前文已经讲过。
在Spring项目中集成Shiro和CAS
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-lazy-init="true"> <beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<propertyname="securityManager" ref="securityManager" /> <!--没有单点登录下的配置:没有权限或者失败后跳转的页面 -->
<!--<property name="loginUrl" value="/login/toLoginAction"/> --> <!--有单点登录的配置:登录 CAS 服务端地址,参数 service 为服务端的返回地址 -->
<propertyname="loginUrl"
value="http://localhost:18080/cas/login?service=http://localhost:8080/gxpt_web_qx_login/shiro-cas"/>
<!--<property name="successUrl" value="/page/index.jsp"/> -->
<propertyname="successUrl" value="/indexAction" /> <propertyname="filters">
<map>
<!--添加casFilter到shiroFilter -->
<entrykey="casFilter" value-ref="casFilter">
</entry>
</map>
</property> <propertyname="filterChainDefinitions">
<value>
/shiro-cas= casFilter
/styles/**= anon
/**= user
</value>
</property> <!--没有单点登录下的配置: -->
<!--<property name="filterChainDefinitions">
<value>
/styles/**= anon
/login/loginAction= anon
/login/logoutAction= logout
/**= user
</value>
</property>-->
</bean> <beanid="casFilter" class="org.apache.shiro.cas.CasFilter">
<!--配置验证错误时的失败页面(Ticket 校验不通过时展示的错误页面) -->
<propertyname="failureUrl" value="/page/error.jsp" />
</bean> <beanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--Single realm app. If you have multiple realms, use the 'realms' property
instead.-->
<!--没有单点登录下的配置: -->
<!--<property name="realm" ref="shiroDbRealm" /> --> <propertyname="realm" ref="casRealm" />
<propertyname="subjectFactory" ref="casSubjectFactory" /> <propertyname="cacheManager" ref="shiroEhcacheManager" />
</bean> <beanid="casRealm" class="web.qx.login.shiro.MyCasRealm">
<propertyname="defaultRoles" value="ROLE_USER"/>
<propertyname="casServerUrlPrefix"value="http://localhost:18080/cas" />
<!--客户端的回调地址设置,必须和上面的shiro-cas过滤器拦截的地址一致 -->
<propertyname="casService"
value="http://localhost:8080/gxpt_web_qx_login/shiro-cas"/>
</bean> <!--Define the realm you want to use to connect to your back-end security
datasource:-->
<!--
<beanid="shiroDbRealm"class="web.qx.login.shiro.ShiroDbRealm">
<propertyname="loginService"ref="login-loginBean"></property>
</bean>
--> <beanid="casSubjectFactory"class="org.apache.shiro.cas.CasSubjectFactory" /> <!--用户授权/认证信息Cache, 采用EhCache 缓存 -->
<beanid="shiroEhcacheManager"class="org.apache.shiro.cache.ehcache.EhCacheManager">
<propertyname="cacheManagerConfigFile"value="classpath:config/ehcache-shiro.xml" />
</bean> <!--保证实现了Shiro内部lifecycle函数的bean执行 -->
<beanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!--AOP式方法级权限检查 -->
<!--Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!--the lifecycleBeanProcessor has run: -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<propertyname="proxyTargetClass" value="true" />
</bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<propertyname="securityManager" ref="securityManager" />
</bean> </beans>
没有单点登录情况下的话,登录认证和授权认证默认在AuthorizingRealm的doGetAuthorizationInfo和doGetAuthenticationInfo中进行,所以我这里是通过shiroDbRealm(继承AuthorizingRealm的自定义类)覆写doGetAuthorizationInfo和doGetAuthenticationInfo,实现自定义登录认证和授权认证。
有单点登录情况下,登录认证是在casserver进行的,那么执行流程是这样的:用户从
cas server登录成功后,跳到cas client的CasRealm执行默认的doGetAuthorizationInfo和doGetAuthenticationInfo,此时doGetAuthenticationInfo做的工作是把登录用户信息传递给shiro,保持默认即可,而对于授权的处理,可以通过MyCasRealm(继承CasRealm的自定义类)覆写doGetAuthorizationInfo进行自定义授权认证。
[原]CAS和Shiro在spring中集成的更多相关文章
- spring中集成shiro
Shiro的组件都是JavaBean/POJO式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web应用的集成. 在示 ...
- spring中集成shiro进行安全管理
shiro是一款轻量级的安全框架,提供认证.授权.加密和会话管理四个基础功能,除此之外也提供了很好的系统集成方案. 下面将它集成到之前的demo中,在之前spring中使用aop配置事务这篇所附代码的 ...
- CAS单点登录和spring securtiy集成
说明:本文章主要建立在spring-security早已集成在系统中的前提下: 1.需要创建一个spring-security.xml文件并关联在applicationContext.xml文件中:& ...
- Java-Shiro(三):Shiro与Spring MVC集成
新建Java Daynamic Web项目 导入Spring.SpringMVC依赖包: 导入Spring & Spring MVC包(导入如下所有开发包): Spring AOP依赖扩展包: ...
- shiro 与spring的集成
1.导入spring与shiro的jar包 2.在web.xml 文件中配置shiro的shiroFilter <filter> <filter-name>shiroFilte ...
- MyBatis延迟加载及在spring中集成配置
当你要使用one to one,many to one 就会碰到N+1的问题.很明显,对象之间关联很多有A关联B,B关联C,C关联A这样的关系,如果不是采用延迟加载,很容易一下在出现成千上万对象 ...
- Spring中集成Ehcache缓存
1.导入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- spring中集成hibernate
1.hibernate框架是什么? (1)hibernate对jdbc进行的封装 (2)开源的轻量级框架. 2.hibernate思想:ORM(对象关系映射) (1)让实体类和数据库中的表一一对应(表 ...
- 细说shiro之五:在spring框架中集成shiro
官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...
随机推荐
- MySQL安全问题
使用MySQL,安全问题不能不注意.以下是MySQL提示的23个注意事项:1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信.2.用set pass ...
- mysql表结构设计
允许NULL值的字段,数据库在进行比较操作时,会先判断其是否为NULL,非NULL时才进行值的必对.因此基于效率的考虑,所有字段均不能为空,即全部NOT NULL: 对于变长表,由于记录大小不同,在其 ...
- php 数据结构 hash表
hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...
- 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- css案例学习之class执行的顺序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 开源DirectShow分析器和解码器: LAV Filter
LAV Filter 是一款开源的DirectShow视频分离和解码软件,他的分离器LAVSplitter封装了FFMPEG中的libavformat,解码器LAVAudio和LAVVideo则封装了 ...
- linux学习(二)-目录的操作命令
Linux命令大全:http://www.jb51.net/linux/ 目录分绝对路径和相对路径 : 绝对路径,在路径前会加 / 相对路径就是相对于当前的路径,直接 路径名即可. 查看目录: cd ...
- 全国计算机等级考试二级教程-C语言程序设计_第6章_字符型数据
#include <stdio.h> main() { char c; char d; c = ; d = '; if (c == d) { printf("yes\n" ...
- shape和selector是Android UI设计中经常用到的
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- 【jQueryMobile】Helloworld而页面切换
jQuery Mobile它是jQuery 在手机和平板设备的版本号. jQuery Mobile 它不仅会带来重大的移动平台jQuery核心库,而且会发布一个完整统一jQuery搬家UI相框.全球主 ...