2017-06-01 115 views
0

我試圖保護我的web應用程序在tomcat web.xmltomcat-users.xml但它不工作。正確的登錄名和密碼導致401錯誤。如何在tomcat 8.5中配置基本認證? HTTP錯誤401

web.xml安全部分:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Web Application</web-resource-name> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>myuser</role-name> 
    </auth-constraint> 
</security-constraint> 

<security-role> 
    <role-name>myuser</role-name> 
</security-role> 

<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 

tomcat-users.xml

<?xml version="1.0" encoding="UTF-8"?> 
<tomcat-users xmlns="http://tomcat.apache.org/xml" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" 
       version="1.0"> 

    <role rolename="myuser"/> 
    <role username="myuser" password="myuser" role="myuser"/> 

</tomcat-users> 

回答

1

我認爲它缺少http-methods,所以你可以嘗試添加此:後

<http-method>GET</http-method> 
<http-method>POST</http-method> 

<url-pattern>/*</url-pattern>標籤

UPDATE

更新您的tomcat-users.xml文件,更改此:

<role username="myuser" password="myuser" role="myuser"/> 

此:

<user username="myuser" password="myuser" role="myuser"/> 
+0

我已經嘗試過這樣做,結果是一樣的。日誌中只有一處更改:'對於帶有URL模式的安全限制[/ *],僅包含HTTP方法[POST GET]。所有其他方法都被發現。' – SemperPeritus

+0

嘗試更新您的'tomcat-users.xml'文件,檢查更新條目 –

+0

謝謝!改變'用戶'上的'角色'作品。我的錯字。 – SemperPeritus