【发布时间】:2014-01-30 21:03:08
【问题描述】:
我有一个网络应用程序,用户可以在其中连接到他们的个人资料。
当用户请求受保护的资源(在本例中为 profile.xhtml 页面)时,我能够使用 JAAS 对数据库进行身份验证。
身份验证后,我可以访问个人资料页面,该页面显然是空的(没有从数据库中传输任何数据)。
所以,我的问题是:我不知道如何在身份验证后为他提供适当的个人资料(充满此用户信息)。换句话说:
1) 我如何从登录模块获取用户名(用户表的这个 id)到我的个人资料页面?
2)如何将用户元组的信息放入服务的JSF页面中,以便呈现给用户?
这是我的登录模块(有点长,所以我只剪掉了登录方法):
@Override
public boolean login() throws LoginException {
if (callbackHandler == null) {
throw new LoginException("Error: no CallbackHandler available "
+ "to garner authentication information from the user");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("username");
callbacks[1] = new PasswordCallback("password: ", false);
try {
callbackHandler.handle(callbacks);
username = ((NameCallback) callbacks[0]).getName();
password = ((PasswordCallback) callbacks[1]).getPassword();
if (debug) {
System.out.println("Username :" + username);
System.out.println("Password : " + password);
}
if (username == null || password == null) {
System.out.println("Callback handler does not return login data properly");
throw new LoginException(
"Callback handler does not return login data properly");
}
if (isValidUser()) { // validate user.
Profile profile = new Profile();
profile.setUsername(username);
succeeded = true;
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedCallbackException e) {
e.printStackTrace();
}
return false;
}
这是我在请求 profile.xhtml 页面后弹出的表单身份验证(我在 web.xml 中配置了此规则):
<form method=post action="j_security_check">
<p>
<span>Username:</span> <br /> <input type="text" name="j_username">
</p>
<p>
<span>Password:</span> <br /> <input type="password"
name="j_password">
</p>
<p>
<input type="submit" value="Login">
</p>
</form>
这是我个人资料的 jsf 页面的一部分,与“profile.java”相关联,“profile.java”是该页面的 managedBean。我不知道如何在认证后获取信息并将其放入 managedBean 并提供给他的 profile jsf 页面:
<h:form>
<h:outputText value="#{profile.username}"/><br/>
<h:outputText value="#{profile.password}"/><br/>
</h:form>
如果您需要其他代码,请告诉我。 谢谢
好的,现在我可以使用 @PostConstruct 注释在我的 profile.xhtml 页面中显示一些垃圾信息,如下所示:
@PostConstruct
private void prepareProfile(){
Subject subject = new Subject();
username = String.valueOf(subject.getPrincipals().size());
System.out.println(username);
}
但是,我忽略了如何从刚刚通过身份验证的用户那里获取信息。 你有什么主意吗? 谢谢
【问题讨论】:
标签: jaas