`

单点登录 - CAS【四】获取更全面的用户信息(续)

 
阅读更多

       在上一篇文章单点登录 - CAS【四】获取更全面的用户信息中提到 配置attributeRepository

<!-- 组装sql用的查询条件属性 -->	
		<property name="queryAttributeMapping">
			<map>
			    <!-- key必须是uername而且是小写否则会导致取不到用户的其它信息,value对应数据库用户名字段,系统会自己匹配 -->
				<entry key="username" value="login_name" />
			</map>
		</property>

 key="username"必须是“username”,为什么会是一个定值哪?现在分析下:

 

 1、查看org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao源代码

 

 

2、通过SingleRowJdbcPersonAttributeDao找到父类AbstractJdbcPersonAttributeDao

 

 

3、通过AbstractJdbcPersonAttributeDao找到父类AbstractDefaultAttributePersonAttributeDao

     看下关键的代码:

    

public abstract class AbstractDefaultAttributePersonAttributeDao extends AbstractFlatteningPersonAttributeDao {
    private IUsernameAttributeProvider usernameAttributeProvider = new SimpleUsernameAttributeProvider();

    /**
     * @see org.jasig.services.persondir.IPersonAttributeDao#getPerson(java.lang.String)
     * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one matching {@link IPersonAttributes} is found.
     */
    public IPersonAttributes getPerson(String uid) {
        Validate.notNull(uid, "uid may not be null.");
        
        //Generate the seed map for the uid
        final Map<String, List<Object>> seed = this.toSeedMap(uid);
        
        //Run the query using the seed
        final Set<IPersonAttributes> people = this.getPeopleWithMultivaluedAttributes(seed);
        
        //Ensure a single result is returned
        IPersonAttributes person = (IPersonAttributes)DataAccessUtils.singleResult(people);
        if (person == null) {
            return null;
        }
        
        //Force set the name of the returned IPersonAttributes if it isn't provided in the return object
        if (person.getName() == null) {
            person = new NamedPersonImpl(uid, person.getAttributes());
        }
        
        return person;
    }


    /**
     * Converts the uid to a multi-valued seed Map using the value from {@link #getDefaultAttributeName()}
     * as the key.
     */
    protected Map<String, List<Object>> toSeedMap(String uid) {
        final List<Object> values = Collections.singletonList((Object)uid);
        final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
        final Map<String, List<Object>> seed = Collections.singletonMap(usernameAttribute, values);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Created seed map='" + seed + "' for uid='" + uid + "'");
        }
        return seed;
    }

 

 

发现如下几行

        final List<Object> values = Collections.singletonList((Object)uid);
        final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
        final Map<String, List<Object>> seed = Collections.singletonMap(usernameAttribute, values);

 

4、获取SQL查询参数

    final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();

    然后找到 IUsernameAttributeProvider.java

   

public class SimpleUsernameAttributeProvider implements IUsernameAttributeProvider {
    private String usernameAttribute = "username";
    
    public SimpleUsernameAttributeProvider() {
    }
    
    public SimpleUsernameAttributeProvider(String usernameAttribute) {
        this.setUsernameAttribute(usernameAttribute);
    }
    
    /**
     * The usernameAttribute to use
     */
    public void setUsernameAttribute(String usernameAttribute) {
        Validate.notNull(usernameAttribute);
        this.usernameAttribute = usernameAttribute;
    }

    /* (non-Javadoc)
     * @see org.jasig.services.persondir.support.IUsernameAttributeProvider#getUsernameAttribute()
     */
    public String getUsernameAttribute() {
        return this.usernameAttribute;
    }

 

 

    private String usernameAttribute = "username";

  发现原因了吧,原来是在这个地方将属性给写死了,不明白为什么非得要将属性给写死哪?

queryAttributeMapping配置在org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao中,那么queryAttributeMapping是其或其父类中的属性,完全可以从属性中获取到想要的属性。

 

--------------------------------------------------------华丽的分割线------------------------------------------------------------------

在查看代码是发现AbstractJdbcPersonAttributeDao.java

public abstract class AbstractJdbcPersonAttributeDao<R> extends AbstractQueryPersonAttributeDao<PartialWhereClause> {
    private static final Pattern WHERE_PLACEHOLDER = Pattern.compile("\\{0\\}");
    
    private final SimpleJdbcTemplate simpleJdbcTemplate;
    private final String queryTemplate;
    private QueryType queryType = QueryType.AND;
    
    /**
     * @param ds The DataSource to use for queries
     * @param queryTemplate Template to use for SQL query generation. Use {0} as the placeholder for where the generated portion of the WHERE clause should be inserted. 
     */
    public AbstractJdbcPersonAttributeDao(DataSource ds, String queryTemplate) {
        Validate.notNull(ds, "DataSource can not be null");
        Validate.notNull(queryTemplate, "queryTemplate can not be null");
        
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(ds);
        this.queryTemplate = queryTemplate;
    }
    
    /**
     * @return the queryTemplate
     */
    public String getQueryTemplate() {
        return queryTemplate;
    }

    /**
     * @return the queryType
     */
    public QueryType getQueryType() {
        return queryType;
    }
    /**
     * Type of logical operator to use when joining WHERE clause components
     * 
     * @param queryType the queryType to set
     */
    public void setQueryType(QueryType queryType) {
        this.queryType = queryType;
    }

 

QueryType queryType = QueryType.AND;

/**
     * Type of logical operator to use when joining WHERE clause components
     *
     * @param queryType the queryType to set
     */
    public void setQueryType(QueryType queryType) {
        this.queryType = queryType;
    }

queryType 是查询逻辑操作符,值为:AND、OR

<constructor-arg index="1" value="select email,name,login_name,password from isc_user where {0}" />

 难道是支持多条件查询?

 

 

而我们在AbstractDefaultAttributePersonAttributeDao.java中发现

final List<Object> values = Collections.singletonList((Object)uid);
final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
final Map<String, List<Object>> seed = Collections.singletonMap(usernameAttribute, values);

 

where条件后面参数替换

private static final Pattern WHERE_PLACEHOLDER = Pattern.compile("\\{0\\}");

 

这些只会返回单条数据,而不会支持上面的猜想。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics