cas4.0.1返回给客户端登录用户的更多信息

阅读更多
首先我用的是 cas server 4.0.1版本的 网上很多都是 3.x的 我就不作考虑了
因为版本间配置差异太大
网上有关4.x的 配置资料太少 所以我在这里把这个博客写下 希望对 跟我遇到相同问题的人有所帮助 同时也是对互联网的一种感恩回馈 因为我也是看了很多博客和资料后 才解决这个问题的


我的项目需求有点特殊 可以用邮箱和手机号码登录
而登录用的username又不是用的这两个字段 是通过一个值为uuid的 username字段实现的 (老项目重构 没法改表结构 
所以 光通过默认返回的 登录账号是不够的 因为 用户登录后是可以修改邮箱和手机的 一旦修改后 我的 客户端获取就 会有问题



参考了 网上的

http://www.cnblogs.com/vhua/p/cas_4.html
http://www.tuicool.com/articles/QB3iia
这两个博客
第一个博客的方法没有 给出如何 用数据库的方式实现 之前在如何用jdbctemplate查询的时候一直 搞了很久没搞出来 参考了第二个博客后搞出来了




先把 deployerConfigContext.xml 贴出来






	

	
	
		
			
				
				
				
			
		

		

		
		
			
		
		
			
				
				
			
		
	

	
	

	
	
	
	

	
	
		
		
		
		
		
	





	
	
	
	


	
	


	
	
		
		
		
		

		
		
		
		

		
		

		
		
		
		
		
		 
		 
		 
	

	

        
    

	
	
		
		
		
		
		

		
		
		
		

		
		

		
		

		
		
		

		
		
		

		
		
			
				
			
		
	

	
		
		
	
	

	
	

	
	
		
	



        
       
        
	


     
      
     


	

	

	

	




	
	
 	  
		
			
			
		 
			
			
			
				
		
		
	

	

	

	
		
		
		
	




核心的 AccoutAttributeDao

package org.jasig.services.persondir.support;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jasig.services.persondir.IPersonAttributes;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccoutAttributeDao extends StubPersonAttributeDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public IPersonAttributes getPerson(String uid) {
		String sql = "";
		if (uid.indexOf("@") > 0) { // 如果是邮箱
			sql = "select * from Account where email=?";
		} else {
			sql = "select * from Account where PhoneNumber=?";
		}
		// 
		final Map values = jdbcTemplate.queryForMap(sql, uid);

		Map> attributes = new HashMap>();
		attributes.put("username",
				Collections.singletonList((Object) values.get("UserName")));
		attributes.put("email",
				Collections.singletonList((Object) values.get("Email")));
		attributes.put("phonenumber",
				Collections.singletonList((Object) values.get("PhoneNumber")));
		attributes.put("id",
				Collections.singletonList((Object) values.get("Id")));
		return new AttributeNamedPersonImpl(attributes);
	}
}



protocol 2.0文件下 的 casServiceValidationSuccess.jsp页面加上
    
            
                
                    ${fn:escapeXml(attr.value)}
                
            
        



客户端调用
package com.gttown.app.user.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AssertionHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.gttown.app.user.model.Account;
import com.gttown.app.user.service.AccountService;

@Controller
public class BaseController {
	@Autowired
	private AccountService accountService;

	/**
	 * 获取当前登录用户账号信息
	 */
	public Account getAccount(HttpServletRequest request) {

		// String loginaccount =
		// AssertionHolder.getAssertion().getPrincipal().getName(); //登录账号
		//
		//
		AttributePrincipal principal = (AttributePrincipal) request
				.getUserPrincipal();
		Map attributes = principal.getAttributes();
		// Map attributes2 =
		// AssertionHolder.getAssertion().getPrincipal().getAttributes();
		//
		String id = (String) attributes.get("id");
		String uuid = (String) attributes.get("username"); // 唯一key uuid
		System.out.println(uuid);
		String email = (String) attributes.get("email");
		String phonenumber = (String) attributes.get("phonenumber");

		Account account = new Account();
		account.setId(new Long(id));
		account.setEmail(email);
		account.setPhonenumber(phonenumber);
		account.setUsername(uuid);

		return account;
	}
}




最后贴上客户端登录后 获得的用户值的效果图


cas4.0.1返回给客户端登录用户的更多信息_第1张图片


功夫不负有心人 终于解决这个问题了!


如果看了我的博客 还是没解决 可以 加扣扣群 186408628 来 群里 @ 我 我就是群主 
  • cas4.0.1返回给客户端登录用户的更多信息_第2张图片
  • 大小: 65.6 KB
  • 查看图片附件

你可能感兴趣的:(cas4.0.1)