从 CAS 获取更多属性而不仅仅是用户 ID
我将 CAS 与 JDBC 身份验证处理程序一起使用,想知道是否可以在成功身份验证后获取主体对象的其他属性(例如名字、姓氏),而不仅仅是从 CAS 获取用户名?
I am using CAS with JDBC Authentication handler and was wondering is it possible to get the other attributes of principal object (for e.g. firstname, lastname) not just the username from CAS after successful authentication?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在casServiceValidationSuccess.jsp中,我添加如下内容:
在deployerConfigContent.xml中,我添加如下内容:
它有效。
我在调试过程中遇到了这个问题,如果更改此JSP或XML文件,请关闭浏览器,否则更改将不起作用。当心。
In the casServiceValidationSuccess.jsp, I add like below:
In the deployerConfigContent.xml, I add like below:
It works.
I came across this problem during the debug, please close the browser if you change this JSP or XML files, otherwise the changes won't work. Be careful.
为了从数据库获取任何用户属性,我执行了以下操作:
在deployerConfigContext.xml中使用PersonDirectoryPrincipalResolver
:
而不是使用标准 SingleRowJdbcPersonAttributeDao 类创建您自己的实现,该实现不返回仅查询结果中的一行,但聚合了所有返回行的数据:
复制
SingleRowJdbcPersonAttributeDao
中的所有代码,并仅更改一种方法parseAttributeMapFromResults
。您将看到类似的内容:
在deployerConfigContext.xml 中:
在我的例子中,我也使用了SAML 协议。
因此,您将在客户端上获得您选择返回的所有属性。
例如,如果用户在客户端上可以拥有许多角色:
用户:用户名,名字,姓氏,电子邮件,...,[ROLE_1,ROLE_2,ROLE_3]
我的案例适用于 Spring Security 和 Grails。
我不确定这是 100% 的风水解决方案:),因为它煮得很快,但在我们的情况下它有效。
希望有帮助。
To get any user attributes from DB I did the following:
use PersonDirectoryPrincipalResolver
in deployerConfigContext.xml:
instead of using standard SingleRowJdbcPersonAttributeDao class create your own implementation which returns not only one row from a query result but aggregated data from all returned rows:
copy all code from
SingleRowJdbcPersonAttributeDao
and change only one methodparseAttributeMapFromResults
.you will have something like that:
and in deployerConfigContext.xml:
Also in my case I used SAML protocol.
As a result you will get on the client all attributes which your select returns.
For example, if user have many roles you could have on the client:
User: username, firstname, lastname, email, ... , [ROLE_1, ROLE_2, ROLE_3]
My case works with Spring Security and Grails.
I'm not sure this is 100% Feng Shui solution :) as it's fast cooked but it works in our case.
Hope it helps.
我刚刚花了三天时间尝试正确配置 CAS。我遇到的问题之一是我必须明确指示 CAS 发布属性。我这样做的方法是:
FWIW,另一个问题是 casServiceValidationSuccess.jsp 确实包含将属性传递回响应的任何代码。当我发现你的问题时,我正在寻找解决方案。我注意到您已经重写了您的实现。
I just spent the last three days attempting to get CAS properly configured. One of the issues I encountered was that I had to explicitly instruct CAS to publish the properties. I did this by:
FWIW, the other issue is that casServiceValidationSuccess.jsp does contain any code to pass the properties back in the response. I was looking for a solution to this when I found your question. I notice that you have rewritten your implementation.
最终且完整的解决方案如下(对于此未记录的功能):
服务器端:
a.将
attributeRepository
添加到您的CredentialsToPrincipalResolver
。b.像
IPersonAttributeDao
一样实现your.package.YourPersonAttributeDao
。c.声明将传输到客户端的断言的属性。
d.修改
casServiceValidationSuccess.jsp
以显示属性(感谢xiongjiabin)。客户端。您可以通过执行以下操作获得所有属性:
<块引用>
由于格式问题,我无法发布最终解决方案的代码...如果您有兴趣,请告诉我,我将向您发送包含所有代码的电子邮件。
客户
The definitive and complete solution is the following (for this undocumented feature):
Server side:
a. Add an
attributeRepository
to yourCredentialsToPrincipalResolver
.b. Implement the
your.package.YourPersonAttributeDao
like anIPersonAttributeDao
.c. Declare the attributes that will be transmitted into assertion to client.
d. Modify the
casServiceValidationSuccess.jsp
to display the attributes (thx to xiongjiabin).Client side. You get all attributes by doing this:
除了 @xiongjiabin 提供的答案之外,如果您使用 CAS v4+,您可能希望在 casServiceValidationSuccess.jsp< 中使用
assertion.primaryAuthentication
而不是assertion.chainedAuthentications
/code>:如果您在 CAS v4+ 中使用
assertion.chainedAuthentications
,则allowedAttributes
的serviceRegistryDao
列表将被忽略,并且所有属性都将被返回。In addition to the answer provided by @xiongjiabin if you are using CAS v4+ you probably want to use
assertion.primaryAuthentication
instead ofassertion.chainedAuthentications
incasServiceValidationSuccess.jsp
:If you do use
assertion.chainedAuthentications
with CAS v4+ then theserviceRegistryDao
list ofallowedAttributes
will be ignored and all attributes will be returned.