cas+shiro實現不時時的去請求cas進行身份驗證


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>

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM