配置信息
shiro.loginUrl=http://127.0.0.1:8080/cas/login?service=http://localhost:8080/jeefh/vworkerC/login01
shiro.logoutUrl=http://127.0.0.1:8080/cas/logout?service=http://localhost:8080/jeefh/vworkerC/login01
shiro.cas.serverUrlPrefix=http://127.0.0.1:8080/cas/
shiro.cas.service=http://127.0.0.1:8080/jeesh/
shiro.failureUrl=http://127.0.0.1:8080/cas/login?service=http://localhost:8080/jeefh/vworkerC/login01
shiro.successUrl=/users/loginSuccess/
springmvc+shiro.xml配置信息
<!-- shiro的主过滤器,beanId 和web.xml中配置的filtername需要保持一致 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager" />
<!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->
<property name="loginUrl" value="${shiro.loginUrl}" />
<property name="filters">
<map>
<!-- 添加casFilter到shiroFilter -->
<entry key="casFilter" value-ref="casFilter" />
<entry key="logoutFilter" value-ref="logoutFilter" />
</map>
</property>
<property name="filterChainDefinitions">
<value>
/* = casFilter
/logout = logoutFilter
/users/** = user
/con/** = keepone
/main = keepone
/jee/** = keepone
</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>
<!-- 定义Shiro安全管理配置 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="subjectFactory" ref="casSubjectFactory"></property>
<property name="realm" ref="casrRealm" />
</bean>
<bean id="casSubjectFactory" class="org.apache.shiro.cas.CasSubjectFactory"></bean>
<bean id="casrRealm" class="com.enter.net.cas.realm.UserRealm" depends-on="userService,roleService">
<!-- 认证通过后的默认角色 -->
<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="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- 超时时间 -->
<property name="globalSessionTimeout" value="20000" />
<!-- 定时检查失效的session,默认true -->
<property name="sessionValidationSchedulerEnabled" value="true" />
<!-- 删除过期的session,默认true -->
<property name="deleteInvalidSessions" value="true" />
<!-- 相隔多久检查一次session的有效性,使用默认的60分钟 -->
<!-- session存储的实现 -->
<property name="sessionDAO" ref="shiroSessionDao" />
<!-- sessionIdCookie的实现,用于重写覆盖容器默认的JSESSIONID -->
<property name="sessionIdCookie" ref="sessionIdCookie" />
<property name="sessionIdCookieEnabled" value="true"/>
</bean>
<!-- 指定本系统SESSIONID, 默认为: JSESSIONID 问题: 与SERVLET容器名冲突, 如JETTY, TOMCAT 等默认JSESSIONID,
当跳出SHIRO SERVLET时如ERROR-PAGE容器会为JSESSIONID重新分配值导致登录会话丢失! -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="SHAREJSESSIONID"/>
<!-- 设置Cookie的路径,默认空,即存储在域名根下 jsessionId的path为 / 用于多个系统共享jsessionId -->
<property name="path" value="/" />
<!-- more secure, protects against XSS attacks -->
<property name="httpOnly" value="true" />
</bean>
<!-- session存储的实现 -->
<bean id="shiroSessionDao"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO" />
<!-- 单机session -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
<!--保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) -->
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod"
value="org.apache.shiro.SecurityUtils.setSecurityManager" />
<property name="arguments" ref="securityManager" />
</bean>
<!-- 用于开启 Shiro Spring AOP 权限注解的支持 -->
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
自己定义的realm
public class UserRealm extends CasRealm {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
/**
* @功能 为当前登录的Subject授予角色和权限
* **/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String)principals.getPrimaryPrincipal();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
System.out.println(username);
try {
Set result = new HashSet(roleService.getAllRole());
authorizationInfo.setRoles(result);
authorizationInfo.setStringPermissions((Set) userService.getObjById(username));
} catch (Exception e) {
e.printStackTrace();
}
return authorizationInfo;
}
/**
* @功能 验证当前登录的Subject
*
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
AuthenticationInfo authc = super.getAuthenticationInfo(authcToken);
String account = (String) authc.getPrincipals().getPrimaryPrincipal();
try {
User user = userService.getObjById(account);
SecurityUtils.getSubject().getSession().setAttribute("user", user);
} catch (Exception e) {
e.printStackTrace();
}
return authc;
}
}
web.xml
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
Crontroller
/**
*
* @param currUser
* @param request
* @return
*/
@RequestMapping(value = "login01")
@ResponseBody
@SystemControllerLog(description="用户登录的操作")
public String login01( User currUser, HttpServletRequest request) {
Subject subject = SecurityUtils.getSubject();
ShiroUser shiroUser01 = (ShiroUser) subject.getPrincipal();
ShiroUser shiroUser = SessionUser.getShiroUser();
String result = "execute jeefh method</br>"
+ "";
result += "sessionId : "+request.getSession().getId() +"</br>";
result += "request.getRemoteUser() : " + request.getRemoteUser() +"</br>";
result += "request.getUserPrincipal() : " + request.getUserPrincipal() +"</br>";
return result;
}
登陆成功以后 shiro一直获取不到登陆人的信息和角色。
求大神帮忙看看!
很少人使用cas吗?