2017-06-01 113 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