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>
도움을 주셔서 감사합니다 XSSInterceptor는 정상적으로 작동하지만 CustomRequestFilter를 사용하여 양식 매개 변수를 업데이트 할 수 없습니다. 나는 그것을 시도했지만 실패했다. – Sandeep
안녕하세요 @pedrofb, 휴식 서비스의 MediaType을 APPLICATION_FORM_URLENCODED에서 Application_json으로 변경하면 저를 도와주세요. 제 경우에는 UI가 Application_json 요청 만 보내고이 경우에는 실패합니다. – Sandeep
'application/json'을 사용하면 매개 변수가 아닌 들어오는 스트림을 처리해야합니다. 'message.getContent (InputStream.클래스)', 문자열 데이터를 소비하고 처리하고'message.getExchange(). setContent (analyzStream)'로 새로운 내용을 설정한다. – pedrofb