Web services 如何将拦截器(模式验证)应用于服务器上部署的多个服务中的特定端点
我在一台服务器中部署了两个soap端点(soap服务)。当我重写以下拦截器时,它将应用于两个服务。如何启用/禁用特定于一个服务的侦听器。好心帮忙 拦截器代码如下所示Web services 如何将拦截器(模式验证)应用于服务器上部署的多个服务中的特定端点,web-services,soap,interceptor,Web Services,Soap,Interceptor,我在一台服务器中部署了两个soap端点(soap服务)。当我重写以下拦截器时,它将应用于两个服务。如何启用/禁用特定于一个服务的侦听器。好心帮忙 拦截器代码如下所示 @Override public void addInterceptors(List<EndpointInterceptor> interceptors) { PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingIn
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(true);
validatingInterceptor.setValidateResponse(true);
validatingInterceptor.setXsdSchemaCollection(LogAnalyzerFile());
interceptors.add(validatingInterceptor);
}
@覆盖
公共void附加侦听器(列表侦听器){
PayloadValidatingInterceptor validatingInterceptor=新的PayloadValidatingInterceptor();
validatingInterceptor.setValidateRequest(true);
validatingInterceptor.setValidateResponse(true);
验证Interceptor.setXsdSchemaCollection(LogAnalyzerFile());
添加(验证Interceptor);
}
注意:这是一个spring引导项目,使用注释。
Ipackage com.config;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.boot.web.servlet.ServletRegistrationBean;
导入org.springframework.context.ApplicationContext;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.core.io.Resource;
导入org.springframework.util.StringUtils;
导入org.springframework.ws.webservicesessage;
导入org.springframework.ws.config.annotation.EnableWs;
导入org.springframework.ws.config.annotation.WsConfigurationSupport;
导入org.springframework.ws.context.MessageContext;
导入org.springframework.ws.server.SmartEndpointInterceptor;
导入org.springframework.ws.server.endpoint.MethodEndpoint;
导入org.springframework.ws.soap.SoapBody;
导入org.springframework.ws.soap.SoapHeader;
导入org.springframework.ws.soap.SoapMessage;
导入org.springframework.ws.transport.http.MessageDispatcherServlet;
导入org.springframework.ws.wsdl.wsdl11.simplewsdl11定义;
导入org.springframework.ws.wsdl.wsdl11.wsdl11定义;
导入org.springframework.xml.xsd.simplexsdsdschema;
导入org.springframework.xml.xsd.XsdSchema;
导入org.xml.sax.SAXException;
导入javax.xml.xmlstants;
导入javax.xml.transform.Source;
导入javax.xml.transform.dom.DOMResult;
导入javax.xml.validation.Schema;
导入javax.xml.validation.SchemaFactory;
导入javax.xml.validation.Validator;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Locale;
@使能
@配置
公共类TestConfig扩展WsConfigurationSupport实现SmartEndpointInterceptor{
私有最终记录器Logger=LoggerFactory.getLogger(this.getClass());
@Bean(name=“testlog”)
公共ServletRegistrationBean测试日志(ApplicationContext ApplicationContext){
MessageDispatcherServlet servlet=new MessageDispatcherServlet();
setApplicationContext(applicationContext);
setTransformWsdlLocations(true);
ServletRegistrationBean ServletRegistrationBean=新的ServletRegistrationBean(servlet,“/File/*”);
servletRegistrationBean.setName(“日志”);
返回servletRegistrationBean;
}
@Bean(name=“testFile”)
公共Wsdl11Definition测试文件()
{
SimpleWsdl11Definition wsdl11Definition=新的SimpleWsdl11Definition();
setWsdl(新类路径资源(“test.wsdl”);
logger.info(“test.wsdl:”);
返回wsdl11Definition;
}
@Bean(name=“UploadLogFile”)
公共XsdSchema上载日志文件(){
返回新的simplexsdsdschema(新的类路径资源(“1.xsd”);
}
@Bean(name=“ErrorInfo”)
公共XsdSchema ErrorInfo(){
返回新的simplexsdsdschema(新的类路径资源(“2.xsd”);
}
公共资源[]getSchemas(){
List schemaResources=new ArrayList();
添加(新类路径资源(“1.xsd”);
添加(新类路径资源(“2.xsd”);
返回schemaResources.toArray(新资源[schemaResources.size()]);
}
@凌驾
公共布尔值shouldIntercept(MessageContext MessageContext,对象端点){
if(方法端点的端点实例){
MethodEndpoint MethodEndpoint=(MethodEndpoint)端点;
返回methodEndpoint.getMethod().getDeclaringClass()==YourEndpoint.class;
}
返回false;
}
私有布尔validateSchema(Source,MessageContext,MessageContext)引发异常{
布尔errorFlag=true;
SchemaFactory\u SchemaFactory=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI);
Schema _Schema=_schemaFactory.newSchema(getSchemas()[0].getFile());
验证器_Validator=_schema.newValidator();
DOMResult _result=新的DOMResult();
试一试{
_validator.validate(源、结果);
}捕获(SAXException\u异常){
errorFlag=false;
SoapMessage响应=(SoapMessage)messageContext.getResponse();
String faultString=StringUtils.hasLength(_exception.getMessage())?_exception.getMessage():_exception.toString();
SoapBody=response.getSoapBody();
body.addServerOrReceiverFault(faultString,Locale.US);
_异常。printStackTrace();
}
返回错误标志;
}
@凌驾
公共布尔HandlerRequest(MessageContext,对象端点)引发异常{
WebServiceMessageWebServiceMessageRequest=messageContext.getRequest();
SoapMessage SoapMessage=(SoapMessage)webServiceMessageRequest;
SoapHeader SoapHeader=soapMessage.getSoapHeader();
Source bodySource=soapMessage.getSoapBody().getPayloadSource();
返回validateSchema(bodySource,messageContext);
}
@凌驾
公共布尔HandlerResponse(MessageContext MessageContext,对象端点)引发异常{
返回true;
}
@凌驾
公共布尔handleFault(MessageContext MessageContext,对象端点)thr
package com.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurationSupport;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.SmartEndpointInterceptor;
import org.springframework.ws.server.endpoint.MethodEndpoint;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition;
import org.springframework.ws.wsdl.wsdl11.Wsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@EnableWs
@Configuration
public class TestConfig extends WsConfigurationSupport implements SmartEndpointInterceptor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Bean(name="testlog")
public ServletRegistrationBean testlog(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "/File/*");
servletRegistrationBean.setName("Log");
return servletRegistrationBean;
}
@Bean(name = "testFile")
public Wsdl11Definition testFile()
{
SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
wsdl11Definition.setWsdl(new ClassPathResource("test.wsdl"));
logger.info("test.wsdl:");
return wsdl11Definition;
}
@Bean(name = "UploadLogFile")
public XsdSchema UploadLogFile() {
return new SimpleXsdSchema(new ClassPathResource("1.xsd"));
}
@Bean(name = "ErrorInfo")
public XsdSchema ErrorInfo() {
return new SimpleXsdSchema(new ClassPathResource("2.xsd"));
}
public Resource[] getSchemas() {
List<Resource> schemaResources = new ArrayList<>();
schemaResources.add(new ClassPathResource("1.xsd"));
schemaResources.add(new ClassPathResource("2.xsd"));
return schemaResources.toArray(new Resource[schemaResources.size()]);
}
@Override
public boolean shouldIntercept(MessageContext messageContext, Object endpoint) {
if (endpoint instanceof MethodEndpoint) {
MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
return methodEndpoint.getMethod().getDeclaringClass() == YourEndpoint.class;
}
return false;
}
private Boolean validateSchema(Source source_, MessageContext messageContext) throws Exception {
boolean errorFlag = true;
SchemaFactory _schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema _schema = _schemaFactory.newSchema(getSchemas()[0].getFile());
Validator _validator = _schema.newValidator();
DOMResult _result = new DOMResult();
try {
_validator.validate(source_, _result);
} catch (SAXException _exception) {
errorFlag = false;
SoapMessage response = (SoapMessage) messageContext.getResponse();
String faultString = StringUtils.hasLength(_exception.getMessage()) ? _exception.getMessage() : _exception.toString();
SoapBody body = response.getSoapBody();
body.addServerOrReceiverFault(faultString, Locale.US);
_exception.printStackTrace();
}
return errorFlag;
}
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
WebServiceMessage webServiceMessageRequest = messageContext.getRequest();
SoapMessage soapMessage = (SoapMessage) webServiceMessageRequest;
SoapHeader soapHeader = soapMessage.getSoapHeader();
Source bodySource = soapMessage.getSoapBody().getPayloadSource();
return validateSchema(bodySource,messageContext);
}
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
return false;
}
@Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
}
}