後以編程方式添加角色,我有以下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