1. 程式人生 > >Cas單點登入(整合shiro版本)

Cas單點登入(整合shiro版本)

/**
 * shiro登入實現類 
 * 
 */
//重點是整合CasRealm
public class ShiroRealm extends CasRealm {
	
	private Logger log = LoggerFactory.getLogger(ShiroRealm.class);
	
	private TicketValidator ticketValidator;  
	
	 protected TicketValidator ensureTicketValidator()  
	    {  
	        if(ticketValidator == null)  
	            ticketValidator = createTicketValidator();  
	        return ticketValidator;  
	    }  
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		CasToken casToken = (CasToken) authcToken;  
		if (authcToken == null)  
            return null;  
		String ticket = (String) casToken.getCredentials();  
		TicketValidator ticketValidator = ensureTicketValidator();
	  try  
        {  
            Assertion casAssertion = ticketValidator.validate(ticket, getCasService());  
            AttributePrincipal casPrincipal = casAssertion.getPrincipal();  
            String userId = casPrincipal.getName();  
            log.debug("Validate ticket : {} in CAS server : {} to retrieve user : {}", new Object[] {  
                ticket, getCasServerUrlPrefix(), userId  
            });  
            Map<String, Object> attributes = casPrincipal.getAttributes();  
            casToken.setUserId(userId);  
            String rememberMeAttributeName = getRememberMeAttributeName();  
            String rememberMeStringValue = (String)attributes.get(rememberMeAttributeName);  
            boolean isRemembered = rememberMeStringValue != null && Boolean.parseBoolean(rememberMeStringValue);  
            if(isRemembered)  
                casToken.setRememberMe(true);  
            /**  此處是封裝使用者資訊
            sUsr su = new sUsr();
    		su.setUsrCde(userId);
    		sUsr susr = isUsrService.findByCode(su);
    		AccessTokenInfo atInfo = new AccessTokenInfo();
    		atInfo.setUsrCde(userId);
    		//獲取apikey
    		AccessTokenInfo ati = accessTokenInfoService.selectOneByObject(atInfo);
    		//構建ShiroUserAccount
    		ShiroUserAccount sua = new ShiroUserAccount(susr,ati);
            */
            List<Object> principals = CollectionUtils.asList(new Object[] {  
            	sua, attributes  
            });              
            PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());  
            return new SimpleAuthenticationInfo(principalCollection, ticket);  
        }  
        catch(TicketValidationException e)  
        {  
            throw new CasAuthenticationException((new StringBuilder()).append("Unable to validate ticket [").append(ticket).append("]").toString(), e);  
        }  		
	}


	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//獲取登入使用者的Shiro物件  ---主體身份資訊(驗權)
		ShiroUserAccount shiroUser = (ShiroUserAccount)principal.getPrimaryPrincipal(); 
		//斷言,若物件為空則直接丟擲異常
		Assert.notNull(shiroUser,"找不到principal中的SessionVariable---shiroUser");
		//新增使用者擁有的role
		addRoles(info,shiroUser);
		addPermissions(info,shiroUser);
		return info;
	}
	
}