
Tomcat 7 サーバー上で REST アプリケーションを構築しています。
ブートストラップに Application クラスを使用します。
アプリケーション クラスの実装は次のとおりです。Resource01 のシングルトン オブジェクトを作成します。
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.somename.api.Resource01;
@ApplicationPath("/service")
public class AppConfig extends Application{
public Set<Object> all_resources;
@Override
public Set<Object> getSingletons()
{
all_resources = new HashSet<Object>();
Resource01 z = new Resource01();
all_resources.add(z);
return all_resources;
}
}
リソース クラスは次のとおりです。2
つの http メソッド PUT と GET をサポートしています。
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("resource")
public class Resource01 {
@GET
@Path("mailer_status")
@Produces(MediaType.TEXT_HTML)
public String getMailStatus()
{
return "<h2>Mailer is in progress 14.47</h2>";
}
@PUT
@Path("create_config")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String getMailConfig(String config)
{
//String instance_rep = m_config.toString();
return "Payload Delivered";
}
}
tomcat-users.xml は次のとおりです。
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>
ファイルは%CATALINA_HOME%/confにあります
失敗の原因についていくつか調べ、それに基づいて%CATALINA_HOME%/webapps/manager/WEB-INFにあるweb.xmlを変更しました。
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager-gui</role-name>
</auth-constraint>
<!-- Specifying a Secure Connection -->
<user-data-constraint>
<!-- transport-guarantee can be CONFIDENTIAL (forced SSL), INTEGRAL, or NONE -->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Tomcat Manager Application</realm-name>
</login-config>
次に、動的 Web プロジェクトを WAR ファイルとしてエクスポートし、WAR ファイルをデプロイします。テスト目的で、ARC (高度な REST クライアント) が使用されます。
最初に GET 要求を試しました - 応答が受信されました。スクリーンショットを添付します。
GETリクエスト
スクリーンショットでわかるように、200 OK 応答が受信されました。ブラウザで目的の文字列を確認できます。PUT
リクエストを試みると、403 Forbidden エラーが発生します。同じことを説明するスクリーンショットをいくつか添付します。
PUT_REQUEST_HEADER
PUT_REQUEST-PAYLOAD
応答を返す
どのような助けや指導でも大歓迎です。
よろしく、
アシッシュ
答え1
conf フォルダーに保存されている web.xml を確認しませんでした。
そこにはこれがありました
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
PUTメソッドをコメントしました。
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<!--<http-method>PUT</http-method>-->
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
そしてそれはうまくいきました!