2012-08-07 193 views
1

我们有一个使用基本HTTP身份验证的Web应用程序。JSF 2和基本HTTP身份验证

的web.xml

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

间接使用某处深代码空间...

User getLogedUser(HttpServletRequest request) 

我不得不重写它在JSF 2,但我不知道如何使用JSF 2中的这种身份验证方法(我找不到如何获得'HttpServletRequest请求'的方式)

谷歌没有在他的第一页上发现有用的命中

感谢您提前给出答案。

回答

1

原始HttpServletRequest是通过提供JSF ExternalContext#getRequest()

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

但是,我认为最好将getLoggedUser()方法改为使用远程用户。

User getLoggedUser(String remoteUser) 

,这样就可以只给任何ExternalContext#getRemoteUser()HttpServletRequest#getRemoteUser()它。这也将该方法从任何Servlet API或JSF API依赖关系中分离出来。

+0

非常感谢您的快速回复! – cscsaba 2012-08-08 07:52:00

1

我想你正在寻找ExternalContext.getRemoteUser() 它返回用户名。

用法:

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

非常感谢您的快速回复! – cscsaba 2012-08-08 07:56:48