欢迎使用CSDN-markdown编辑器

来源:互联网 发布:2016中国网络视频报告 编辑:程序博客网 时间:2024/06/06 01:07

springmvc+mybatis+cas 客户端配置

本文章并非从一开始搭建项目,而是自己在配置cas的时候遇到的问题记录一下,下面是客户端的几个重要的配置文件,cas服务端可以直接下载源码更改样式就可以用,这里不做描述

spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"    xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         http://www.springframework.org/schema/cache          http://www.springframework.org/schema/cache/spring-cache-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!-- Shiro Filter -->    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">        <property name="securityManager" ref="securityManager" />        <!-- 设定用户的登录链接,这里为cas登录页面的链接地址可配置回调地址 -->        <property name="loginUrl" value="${shiro.loginUrl}" />        <property name="filters">            <map>                <!-- 添加casFiltershiroFilter -->                <entry key="casFilter" value-ref="casFilter" />                <entry key="logoutFilter" value-ref="logoutFilter" />            </map>        </property>        <property name="filterChainDefinitions">            <value>                /login_toLogin = casFilter                /logout = logoutFilter                /** = user            </value>        </property>    </bean>    <bean id="casFilter" class="org.apache.shiro.cas.CasFilter">        <!-- 配置验证错误时的失败页面 -->        <property name="failureUrl" value="${shiro.failureUrl}" />        <property name="successUrl" value="${shiro.successUrl}" />    </bean>    <bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">        <!-- 配置验证错误时的失败页面 -->        <property name="redirectUrl" value="${shiro.logoutUrl}" />    </bean>    <bean id="casRealm" class="com.unifiedportal.base.shiro.UserRealm">        <!-- 认证通过后的默认角色 -->        <property name="defaultRoles" value="ROLE_USER" />        <!-- cas服务端地址前缀 -->        <property name="casServerUrlPrefix" value="${shiro.cas.serverUrlPrefix}" />        <!-- 应用服务地址,用来接收cas服务端票据 -->        <property name="casService" value="${shiro.cas.service}" />    </bean>    <!--缓存机制-->    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">        <property name="cacheManagerConfigFile" value="classpath:configuration/project/ehcache-shiro.xml" />    </bean>    <!-- Shiro's main business-tier object for web-enabled applications -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <!-- <property name="sessionManager" ref="sessionManager" /> -->        <property name="subjectFactory" ref="casSubjectFactory"></property>        <property name="realm" ref="casRealm" />        <property name="cacheManager" ref="cacheManager" />    </bean>    <!-- 如果要实现cas的remember me的功能,需要用到下面这个bean,并设置到securityManager的subjectFactory中 -->    <bean id="casSubjectFactory" class="org.apache.shiro.cas.CasSubjectFactory"></bean>    <bean        class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">        <property name="securityManager" ref="securityManager" />    </bean>    <!-- <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">         <property name="globalSessionTimeout" value="3600000" /> <property name="sessionDAO"         ref="sessionDAO" /> </bean> -->    <!-- <bean id="sessionDAO" class="com.distinct.web.session.redis.RedisSessionDAO">         <property name="sessionTimeout" value="1800000" /> <property name="redisManager"         ref="redisManager" /> </bean> -->    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>    <bean        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">        <property name="staticMethod"            value="org.apache.shiro.SecurityUtils.setSecurityManager"></property>        <property name="arguments" ref="securityManager"></property>    </bean></beans>

shiro.properties

#localhostshiro.loginUrl=http://172.31.142.110:8080/login?service=http://127.0.0.1:8080/login_toLoginshiro.logoutUrl=http://172.31.142.110:8080/logout?service=http://127.0.0.1:8080/indexshiro.cas.serverUrlPrefix=http://172.31.142.110:8080/shiro.cas.service=http://127.0.0.1:8080/login_toLoginshiro.successUrl=/indexshiro.failureUrl=/index

注意:改/index 必须是正常可以走同的路径,并且/index,/login_toLogin必须在上面spring-shiro.xml中有拦截。

web.xml

web.xml中需要添加cas拦截器

 <!-- 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>

注意:web.xml中的shiroFilter 必须在spring.shiro.xml中有个名字为shiroFilter 的bean

UserRealm.java

package com.unifiedportal.base.shiro;import com.unifiedportal.core.bean.UserBean;import com.unifiedportal.core.service.UserService;import org.apache.shiro.SecurityUtils;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 javax.annotation.Resource;import javax.xml.registry.infomodel.User;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * <b>TODO(com.unifiedportal.core.controller @TODO)</b><br> * 修改备注: * @since V1.0 */public class UserRealm  extends CasRealm {    @Resource    private UserService userService;    protected final Map<String, SimpleAuthorizationInfo> roles = new ConcurrentHashMap<String, SimpleAuthorizationInfo>();    /**     * 设置角色和权限信息     */    @Override    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {        String account = (String) principals.getPrimaryPrincipal();        SimpleAuthorizationInfo authorizationInfo = null;        if (authorizationInfo == null) {            authorizationInfo = new SimpleAuthorizationInfo();//            authorizationInfo.addStringPermissions(roleService.getPermissions(account));//            authorizationInfo.addRoles(roleService.getRoles(account));            roles.put(account, authorizationInfo);        }        return authorizationInfo;    }    /**     * 1、CAS认证 ,验证用户身份     * 2、将用户基本信息设置到会话中     */    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {        AuthenticationInfo authc = super.doGetAuthenticationInfo(token);        String account = (String) authc.getPrincipals().getPrimaryPrincipal();        UserBean userBean = new UserBean();        userBean.setUsername(account);       // userBean = userService.userLogin(userBean);        SecurityUtils.getSubject().getSession().setAttribute("user", userBean);        return authc;    }}

配置中遇到的坑:

配置完这些之后发现,能登陆成功,就是一直跳转不到我想要的首页,出现多次重定向的问题:找了好久发现是因为配置文件中,登录成功跳转的地址并没有真正的对应地址,添加上controller 就可以了

原创粉丝点击