2016-08-03 3 views
1

CXF를 사용하여 XSS 인터셉터 작업을하고 있습니다. 프로젝트 종속성에 따라 저지를 사용할 수 없습니다. 인터셉터 나 필터를 사용하여 요청에서 양식 데이터 (양식 매개 변수)를 변경할 수있는 방법이 없습니다. 저지가 이러한 매개 변수를 수정할 수있는 방법을 제공하지만 cxf를 사용하여 요청 매개 변수 (쿼리 매개 변수 제외)를 수정할 수 없음을 문서에서 발견했습니다.Apache CXF를 사용하는 Jax-rs 용 XSS 인터셉터

그러나 요격기에서도 양식 매개 변수를 수정하는 방법을 볼 수 없습니다. 헤더/쿼리 매개 변수를 수정하는 많은 예제를 볼 수 있습니다. 그러나 요청 매개 변수를 수정하는 예는 볼 수 없습니다. 누구나 도움을 줄 수 있고 샘플 코드 예제를 제공 할 수 있습니까?

지금은 ReaderInterceptor를 확인하고 있습니다. 그러나 인터셉터가 호출되는 방식도 호출하지 않습니다. 당신도 그것에 대해 알고 있다면 알려주십시오.

답변

0

bussiness 서비스에서 처리하기 전에 인바운드 메시지를 캡처하고, 양식 매개 변수를 추출하고, XSS 필터를 전달하고 매개 변수를 업데이트하거나 필요에 따라 처리를 중단하기 위해 인터셉터를 정의해야합니다.

인터셉터

이 CXF AbstractPhaseInterceptor

public class XSSInterceptor extends AbstractPhaseInterceptor<Message> { 

    public XSSInterceptor() { 
     super(Phase.INVOKE); 
    } 

    @Override 
    public void handleMessage(Message message) throws Fault { 

     // filter only application/x-www-form-urlencoded 
     String contentType = (String)message.get(Message.CONTENT_TYPE); 
     if (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)){/

      // message parts 
      List list = message.getContent(List.class); 
      for (int i = 0; i < list.size();i++){ 

       //get the parameter map 
       MultivaluedMap<String, String> params = (MultivaluedMap<String, String>)list.get(i); 
       for(String param: params.keySet()){ 
        List<String> values = params.get(param); 

        //XSS filter here. Update the value list if needed or abort the request 
        List<String> updatedValues = xssFilter(values); 
        params.put(param, updatedValues); 
       } 
      } 
     } 

    } 
    @Override 
    public void handleFault(Message messageParam) { 
    } 
} 

CXF 구성을 사용하는 기본 예이다

<bean id="xssInterceptor" class="XSSInterceptor" /> 
<cxf:bus> 
    <cxf:inInterceptors> 
     <ref bean="xssInterceptor"/> 
    </cxf:inInterceptors> 
</cxf:bus> 

이 구성이

,369 같은 서비스를 적용
@POST 
@Path("/form") 
@Consumes({MediaType.APPLICATION_FORM_URLENCODED}) 
public Response form(MultivaluedMap<String, String> params) throws WebApplicationException; 

* RequestContextFilter

는 다른 방법이 공용 클래스 XSSInterceptor을 @PreMatching RequestContextFilter

public class CustomRequestFilter implements ContainerRequestFilter { 
    public void filter(ContainerRequestContext context) { 
    Message m = JAXRSUtils.getCurrentMessage(); 
    //XSS filter here. In the same way the above intercerceptor 
    // finally use context.abortWith(Response) if you need to block the request 

봄 설정

<bean id="customRequestFilter" class="com.CustomRequestFilter" /> 

<!-- Add filters to provider zone in JAX-RS server--> 
<bean id="myRestServer" class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" lazy-init="false" init-method="create"> 
    ... 
    <property name="providers"> 
     <list> 
       <ref bean="customRequestFilter" /> 
     </list> 
    </property> 
+0

도움을 주셔서 감사합니다 XSSInterceptor는 정상적으로 작동하지만 CustomRequestFilter를 사용하여 양식 매개 변수를 업데이트 할 수 없습니다. 나는 그것을 시도했지만 실패했다. – Sandeep

+0

안녕하세요 @pedrofb, 휴식 서비스의 MediaType을 APPLICATION_FORM_URLENCODED에서 Application_json으로 변경하면 저를 도와주세요. 제 경우에는 UI가 Application_json 요청 만 보내고이 경우에는 실패합니다. – Sandeep

+0

'application/json'을 사용하면 매개 변수가 아닌 들어오는 스트림을 처리해야합니다. 'message.getContent (InputStream.클래스)', 문자열 데이터를 소비하고 처리하고'message.getExchange(). setContent (analyzStream)'로 새로운 내용을 설정한다. – pedrofb

0

를 사용할 수있는 것은 {

AbstractPhaseInterceptor를 확장
private XSSRequestWrapper xssRequestWrapper; 

public XSSInterceptor() { 
    super(Phase.POST_LOGICAL); 
} 

@SuppressWarnings("unchecked") 
@Override 
public void handleMessage(Message message) throws Fault { 
    HttpServletRequest httpRequest = (HttpServletRequest) message.get("HTTP.REQUEST"); 
    xssRequestWrapper = new XSSRequestWrapper(httpRequest); 

    // filter only application/x-www-form-urlencoded 
    String contentType = (String) message.get(Message.CONTENT_TYPE); 
    if (MediaType.APPLICATION_FORM_URLENCODED.equalsIgnoreCase(contentType) 
      || MediaType.APPLICATION_JSON.equalsIgnoreCase(contentType)) { 
     // message parts 
     List list = message.getContent(List.class); 
     String jsonString = ""; 

     for (int i = 0; i < list.size(); i++) { 
      jsonString = list.get(i).toString(); 
     } 

     Response response = Response.status(Response.Status.ACCEPTED) 
       .entity(xssRequestWrapper.stripXSS(jsonString)).build(); 

     message.getExchange().put(Response.class, response); 
    } 
} 

}

이 인터셉터는 다음 나머지 API 작업을 위해 작동합니다 : - @POST @Path ("details2") @Consumes ({MediaType.APPLICATION_JSON}) @Produces ({의 MediaType .APPLICATION_JSON}) public 응답 getPersonalInfoDetails2 (String jsonString) throws DataNotFoundException;

관련 문제