java - Http 状态 401 此请求需要 HTTP 身份验证 ()。在 tomcat 6

标签 java xml tomcat

我开发了一个网络应用程序 enter image description here

如果我在 tomcat 6 中运行应用程序,它会要求输入用户名和密码 但经过 3 次尝试后显示 enter image description here web.xml 文件是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>LoginRemote</display-name>
  <welcome-file-list>

    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>

    <description></description>
    <display-name>remoteSample</display-name>
    <servlet-name>remoteSample</servlet-name>
    <servlet-class>com.src.remoteSample</servlet-class>
  </servlet>

  <servlet-mapping>

    <servlet-name>remoteSample</servlet-name>
    <url-pattern>/remoteSample</url-pattern>
  </servlet-mapping>
  <security-constraint>
      <web-resource-collection>
        <web-resource-name>Secure</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
      </web-resource-collection>
     <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>


    </security-constraint>
<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>Authentication required</realm-name>
</login-config>
<security-role>
      <role-name>*</role-name>
   </security-role>
</web-app>

我想获取远程用户的用户名和密码,我应该怎么做? 这是我的 servlet 代码

package com.src;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.buf.Base64;

public class remoteSample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public remoteSample() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.setContentType("text/html;charset=UTF-8");
        System.out.println("login info:"+request.getRemoteUser());
        System.out.println("Authentication type:"+request.getAuthType());

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

我应该更改服务器的 tomcat-server.xml 和 web.xml 中的某些内容吗?

最佳答案

检查您在服务器和远程用户中使用的域名是否与以下名称匹配。

<realm-name>Authentication required</realm-name>

请检查这个link有关异常的更多信息

关于java - Http 状态 401 此请求需要 HTTP 身份验证 ()。在 tomcat 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359550/

相关文章:

java - HttpURLConnection urlConnection = null; (错误)

Java 不希望我在使用 inputStream 之前关闭 outputStream?

java - 解决 IntelliJ IDEA 中持续无效的目标发布问题

tomcat - Sysdeo Tomcat DevLoader - Java 类的热部署导致整个应用程序重新启动

java - 从 Netbeans 启动 Tomcat 失败

Java - 在 GUI 中构建从输入到输出的数据流

java - 使用java根据xml标签将xml拆分为多个xml

.net - 与 xsd :anytype 的 BizTalk 映射

Python输出乱序

java.io.NotSerializableException 数据类型