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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28359550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:05:48  来源:igfitidea点击:

Http status 401 This request requires HTTP authentication (). in tomcat 6

javaxmltomcat

提问by ambrosia1993

I have developed a web application enter image description here

我开发了一个网络应用程序 在此处输入图片说明

if i run the application in tomcat 6 it asks for username and password but after 3 attempsts it shows enter image description herethe web.xml document is

如果我在 tomcat 6 中运行该应用程序,它会要求输入用户名和密码,但经过 3 次尝试后,它会显示 在此处输入图片说明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>

i want to get the remote user's user name and password what should i do for it? this is my servlet code

我想获取远程用户的用户名和密码,我该怎么办?这是我的 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
    }

}

should i change something in my server's tomcat-server.xml and web.xml?

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

回答by Venky

check that realm name you are using in server & remote user is matched with following name or not.

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

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

please check this linkfor more information regarding exception

请查看此链接以获取有关异常的更多信息