后以编程方式添加角色,我有以下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());
感谢, 科恩
是否要硬编码每个用户的角色?如果你使用容器认证,角色应该来自认证领域。 – oers 2012-02-01 09:18:55
嗨,我需要查询另一个LDAP树的某些角色,并将这些角色分配给用户。角色在web.xml中定义。因此,身份验证是容器管理的,但分配角色不是。 – 2012-02-01 09:31:13