【问题标题】:Authentication with JAAS使用 JAAS 进行身份验证
【发布时间】: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


    【解决方案1】:

    这可能来得太晚了,但是当我在搜索中遇到此页面时,这可能对其他人有好处。

    我们通过在会话中存储经过身份验证的用户来解决此问题。 您可以使用 ((HttpRequest)PolicyContext.getContext("javax.servlet.http.HttpServletRequest")).getSession()CustomLoginModule 检索会话

    然后,您可以在任何需要的地方从会话中检索它。

    在您的情况下,使用 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 中使用它。

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多