9 apache tomcat basic-authentication jsf-2
我正在为我的网络应用程序使用tomcat基本身份验证:
我在web应用程序中向web.xml添加了以下行:
<security-constraint>
<web-resource-collection>
<web-resource-name>webpages</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
<user-data-constraint>
<!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<role-name>*</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)
我的退出链接:
<h:commandLink value="Logout" action="#{userBean.logout}" />
Run Code Online (Sandbox Code Playgroud)
我的注销链接操作:
public void logout() throws IOException
{
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getExternalContext().redirect("add_international_job.faces");
}
Run Code Online (Sandbox Code Playgroud)
现在,当调用logout时,它会重定向到另一个应该要求身份验证的页面.但是它会在用户登录时呈现.PS:当用户第一次在地址栏中键入同一页面的URL时,会向他显示身份验证质询(这意味着在保护页面密码时没有问题) .
Bal*_*usC 16
您正在使用HTTP BASIC
身份验证而不是HTTP FORM
身份验证j_security_check
.该BASIC
认证是通过完成Authorization
从浏览器端请求报头,这是会话独立的.
要强制"注销" BASIC
身份验证,服务器基本上需要返回401响应.
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.invalidateSession();
externalContext.responseSendError(401, "You are logged out.");
facesContext.responseComplete();
Run Code Online (Sandbox Code Playgroud)
这将提出一个HTTP 401错误页面,这是自定义为<error-page>
在web.xml
.
您也可以返回带有元刷新的HTML页面,以便将最终用户重定向到元刷新标头内容中指定的所需目标页面.
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.invalidateSession();
externalContext.setResponseStatus(401);
externalContext.getResponseOutputWriter().write("<html><head><meta http-equiv='refresh' content='0;add_international_job.faces'></head></html>");
facesContext.responseComplete();
Run Code Online (Sandbox Code Playgroud)
这似乎确实很低级别和hacky,但BASIC
身份验证也是相当低的水平.使用FORM
身份验证时不需要这样做.只是使会话无效并发送正常的重定向应该用于FORM
身份验证.
归档时间: |
|
查看次数: |
10879 次 |
最近记录: |