1

ログイン情報を保持するためにステートフルEJBを使用しています。

@Stateful
public class SecurityService {

    private static final Logger log4jLogger = Logger.getLogger(SecurityService.class);

    @Inject UtenteDao utenteDao;
    @Inject AutorizzazioneDao autorizzazioneDao;

    private Utente utenteCorrente;

    private Negozio negozioCorrente;

    public SecurityService() {

    }

    public boolean authenticate() {

        boolean result = false;

        Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if (principal!=null) {
            utenteCorrente = utenteDao.findByUsername(principal.getName());
        }

        if (negozioCorrente!=null && utenteCorrente!=null) {
            Autorizzazione a = autorizzazioneDao.cercaPerNegozioAndUtente(negozioCorrente, utenteCorrente);
            result = a!=null;
        }

        return result;
    }

//...}

私のJSFログインページは以下によって制御されています:

@Named
@RequestScoped
public class LoginController {

@Inject private SecurityService securityService;

private String username;    
private String password;

private Negozio negozio;

public void login() throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

    try {

        if (request.getUserPrincipal() != null) {
            request.logout();
        }
        request.login(username, password);

        securityService.setNegozioCorrente(negozio);
        if (!securityService.authenticate()) {
            throw new ServletException("Utente non abilitato.");
        }

        externalContext.redirect("/pippo/");

    } catch (ServletException e) {
        e.printStackTrace();
        context.addMessage(null, new FacesMessage("Accesso Negato"));        
    }
}

public void logout() throws IOException {
//...

}

public String getLoggedUsername() {
    Utente utenteCorrente = securityService.getUtenteCorrente();
    String fullName = "";
    if (utenteCorrente!=null) {
        fullName = utenteCorrente.getNomeCompleto();
    } else {
        System.out.println("Utente NULLO");
    }
    return fullName;
}
//... 

}

私のユーザーは実際に私が望む方法でログインできます(私のドメインからのいくつかの追加によるプログラムによるセキュリティ)。

私が抱えている問題は、あなたがすでにログインしている次のページにあります。すべてのページのヘッダーに「ようこそ!あなたはとしてログインしています#{loginController.loggedUsername}

null を取得し続けますsecurityService.getUtenteCorrente()

SecurityService EJBは、ステートレスセッションBeanのように動作します。ステートフルEJBについて何か誤解しているのか、それとも期待どおりに機能するために何かを省略しただけなのかを知りたいのですが。

私の目標は、ユーザーの状態を維持するための「セッション全体」のBeanを用意することです。EJBは必要ですか、それともSessionScoped JSF ManagedBeanを使用できますか?

4

2 に答える 2

4

LoginControllerはリクエスト スコープであり、yourSecurityServiceは依存スコープです (すべての目的において、そのように指定しない限り、セッション スコープではありません)。したがって、2 番目の JSF ページLoginControllerが EL 式で を参照すると、 SFSBLoginControllerの別のインスタンスへの参照を持つ の新しいインスタンスが作成されます。SecurityService

元のインスタンスにアクセスする必要がある場合は、のようなクライアントがリクエスト間でアクセスできるように、SecurityServiceそれを としてマークする必要があります。ただし、このタスクはマネージド Beanで実行できるため、そもそもなぜアノテーションが必要なのかを検討することもできます。ユーザー/プリンシパル オブジェクトへの参照を格納するために SFSB は実際には必要ありません。@SessionScopedLoginController@Stateful@SessionScoped

于 2013-01-14T08:54:15.063 に答える
-2

セッション Bean を管理するには、アノテーションを使用して宣言する@EJBか、JNDI を使用してルックアップする必要があります。注入する方法は、アプリ サーバーによって管理されないプレーンなオブジェクトを提供するだけです。実際、それを使用するたびに新しいオブジェクトを作成します。 .

于 2013-01-14T08:53:23.317 に答える