2014-01-11 160 views
0

我有一個Web應用程序,用戶可以連接到其配置文件。使用JAAS進行身份驗證

當他請求受保護的資源(在本例中爲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並送達給他概況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); 
} 

不過,我忽略瞭如何抓住信息來自剛認證的用戶。 你有什麼想法嗎? 謝謝

回答

1

這可能來得太晚,但當我在我的搜索過程中遇到此頁面時,這可能會讓其他人感覺良好。

我們通過在會話中存儲經過身份驗證的用戶來解決此問題。 您可以使用以下方法從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。