首页 技术 正文
技术 2022年11月15日
0 收藏 858 点赞 3,983 浏览 2944 个字

cas+shiro不进行时时去cas验证身份信息,需要用shiro在当前系统有一份完整的认证机构。

那么有一个问题,什么时候去cas校验信息,目前的配置方式:

  cas系统设置默认的浏览器session存活时间,当前系统的session存活时间为30分钟,那么当当前系统身份验证失败是,去cas校验。

  这里涉及到一个非常重要的节点,就是shiro框架内部是怎么进行cas校验的呢,请看代码:

    org.apache.shiro.web.filter.AccessControlFilterd还是所有默认验证类的父类,

    父类中的redirectToLogin方法就是去请求cas服务器,重新获取验证信息

/**
* Convenience method for subclasses that merely acquires the {@link #getLoginUrl() getLoginUrl} and redirects
* the request to that url.
* <p/>
* <b>N.B.</b> If you want to issue a redirect with the intention of allowing the user to then return to their
* originally requested URL, don't use this method directly. Instead you should call
* {@link #saveRequestAndRedirectToLogin(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
* saveRequestAndRedirectToLogin(request,response)}, which will save the current request state so that it can
* be reconstructed and re-used after a successful login.
*
* @param request the incoming <code>ServletRequest</code>
* @param response the outgoing <code>ServletResponse</code>
* @throws IOException if an error occurs.
*/
protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
String loginUrl = getLoginUrl();
WebUtils.issueRedirect(request, response, loginUrl);
}

    

    现在要解决一个问题,就是当前系统的身份验证信息过期了,这个时候页面向后台发起了一个ajax请求,那么后台拿到这个请求之后直接对这个请求进行转发到cas服务就会出现一个问题:跨域问题。

   参考解决办法:因为我的所有后台除了首页是用默认的org.apache.shiro.web.filter.authc.AnonymousFilter类进行身份验证,其他的请求都是通过

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter进行的权限验证,又因为PermissionsAuthorizationFilter继承了AccessControlFilterd
所以我的解决办法就是创建一个自己的PermissionsAuthorizationFilter覆盖AccessControlFilterd的redirectToLogin方法

    

import java.io.IOException;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter;import com.chenrd.shiro.AuthorRuntimeException;/**
* 最最重要的一点,解决了页面没有刷新点击功能,但是后台的author已经被注销的情况下会去发送cas请求而产生的跨域问题
*
* @author chenrd
* @version 2015年12月11日
* @see MyPermissionsAuthorizationFilter
* @since
*/
public class MyPermissionsAuthorizationFilter extends PermissionsAuthorizationFilter
{ @Override
protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
throw new AuthorRuntimeException("身份异常,不进行转发到登录页面");
/*String loginUrl = getLoginUrl();
WebUtils.issueRedirect(request, response, loginUrl);*/
}
}

  然后在shiro的配置文件里面修改如下:

  

    <bean id="myPermissionsAuthorizationFilter" class="com.chenrd.shiro.filter.MyPermissionsAuthorizationFilter"/>    <bean id="filterChainManager" class="com.chenrd.shiro.filter.CustomDefaultFilterChainManager">
<property name="loginUrl" value="${cas.url}/login?service=${apply.url}/cas"/>
<property name="successUrl" value="/"/>
<property name="unauthorizedUrl" value="/authority"/>
<property name="customFilters">
<util:map>
<entry key="cas" value-ref="casFilter"/>
          <!--替换默认的权限控制类-->
<entry key="perms" value-ref="myPermissionsAuthorizationFilter"/>
</util:map>
</property>
<property name="defaultFilterChainDefinitions">
<value>
/login=anon
/cas=cas
/jaxws/services/**=anon
/**=authc
</value>
</property>
</bean>

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781