目录

1.tomcat添加https安全协议

2.下载cas server端部署到tomcat上

3.CAS服务器深入配置(连接MYSQL)

4.Apache Shiro 集成Cas作为cas client端实现

Apache Shiro 集成Cas作为cas client端实现

第一步、添加maven依赖

     <!-- shiro依赖包 -->
<!-- <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-quartz</artifactId> <version>${shiro.version}</version> </dependency> -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- shiro-cas集成依赖包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>${shiro.version}</version>
</dependency>

第二步、添加spring-shiro.xml配置文件

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="true">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- 设定角色的登录链接,这里为cas登录页面的链接可配置回调地址 -->
<property name="loginUrl" value="http://192.168.20.248:8080/cas/login?service=http://127.0.0.1:8080/nhmz/shiro-cas" />
<property name="successUrl" value="/index.htm"></property>
<property name="filters">
<util:map>
<entry key="casFilter" value-ref="casFilter" />
<entry key="logout" value-ref="logout" />
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/shiro-cas=casFilter
/logout.htm=logout
/login.htm=authc
/xzqh/**=authc
/tjfx/**=authc
/gxfw/**=authc
/xtsz/**=authc
/region/**=authc
/shareApi/**=authc
/**=anon
</value>
</property>
</bean>
<!-- shiro-cas登录过滤器 -->
<bean id="casFilter" class="org.apache.shiro.cas.CasFilter">
<!-- 配置验证错误时的失败页面 ,这里配置为登录页面 -->
<property name="failureUrl" value="http://192.168.20.248:8080/cas/login?service=http://127.0.0.1:8080/nhmz/shiro-cas" />
</bean>
<!-- 退出登录过滤器 -->
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<property name="redirectUrl" value="http://192.168.20.248:8080/cas/logout?service=http://127.0.0.1:8080/nhmz/index.htm" />
</bean>

<!-- 自定义casRealm -->
<bean id="casRealm" class="com.ld.nhmz.shiro.MzCasRealm">
<!-- <property name="defaultRoles" value="ROLE_USER" /> -->
<!-- 配置cas服务器地址 -->
<property name="casServerUrlPrefix" value="http://192.168.20.248:8080/cas" />
<!-- 客户端的回调地址设置,必须和上面的shiro-cas过滤器casFilter拦截的地址一致 -->
<property name="casService" value="http://127.0.0.1:8080/nhmz/shiro-cas" />
</bean> <!--缓存机制 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
</bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="casRealm" />
<property name="subjectFactory" ref="casSubjectFactory" />
<property name="cacheManager" ref="cacheManager" />
</bean> <!-- 如果要实现cas的remember me的功能,需要用到下面这个bean,并设置到securityManager的subjectFactory中 -->
<bean id="casSubjectFactory" class="org.apache.shiro.cas.CasSubjectFactory" /> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" />
<property name="arguments" ref="securityManager" />
</bean>
</beans>

这里由于前端的每一个操作shiro都会做登录和权限认证,所以上面配置添加了缓存机制

所以在classpath目录下还要添加缓存而配置文件ehcache-shiro.xml,配置如下:

<?xml version="1.1" encoding="UTF-8"?>
<ehcache name="shirocache"> <diskStore path="java.io.tmpdir"/> <defaultCache
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> <!-- <cache name="diskCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache> --> <cache name="passwordRetryCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="0"
overflowToDisk="false"
>
</cache> <cache name="authorizationCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="0"
overflowToDisk="false"
>
</cache> <cache name="authenticationCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="0"
overflowToDisk="false"
>
</cache> <cache name="shiro-activeSessionCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="0"
overflowToDisk="false"
>
</cache>
</ehcache>

第三步、添加<bean id="casRealm" class="com.ld.nhmz.shiro.MzCasRealm">配置的自定义实现类

package com.ld.nhmz.shiro;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cas.CasRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import com.ld.nhmz.ebo.SusRoleEntity;
import com.ld.nhmz.ebo.SusUserEntity;
import com.ld.nhmz.service.SusRolePermissionService;
import com.ld.nhmz.service.SusRoleService;
import com.ld.nhmz.service.SusUserService; //@Service
@SuppressWarnings("deprecation")
@Transactional(value = "nhmzTM")
public class MzCasRealm extends CasRealm { @Autowired
private SusUserService userService; @Autowired
private SusRolePermissionService rolePermissionService; @Autowired
private SusRoleService roleService; /**
* 授权访问控制,用于对用户进行的操作进行人证授权,证明该用户是否允许进行当前操作,如访问某个链接,某个资源文件等
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
SusUserEntity user = userService.findByUsername(username); if (user != null) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); // 设置用户的角色
String roleId = userService.getRoleIdFromUserId(user.getId());
SusRoleEntity roleEntity = roleService.getRole(roleId);
if (null != roleEntity)
authorizationInfo.addRole(roleEntity.getRolecode()); // 设置用户对应的角色的权限集合
List<String> permissons;
try {
permissons = rolePermissionService.getPermissionIds2(roleId);
for (String permission : permissons) {
authorizationInfo.addStringPermission(permission);
}
} catch (Exception e) {
e.printStackTrace();
} return authorizationInfo;
}
return null; } /**
* 验证用户身份
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return super.doGetAuthenticationInfo(token);
} @Override
public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
super.clearCachedAuthorizationInfo(principals);
} @Override
public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
super.clearCachedAuthenticationInfo(principals);
} @Override
public void clearCache(PrincipalCollection principals) {
super.clearCache(principals);
} public void clearAllCachedAuthorizationInfo() {
getAuthorizationCache().clear();
} public void clearAllCachedAuthenticationInfo() {
getAuthenticationCache().clear();
} public void clearAllCachedKickoutInfo() { } public void clearAllCache() {
clearAllCachedAuthenticationInfo();
clearAllCachedAuthorizationInfo();
clearAllCachedKickoutInfo();
}
}

第四步、将spring-shiro.xml配置文件导入到spring的配置文件中,注意配置文件间的相对路径

第五步、web.xml中添加shiro过滤器

<!-- shiro filter的名字是shiroFilter,那么在spring的配置文件中要有一个名字为shiroFilter的bean -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

以上步骤完成,shiro集成cas的步骤就完成了,实现了单点登录和单点注销登录。

cas+tomcat+shiro实现单点登录-4-Apache Shiro 集成Cas作为cas client端实现的更多相关文章

  1. cas+tomcat+shiro实现单点登录-3-CAS服务器深入配置(连接MYSQL)

    目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...

  2. cas+tomcat+shiro实现单点登录-2-部署cas server到tomcat

    目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...

  3. cas+tomcat+shiro实现单点登录-1-tomcat添加https协议

    目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...

  4. CAS 在 Tomcat 中实现单点登录

    单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统 中,用户只需要登录一次就可以访问所有相互信任的应用系统.CA ...

  5. 【IBM】使用 CAS 在 Tomcat 中实现单点登录

    来源: IBM Developer http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/ 张 涛 (zzhangt@cn.ibm.com ...

  6. 使用 CAS 在 Tomcat 中实现单点登录

    单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.CAS ...

  7. 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    developerWorks 中国 技术主题 Open source 文档库 使用 CAS 在 Tomcat 中实现单点登录 单点登录(Single Sign On , 简称 SSO )是目前比较流行 ...

  8. 【CAS单点登录视频教程】 第02集 -- 安装CAS

    目录 ----------------------------------------- [CAS单点登录视频教程] 第06集[完] -- Cas认证 学习 票据认证FormsAuthenticati ...

  9. 【CAS单点登录视频教程】 第01集-- 认识CAS

    CAS 是什么? 目录 ----------------------------------------- [CAS单点登录视频教程] 第06集[完] -- Cas认证 学习 票据认证FormsAut ...

随机推荐

  1. jq 事件冒泡总结

    什么是JS事件冒泡? 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个 ...

  2. :after伪类+content内容生成经典应用举例

    一.简单说说content内容生成 content内容生成就是通过content属性生成内容,content属性早在CSS2.1的时候就被引入了,可以使用:before以及:after伪元素生成内容. ...

  3. C++中复制构造函数与重载赋值操作符

    我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成这样的四个函数.例如以下类:   class CTe ...

  4. 转:virtualbox+ubuntu设置共享文件夹

    1.       进入系统,安装增强功能,安装完关闭系统. 2.       在E盘下创建一个共享文件夹 gongxiang. 3.       设置->数据空间->固定分配,选择共享文件 ...

  5. c#使用spy进行模拟操作

    很无奈,写了很长时间,最后保存时网页失去响应,真是要命呢.本来想就此放弃了,但是想还是粗略的重写一次吧,希望日后可以对朋友有一定的帮助. Microsoft.Spy工具是一个基础工具,我们简要介绍一下 ...

  6. MVC通俗演义系列开篇

         最近在读<世界史通俗演义>,发现这本书非常好,比从小上大的历史书好的多.读的开心之余,也不敢跟别人说,怕被说成“通俗”.这跟英文的技术类文章很像.英文中的IT类文章几乎是通俗形象 ...

  7. 【细说Java】Java变量初始化顺序

    Java的变量初始化顺序,对这里一直似懂非懂,面试的时候也经常被问到,但答的一直不好,现在整理记录一下,以后忘记了可以来看看. 程序分为两个部分,第一个部分不考虑继承,第二个部分考虑继承: (1)不考 ...

  8. mybatis dao无实现类的配置

    spring的配置文件 添加: <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">     ...

  9. 利用eclipse新建的java web项目没有部署描述符web.xml文件怎么办?

    原文转自:http://blog.csdn.net/suyu_yuan/article/details/50947007 利用eclipse新建的Java Web项目没有部署描述符web.xml文件, ...

  10. JS浏览器对象-window对象

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...