简体   繁体   English

在Tomcat 8中使用基本身份验证时出现错误403

[英]Error 403 when using Basic Authentication with Tomcat 8

Hi Everyone, 嗨,大家好,

I followed the steps in the below link to accomplish Basic Authentication with Tomcat and I was successfully able to achieve the result: 我按照以下链接中的步骤完成了对Tomcat的基本身份验证,并成功实现了以下结果:

http://www.avajava.com/tutorials/lessons/how-do-i-use-basic-authentication-with-tomcat.html?page=1 http://www.avajava.com/tutorials/lessons/how-do-i-use-basic-authentication-with-tomcat.html?page=1

I went then afterward to accomplish the same on an existing project I'm working on but I'm unfortunately getting error 403 ( Access to the specified resource has been forbidden) when trying to access the servlet either from the browser or through a java program. 之后,我在正在处理的现有项目上完成了同样的工作,但不幸的是,尝试从浏览器或通过Java程序访问servlet时,出现错误403(禁止访问指定的资源)。 。 Error 403 means the user was authenticated but not authorized. 错误403表示用户已通过身份验证,但未获得授权。

I did an in depth research on that issue but still I'm stuck there, so appreciate your help here. 我对此问题进行了深入的研究,但仍然受困于此,在此感谢您的帮助。

Here is the tomcat-user.xml: 这是tomcat-user.xml:

 <role rolename="tomee-admin"/> <role rolename="adapter-role"/> <user password="tomee" roles="tomee-admin,manager-gui" username="tomee"/> <user username="adapter" password="adapterpwd" role="adapter-role"/> 

Her is my web.xml: 她是我的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>sbee-adapter</display-name> <security-constraint> <web-resource-collection> <web-resource-name>OZZ-Adapter</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>OZZ-Adapter</web-resource-name> <url-pattern>/API</url-pattern> </web-resource-collection> <auth-constraint> <role-name>adapter-role</role-name> </auth-constraint> </security-constraint> <servlet> <description></description> <servlet-name>IDP</servlet-name> <servlet-class>com.xxxx.ozz.adapter.ztee.IDP</servlet-class> </servlet> <servlet-mapping> <servlet-name>IDP</servlet-name> <url-pattern>/IDP</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>API</display-name> <servlet-name>API</servlet-name> <servlet-class>com.xxxx.ozz.adapter.ztee.API</servlet-class> </servlet> <servlet-mapping> <servlet-name>API</servlet-name> <url-pattern>/API</url-pattern> </servlet-mapping> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app> 

My java code: 我的Java代码:

HttpURLConnection con1 = (HttpURLConnection) urlSBEE_API.openConnection();
                con1.setRequestMethod("POST");
                String basis_encode = Base64.getEncoder().encodeToString(("adapter" + ":" + "adapterpwd").getBytes("UTF-8"));
                con1.setRequestProperty("Authorization", "Basic " + basis_encode);
    responseCode = con1.getResponseCode();
    System.out.println("Response code====== "+responseCode);

the responseCode value is being 403. responseCode值为403。

I made sure to include in the web.xml the 我确保在web.xml中包含

<auth-method>BASIC</auth-method>

as I saw on the net many suggestions to do that. 正如我在网上看到的许多建议那样做。

On the other hand if in the java code I enter a wrong username and pwd then I get error 401 which is correct since it means user not authenticated. 另一方面,如果在Java代码中输入了错误的用户名和密码,则会收到错误401,它是正确的,因为这意味着用户未通过身份验证。

I cleared browser cache and retired. 我清除了浏览器缓存并退出了。 still same issue. 还是同样的问题。

Could the issue be related to Proxy setting??? 问题可能与代理设置有关吗???

My windows proxy settings are as follow: Use Automatic Configuration script, Address: http://proxy:8083/ 我的Windows代理设置如下:使用自动配置脚本,地址: http:// proxy:8083 /

I tried the following code as well: 我也尝试了以下代码:

     Proxy proxy = new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress("Proxy"),
                                    Integer.parseInt("8083"));
        HttpURLConnection con1 = (HttpURLConnection) urlDistribution.openConnection(proxy);
    con1.setRequestMethod("POST");
    String basis_encode = Base64.getEncoder().encodeToString(("adapter" + ":" + "adapterpwd").getBytes("UTF-8"));
    con1.setRequestProperty("Authorization", "Basic " + basis_encode);
    responseCode = con1.getResponseCode();
   System.out.println("Response code====== "+responseCode);

I tried including the following before HttpURLConnection con1 : 我尝试在HttpURLConnection con1之前包括以下内容:

System.setProperty("http.nonProxyHosts", "<MY_LOCAL_HOST>");

but still no luck. 但仍然没有运气。

Thank you! 谢谢!

SOLUTION: 解:

I just replaced in tomcat-user.xml: role="adapter-role" 我只是替换为tomcat-user.xml:role =“ adapter-role”

WITH

roles ="adapter-role" 角色 =“ adapter-role”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM