cscsaba
cscsaba

Reputation: 1289

JSF 2 and the Basic HTTP authentication

We have an web app what uses Basic HTTP authentication.

web.xml

...
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>file</realm-name>
</login-config>
...

Indirect usage somewhere in the deep code space ...

User getLogedUser(HttpServletRequest request)

And I have to rewrite it in JSF 2, but I have no clue how can use the this authentication method in JSF 2. (i could not find the way how can i get 'HttpServletRequest request')

Google did not throw any useful hit on his first page

Thanks for the answers in advance.

Upvotes: 1

Views: 1320

Answers (2)

BalusC
BalusC

Reputation: 1109665

The raw HttpServletRequest is in JSF available by ExternalContext#getRequest().

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
// ...

However, I think it's better to change the getLoggedUser() method to take the remote user instead.

User getLoggedUser(String remoteUser)

so that you can just feed either ExternalContext#getRemoteUser() or HttpServletRequest#getRemoteUser() to it. This also decouples the method from any Servlet API or JSF API dependency.

Upvotes: 1

Ingo
Ingo

Reputation: 429

I guess you are looking for ExternalContext.getRemoteUser() which returns the user name.

Usage:

FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();

Upvotes: 1

Related Questions