一般都是通过配置实现,实在有点晕~~
可以搜索:cas 返回更多 ---------------------------------- 配置很麻烦,而且没咋看懂,干脆点,咱直接改代码,反正已经改了好多了
瞎改凑合用呗~~ ---------------------------------- 取用户信息在org.jasig.cas.CentralAuthenticationServiceImpl类中
validateServiceTicket方法中。
final String principalId = determinePrincipalIdForRegisteredService(principal, registeredService, serviceTicket); final Authentication authToUse;
if (!registeredService.isIgnoreAttributes()) { final Map<String, Object> attributes = new HashMap<String, Object>();
for (final String attribute : registeredService.getAllowedAttributes()) { final Object value = principal.getAttributes().get(attribute);
if (value != null) { attributes.put(attribute, value); } }
//-----------------在此往attributes里put东西即可----------------
final Principal modifiedPrincipal = new SimplePrincipal(principalId, attributes); final MutableAuthentication mutableAuthentication = new MutableAuthentication( modifiedPrincipal, authentication.getAuthenticatedDate()); mutableAuthentication.getAttributes().putAll( authentication.getAttributes()); mutableAuthentication.getAuthenticatedDate().setTime( authentication.getAuthenticatedDate().getTime()); authToUse = mutableAuthentication; } else { final Principal modifiedPrincipal = new SimplePrincipal(principalId, principal.getAttributes()); authToUse = new MutableAuthentication(modifiedPrincipal, authentication.getAuthenticatedDate()); }
看上面的代码,里面注释那行,在那put东西即可。
我是直接把这个类改成继承extends AbstractJdbcUsernamePasswordAuthenticationHandler了,
就可以取DB了
String sql = "SELECT count(*) as myCount from ..."; List xx = getJdbcTemplate().queryForList(sql); Object object = ((Map) xx.get(0)).get("myCount");
应该有正常的方法,但偶不会,就这样瞎用吧。 ------------------------------- 如此改完还不够,最后结果是通过xml返回给客户端的,
这个xml格式是在WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp中写的。
默认只输出了用户名,改下。
<cas:authenticationSuccess> <cas:user>${fn:escapeXml(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.id)}</cas:user> <c:if test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes) > 0}"> <cas:attributes> <c:forEach var="attr" items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}"> <cas:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}> </c:forEach> </cas:attributes> </c:if>
其中的<c:if>这段是后加的,把attributes里面的东西输出到客户端了。 -------------------------------------- 这样就OK了。
客户端:
Assertion assertion = (Assertion) req.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); if(assertion!=null){ String userName = assertion.getPrincipal().getName(); Object xxx = assertion.getPrincipal().getAttributes().get("xxx");
|