题
我想从web应用程序返回401错误代码触发一个基本的认证过程,但Tomcat是劫持来显示其状态报告页面的响应。 有没有办法阻止这样做的tomcat,让错误代码一路去到浏览器?
UPDATE 我的错误:我忘了WWW-Authenticate头
解决方案
响应状态设置为401只告诉浏览器“禁止”,并tomcat会显示错误页面。它本身并不会触发认证过程。
要触发AUTH,则需要另一个头添加到响应:
httpResponse.setHeader("WWW-Authenticate", "Basic realm=\"MyApp\"");
其中“MyApp的”是你所需的HTTP认证领域的名称。添加报头,并且在浏览器弹出一个对话框AUTH和重试请求。
您应该知道,tomcat会仍然有标题一起发送的错误页面,它只是浏览器不会只要响应包含AUTH标题显示出来。
其他提示
不幸与附带的Tomcat默认的错误页面?您可以定义在web.xml文件中自己的自定义错误页。在下面所示的例子中,我们定义2次的网页 - server_error.html和file_not_found.html - 。当服务器遇到错误500或分别错误404将显示
<error-page>
<error-code>500</error-code>
<location>/server_error.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/file_not_found.html</location>
</error-page>
您应该牢记,然而,对于web.xml文件中标签的顺序是非常重要的。如果在你的web.xml文件中的顺序不正确,在启动Tomcat将输出误差
在Tomcat的错误页面可能发生的,因为你不包括您的浏览器做你接下来想要足够的信息。虽然你看到一个网页它不会有一个200个状态码。如果您检查头,你会在那里看到了401。我怀疑这是你的浏览器,不知道下一步该怎么做。
您不要在你的问题说明你希望浏览器做什么,但也许你需要在401错误消息的HTTP头的额外信息。您可以通过添加一个error-page
节点到web.xml文件覆盖默认的错误消息。该位置可以是servlet或JSP,这样可以提供一些逻辑,而这只是一个静态页面。
<error-page>
<error-code>401</error-code>
<location>/my401.jsp</location>
</error-page>
上面的答案是不是100%清楚,所以这是帮助我。我做了一个静态页面401.html和经历了同样的问题原来的海报。我所做的只是在页面更改为401.jsp页面和转储顶部这一权利:
<%
String realmName = "A nice name to show as the title of on the pop-up";
response.setHeader("WWW-Authenticate","Basic realm=\"" + realmName + "\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
%>
因此概括地说,web.xml中的有关部分是这样的:
<!-- Internal server error. -->
<error-page>
<error-code>500</error-code>
<location>/errors/500.html</location>
</error-page>
<!-- Not found. -->
<error-page>
<error-code>404</error-code>
<location>/errors/404.html</location>
</error-page>
<!-- Unauthorized. -->
<error-page>
<error-code>401</error-code>
<location>/errors/401.jsp</location>
</error-page>
和401.jsp看起来像这样:
<%
String realmName = "A nice name to show as the title of on the pop-up";
response.setHeader("WWW-Authenticate","Basic realm=\"" + realmName + "\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>401 Unauthorized</title>
<meta name="description" content="The server has not found anything matching the Request-URI.">
<style type="text/css">
body {background-color:ffffff;background-image:url(http://);background-repeat:no-repeat;background-position:top left;background-attachment:fixed;}
h3{font-family:Arial;color:000000;}
p {font-family:Arial;font-size:14px;font-style:normal;font-weight:normal;color:000000;}
</style>
</head>
<body>
<h3>401 Unauthorized</h3>
<p>The request requires authentication.</p>
</body>
</html>
为什么不使用的登录,配置的的安全约束和安全角色的web.xml文件从元素呢?小例如:
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Your-Name</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error_login.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Some-Name</web-resource-name>
<url-pattern>/path/to/directory/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>USER</role-name>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<description>Role for all users</description>
<role-name>USER</role-name>
</security-role>
<security-role>
<description>role for all admins</description>
<role-name>ADMIN</role-name>
</security-role>
使用谁想要访问你的/路径用户/到/目录/或任何它的子目录需要登录。 Access将如果用户具有角色用户或管理员可以仅授予。您可以设置这甚至到根目录,以便所有用户必须先登录..