[英]Error 403 when using Basic Authentication with Tomcat 8
嗨,大家好,
我按照以下鏈接中的步驟完成了對Tomcat的基本身份驗證,並成功實現了以下結果:
http://www.avajava.com/tutorials/lessons/how-do-i-use-basic-authentication-with-tomcat.html?page=1
之后,我在正在處理的現有項目上完成了同樣的工作,但不幸的是,嘗試從瀏覽器或通過Java程序訪問servlet時,出現錯誤403(禁止訪問指定的資源)。 。 錯誤403表示用戶已通過身份驗證,但未獲得授權。
我對此問題進行了深入的研究,但仍然受困於此,在此感謝您的幫助。
這是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"/>
她是我的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>
我的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);
responseCode值為403。
我確保在web.xml中包含
<auth-method>BASIC</auth-method>
正如我在網上看到的許多建議那樣做。
另一方面,如果在Java代碼中輸入了錯誤的用戶名和密碼,則會收到錯誤401,它是正確的,因為這意味着用戶未通過身份驗證。
我清除了瀏覽器緩存並退出了。 還是同樣的問題。
問題可能與代理設置有關嗎???
我的Windows代理設置如下:使用自動配置腳本,地址: http:// proxy:8083 /
我也嘗試了以下代碼:
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);
我嘗試在HttpURLConnection con1之前包括以下內容:
System.setProperty("http.nonProxyHosts", "<MY_LOCAL_HOST>");
但仍然沒有運氣。
謝謝!
我只是替換為tomcat-user.xml:role =“ adapter-role”
與
角色 =“ adapter-role”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.