我有一个web应用程序,用户可以连接到他们的配置文件。
当用户请求受保护的重新源(在本例中是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“相关联,”managedBean“是该页面的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);
}但是,我忽略了如何从经过公正身份验证的用户那里获取信息。你有什么想法吗?谢谢
发布于 2014-02-14 08:14:18
这可能来得太晚了,但当我在搜索中看到这个页面时,这可能会给其他人带来一些好处。
我们通过在会话中存储一个经过身份验证的用户来解决这个问题。可以使用:CustomLoginModule从您的((HttpRequest)PolicyContext.getContext("javax.servlet.http.HttpServletRequest")).getSession()检索会话
然后,您可以在需要时从会话中检索它。
在您的例子中,使用JSF,您可以将它存储在会话bean中。由于我不会在这里讨论的原因,注入不会自动在您的CustomLoginModule中执行,您需要请求注入,例如在初始化方法中。
这里有一个为类请求注入的示例方法:
public static <T> void programmaticInjection(Class clazz, T injectionObject) throws NamingException {
log.trace("trying programmatic injection for "+clazz.getSimpleName());
InitialContext initialContext = new InitialContext();
Object lookup = initialContext.lookup("java:comp/BeanManager");
BeanManager beanManager = (BeanManager) lookup;
AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz);
InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
CreationalContext creationalContext = beanManager.createCreationalContext(null);
injectionTarget.inject(injectionObject, creationalContext);
creationalContext.release();
}就这样叫它:programmaticInjection(CustomLoginModule.class, this);
然后,注入会话bean,插入用户信息,并按需要在配置文件备份bean中使用它。
https://stackoverflow.com/questions/21068898
复制相似问题