Must close browser to get authentication data in P

2019-08-29 11:03发布

问题:

I am trying to cache the authentication information in a SessionScoped managed bean.

When I open a browser and login into the server (the browser asks me for username/password) the first time, it works as it should.

The trouble comes when I restart the webapp or the server (it is a development setup). Then, accessing the webapp from one of the browser where I had previously logged in, causes that in my @PostConstruct method, FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal() returns null (I check that it is effectively executed).

On the other hand, if I just check that value from getUser(), it works correctly.

AFAIK, I expected the browser the just cache the credentials and that when I was reentering the application after a restart the only difference was that the browser would automatically send the credentials without prompting me again. I did not expect that it would make any difference in the server.

The code is the following (simplified)

@ManagedBean
@SessionScoped
public class UserManager {

private Principal userPrincipal = null;

@PostConstruct
public void init() {
  this.userPrincipal = 
    FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
  System.out.println("EN POSTCONSTRUCT DE LDAP PRINCIPAL!! " + this.userPrincipal);
}

public String getUser() {
  return this.userPrincipal.getName();
}

}

The setup is JBoss 6.1 Final with Mojarra 2.03, JDK 6. I have tested it both with IE7 and Firefox.

UPDATE: I have found more about it. If I go into my welcome page, then the webapp works as expected. It is when I try to directly access another page (*1) that it fails to initialize the user info.

*1: I am not talking about reloading the page as this always fails, but typing http://myserver/mywebapp/page_that_is_not_welcome_one.xhtml in the URL bar.