JSF2 + CDI @Produces keeping old session state
I have an AuthenticatorBean declared like this:
@Named
@SessionScoped
public class AutenticadorBean implements Serializable {
@Inject
private AutenticadorService autenticadorService;
This bean calls a method inside the AutenticadorService that validates that user's credentials and @Produces in the session as shown below:
@SessionScoped
public class AutenticadorServiceImpl implements AutenticadorService, Serializable {
@EJB
private AnalistaRepository analistaRepository;
private Analista analistaSessao;
@Override
public void inserirSessaoDadosLogin(String login) {
analistaSessao = analistaRepository.buscaPorUserName(login);
}
@Produces
@Named("analistaSessao")
public Analista getAnalistaSessao() {
return analistaSessao;
}
}
The first time I log into the app, it produces a user in session, but the problem is after logging out and logging back in with a different user and try
@Inject Analista analistaSessao
inside another service, the attribute is loaded by the first user before I log out. But if I access analistaSessao directly in AutenticadorService.getAnalistaSessao (); it returns the desired user, which is the second I logged in.
Here's my exit code:
public String logout() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext ex = facesContext.getExternalContext();
ex.invalidateSession();
return "/home?faces-redirect=true&logout=true";
}
Does anyone know the reason for this behavior?
Thank.
source to share
I think what you have done should work because you are invalidating the session.
try removing @Produces and @Named in getAnalistaSessao () from your AutenticadorServiceImpl and add this @Model class to your code.
@Model
public class AnalistaProducer {
@Inject
private AutenticadorService autenticadorService;
@Produces
@Named
public Analista getAnalistaSessao() {
return autenticadorService.getAnalistaSessao();
}
}
source to share