0

Stateful Session Beanログイン セッションを保持するとJSF Session BeanがありServlet Filterます。

私がしなければならないことは、ログインしていないユーザーが自分のページにアクセスできないようにすることです。そのため、フィルターをかけました。

そのdoFilter()ようなもの:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    String path = req.getRequestURI().substring(req.getContextPath().length());
    System.out.println(userManager.isLogged());
    if (userManager.isLogged() || path.equals("/") || path.equals("/index.xhtml") || path.startsWith(ResourceHandler.RESOURCE_IDENTIFIER) || path.startsWith("/resources/") || path.startsWith("/admin") || path.equals("/admin/login.xhtml")) {
        chain.doFilter(request, response);
    } else {
        request.getRequestDispatcher("/error.xhtml").forward(request, response);
    }
}

whereuserManagerは で見つかります:

private UserManagerLocal lookupUserManagerLocal() {
    try {
        Context c = new InitialContext();
        return (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

現在、System.out.println(userManager.isLogged());出力は常にfalse ですが、#{loginBean.logged}出力は true です。

loginBean.loggedちょうどであることに注意してください

public boolean isLogged() {
    return userManager.isLogged();
}

そして、私の管理対象 Bean では、次のようuserManagerに取得されます

@EJB
private UserManagerLocal userManager;

サーブレットは JSF Managed Bean と同じ SFSB を使用していないようです。

私は何を間違っていますか?

編集: 新しいコード

サーブレット

UserManagerLocal userManager = lookupUserManagerLocal();
private UserManagerLocal lookupUserManagerLocal() {
    try {
        Context c = new InitialContext();
        UserManagerLocal userM = (UserManagerLocal) c.lookup("java:global/UNILIFE/UNILIFE-ejb/UserManager!ejb.UserManagerLocal");
        HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        req.setAttribute("userManager", userM);
        return userM;
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

jsf ビーン

@PostConstruct
public void init(){
    HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    userManager = (UserManagerLocal) req.getSession().getAttribute("userManager");
}
4

2 に答える 2

1

ステートフル セッション Bean をルックアップするたびに、一意のインスタンスが作成されるため、isLogged はおそらくデフォルトのフィールド値を返します。何らかの方法でステートフル セッション Bean インスタンスを HttpSession に格納し、それをフィルターから取得する必要があります。私は JSF の専門知識がないので、ステートフル セッション Bean インスタンスを共有する便利な方法があるかどうか、または JSF Bean をステートフル Bean に手動でリンクする必要があるかどうかはわかりません。

于 2012-05-24T17:52:52.960 に答える
0

各 JNDI ルックアップは、新しいハンドルを Bean に返します。したがって、メソッドの呼び出し中にデフォルト値を取得し、以前のアクティビティを破棄します。他の投稿で説明されているように、その参照(HttpSession)をさらに操作するために保存する必要があります。

以下は、JSF Bean でセッションにアクセスするためのサンプル コードです。

HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

リクエストから、セッションを取得してそこに配置できます。

session.setAttribute("userManager", userManager);

フィルターで、リクエストから Bean を取得できるようになりました。また、それに応じてコードを変更します。

于 2012-05-24T18:19:26.493 に答える