身份验证后以编程方式添加角色
我有以下 JSF 2.1 登录表单,在 Glassfish 3.1 中运行
<h:form id="loginForm">
<h:panelGrid columns="2" cellspacing="5">
<h:outputText value="Username" />
<h:inputText value="#{loginHandler.username}" />
<h:outputText value="Password:" />
<h:inputText value="#{loginHandler.password}" />
<h:outputLabel value="" />
<h:commandButton value="Login" action="#{loginHandler.login}" />
</h:panelGrid>
</h:form>
以及以下支持 bean。
public String login() throws IOException, LoginException {
log.debug("Trying to login with username " + username);
HttpSession session = getRequest().getSession(true);
try {
getRequest().login(username, password);
// if OK, add Roles
????????
...................
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.debug("USER principal === " + getRequest().getUserPrincipal());
return "home";
}
问题是,成功登录后如何以编程方式将角色添加到 UserPrincipal 中?
更新 1:我尝试使用以下代码获取主题,但主题 == null。
Subject thisSubject = Subject.getSubject(AccessController
.getContext());
谢谢, 科恩
I have the following JSF 2.1 login form, running in Glassfish 3.1
<h:form id="loginForm">
<h:panelGrid columns="2" cellspacing="5">
<h:outputText value="Username" />
<h:inputText value="#{loginHandler.username}" />
<h:outputText value="Password:" />
<h:inputText value="#{loginHandler.password}" />
<h:outputLabel value="" />
<h:commandButton value="Login" action="#{loginHandler.login}" />
</h:panelGrid>
</h:form>
And the following backing bean.
public String login() throws IOException, LoginException {
log.debug("Trying to login with username " + username);
HttpSession session = getRequest().getSession(true);
try {
getRequest().login(username, password);
// if OK, add Roles
????????
...................
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.debug("USER principal === " + getRequest().getUserPrincipal());
return "home";
}
The question is, how can I add roles programmatically to the UserPrincipal after successful login?
Update 1: I tried to get the Subject by using the following code but subject == null.
Subject thisSubject = Subject.getSubject(AccessController
.getContext());
Thanks,
Coen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了以下解决方案,在登录后以编程方式添加角色,该解决方案至少适用于 GlassFish 3.1.2 build 23。
您需要添加
security.jar
和common-util.jar
从 GlassFish 到您的项目库。并且不要忘记在 web.xml 中为您要添加的角色创建
部分。请注意,我使用的功能似乎不属于已发布的稳定 API 的一部分,因此不能保证这将在 GlassFish 的未来版本中继续工作。
我从GlassFish的
sun.appserv.security.AppservPasswordLoginModule.commit()
的源代码中获取了如何添加角色的信息。如果未来的 GlassFish 版本破坏了我的代码,那么这个函数将是一个很好的起点,以便找出如何修复它。
I came up with the following solution to add roles programmatically after login, which works at least on GlassFish 3.1.2 build 23.
You will need to add
security.jar
andcommon-util.jar
from GlassFish to your project libraries.And don't forget to create a
<security-role>
section in your web.xml for the roles you wish to add.Note that I am using functionality which does not appear to be part of a published stable API, so there is no guarantee that this will keep working in future releases of GlassFish.
I got the information on how to add roles from the source code of
sun.appserv.security.AppservPasswordLoginModule.commit()
of GlassFish.If a future GlassFish release breaks my code, this function would be a good place to start in order to find out how to fix it.
好的,我已经找到了一个解决方法,在我看来这并不是 100% 正确,但欢迎提出建议:)
我还使用以下信息 bean 来检索主题并检查主体。
所以
我解决了 isUserInRole 机制,真正的 isUserInRole 方法仅在 USER 上返回 true,因为该角色是在身份验证时设置的。
我现在可以从 JSF 页面做
希望这可以帮助其他用户,欢迎任何改进建议!
OK, i have figured out a workaround, which is not 100% correct in my point of view but suggestions are welcome :)
Also I use the following info bean to retrieve the subject and check the principals.
}
So I workaround the isUserInRole mechanism, the real isUserInRole method returns only true on USER, because that role is set when authenticating.
From the JSF pages I can now do
Hope this helps other users, any improvement suggestions are welcome!