서블릿 보안 동적 어노테이션
프로그램화된 API를 사용하여 서블릿을 추가 또는 작성하는 경우, 보안 어노테이션, RunAs, declareRoles, ServletSecurity는 각각 setRunAsRole(), declareRoles(), setServletSecurity()를 사용하여 동적으로 업데이트 가능합니다.
애플리케이션 시작 시, 웹 컨테이너는 RunAs, declareRoles 및 ServletSecurity 어노테이션이 있는 모든 서블릿을 조사하여 ServletRegistration 어노테이션의 setServletSecurity() 메소드에서 해당 어노테이션을 설정합니다. 웹 컨테이너는 URL 패턴 및 보안 제한조건이 있는 모든 ServletRegistration 어노테이션을 조사하도록 보안 컴포넌트에 알립니다. 그러면 보안 컴포넌트는 배치 디스크립터에 URL 패턴이 정의되어 있는지 판별합니다. 배치 디스크립터에 일치 항목이 이미 정의되어 있으면 동적 데이터 대신 배치 디스크립터의 URL 패턴에서 보안 제한조건과 RunAs 역할을 사용합니다.

보안 동적 어노테이션에서 ServletSecurity 어노테이션에 정확하게 일치하는 URL 패턴이 있는 경우, 보안 동적 어노테이션에서 URL 패턴의 보안 제한사항이 우선적으로 사용됩니다. 또한, 동일한 URL 패턴으로 setServletSecurity() 메소드를 여러 번 호출하면 마지막 호출이 사용됩니다.
- ServletRegistration.Dynamic.setRunAsRole(String roleName)은 이 서블릿 등록을 위한 RunAs 역할 이름을 설정합니다.
- ServletContext.declareRoles(String roleNames)는 isUserInRole() 메소드에 테스트된 역할 이름을 선언합니다.
- ServletRegistration.Dynmaic.setServletSecurity(ServletSecurityElement constraint)는 이 서블릿 등록에 대해 ServletSecurityElement를 설정합니다.
다음 두 예는 setServletSecurity() 메소드를 사용하여 동적 서블릿에 대해 보안 제한조건 및 RunAs 역할을 설정하는 데 사용할 수 있습니다.
HttpConstraintElement constraint = new HttpConstraintElement(TransportGuarantee.NONE,
new String[]{"Employee"});
List<HttpMethodConstraintElement> methodConstraints =
new ArrayList<HttpMethodConstraintElement>();
methodConstraints.add(new HttpMethodConstraintElement("PUT",
new HttpConstraintElement(TransportGuarantee.CONFIDENTIAL, new String[]{"Manager"})));
ServletSecurityElement servletSecurity =
new ServletSecurityElement(constraint, methodConstraints);
HttpConstraintElement constraint = new HttpConstraintElement();
List<HttpMethodConstraintElement> methodConstraints =
new ArrayList<HttpMethodConstraintElement>();
methodConstraints.add(new HttpMethodConstraintElement("CUSTOM",
new HttpConstraintElement(TransportGuarantee.NONE, new String[]{"Manager"})));
methodConstraints.add(new HttpMethodConstraintElement("GET",
new HttpConstraintElement(TransportGuarantee.CONFIDENTIAL, new String[]{"Employee"})));
ServletSecurityElement servletSecurity = new ServletSecurityElement(constraint,
methodConstraints);