
Tomcat 7 서버에 REST 애플리케이션을 구축 중입니다.
부트스트랩에 애플리케이션 클래스를 사용합니다.
애플리케이션 클래스 구현은 다음과 같습니다. 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;
}
}
리소스 클래스는 다음과 같습니다.
두 가지 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>
그런 다음 동적 웹 프로젝트를 war 파일로 내보내고 war 파일을 배포합니다. 테스트 목적으로 ARC(Advanced Rest Client)를 사용하였습니다.
처음에는 GET 요청을 시도했는데 응답이 수신되었습니다. 스크린샷 첨부.
요청 받기
스크린샷에서 볼 수 있듯이 200 OK 응답이 수신됩니다. 브라우저에서 원하는 문자열을 볼 수 있습니다.
PUT 요청을 시도할 때. 403 Forbidden 오류가 발생합니다. 동일한 내용을 설명하는 몇 가지 스크린샷을 첨부합니다.
PUT_REQUEST_HEADER
PUT_REQUEST-PAYLOAD
PUT_RESPONSE
어떤 도움이나 안내도 부탁드립니다.
안부 인사,
아시쉬
답변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>
그리고 그것은 효과가 있었습니다!