Http status 401此请求需要HTTP身份验证()。在tomcat 6

时间:2015-02-06 06:07:49

标签: java xml tomcat

我开发了一个Web应用程序 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中的内容吗?

1 个答案:

答案 0 :(得分:-1)

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

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

请查看此link以获取有关例外的更多信息