CAS-使用Shiro+buji pac4j集成CAS客户端的配置

由于工作上的要求所以一直在寻找一些符合要求的集成方法,虽然不是自己发现的,但是有些问题经过研究更正。

1、配置web.xml
由于CAS的一个子系统Logout以后其他子系统并没有同步退出,所以这里面加入了,cas官方的集成监听器和过滤器



  Struts Blank
  
    contextConfigLocation
    
	        	classpath:spring-mvc.xml
	        
  
  
    UploadHandleServlet
    me.gacl.web.controller.UploadHandleServlet
  
  
    UploadHandleServlet
    /servlet/UploadHandleServlet
  
  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    dispatcher
    /
  
  
    index.jsp
  
  
		shiroFilter
		org.springframework.web.filter.DelegatingFilterProxy
		
			targetFilterLifecycle
			true
		
	
	
		shiroFilter
		/*
		REQUEST
		FORWARD
	

2、cas_client的pom.xml文件

        
        
			io.buji
			buji-pac4j
			${bujiVersion}
		
        
        
		
			org.apache.shiro
			shiro-core
			${shiro.version}
		
		
			org.apache.shiro
			shiro-web
			${shiro.version}
				
		
			org.apache.shiro
			shiro-ehcache
			${shiro.version}
			
				
					ehcache-core
					net.sf.ehcache
				
			
			
        
       
            org.jasig.cas.client
            cas-client-core
            3.5.1
        

3、集成重写DefaultCasLogoutHandler和Pac4jRealm两个类
①DefaultCasLogoutHandler

package com.mongodb.util;

import org.pac4j.cas.logout.DefaultCasLogoutHandler;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.store.Store;

import io.buji.pac4j.profile.ShiroProfileManager;

public class ShiroCasLogoutHandler extends DefaultCasLogoutHandler {

	public ShiroCasLogoutHandler() {
    }

    public ShiroCasLogoutHandler(final Store store) {
        super(store);
    }

    protected void destroy(final C context, final SessionStore sessionStore, final String channel) {
        // remove profiles
        final ShiroProfileManager manager = new ShiroProfileManager(context);
        manager.logout();
        logger.debug("destroy the user profiles");
        // and optionally the web session
        if (isDestroySession()) {
            logger.debug("destroy the whole session");
            final boolean invalidated = sessionStore.destroySession(context);
            if (!invalidated) {
                logger.error("The session has not been invalidated for {} channel logout", channel);
            }
        }
    }
}

②Pac4jRealm

package com.mongodb.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.bson.Document;
import org.pac4j.core.profile.CommonProfile;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.buji.pac4j.realm.Pac4jRealm;
import io.buji.pac4j.subject.Pac4jPrincipal;
import io.buji.pac4j.token.Pac4jToken;

public class ShiroCASPac4jRealm extends Pac4jRealm {
    //该方法可以获取到cas服务端的多属性返回的属性
	@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		
        final Pac4jToken token = (Pac4jToken) authenticationToken;
        final List profiles = token.getProfiles();

        final Pac4jPrincipal principal = new Pac4jPrincipal(profiles, getPrincipalNameAttribute());
        final PrincipalCollection principalCollection = new SimplePrincipalCollection(principal, getName());
        
        String loginName = principal.getProfile().getId(); 
        Session session = SecurityUtils.getSubject().getSession(true);
		session.setAttribute("user", loginName);
        return new SimpleAuthenticationInfo(principalCollection, profiles.hashCode());
    }
	

	@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
      
		//获取当前登录输入的用户名,等价于(String) principalCollection.fromRealm(getName()).iterator().next(); 
        Session session = SecurityUtils.getSubject().getSession(); 
        //  Account user = Account.class.cast(session.getAttribute(Const.SESSION_USER));

        final SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(new ArrayList());
        return simpleAuthorizationInfo;
	}
}

4、配置shiro-pac4j.properties

##cas服务前缀
sso.cas.server.prefixUrl=http://服务端URL/cas/
##cas服务登录url
sso.cas.server.loginUrl=http://服务端URL/cas/login
##cas客户端回调地址
sso.cas.client.callbackUrl=http://客户端URL/casClient/callback?client_name=msrRim
##cas服务端成功跳转地址
sso.cas.client.successUrl=http://客户端URL/casClient/user2/showshiti
##登出后地址
sso.cas.client.logoutUrl=http://服务端URL/cas/login

5、配置shiro-cas-pac4j.xml




	
	
	
		
		
		
		
	
	
		
	
	
		
	
	
	
		
		
		
		
		
	
	
	
		
			
				
			
		
		
			
		
	
	
	
	
	
		
		
	


	
	
		
	
	
		
		
		
		
		
		
	
	
	
		
		
		
		
	
	
	
	
		
		
	

	
	
		
		
			
				
				
					
						
						
					
				
				
				
					
						
						
					
				
				
				
					
						
						
						
						
					
				
			
		

		
			
				/index =  casSecurityFilter
				/callback = callback
				/static/** = anon
				/logout = logout
				/** = authc
			
		
	

	
	

	
		
	


你可能感兴趣的:(CAS)