如何获取传入请求SOAP的IP地址

时间:2016-07-29 07:29:01

标签: java spring soap

我有以下类,如何获取传入请求源的IP地址?我已经在互联网上查了几个解决方案,但找不到合适的解决方案,如果你需要更多关于我可以添加的项目结构的信息,谢谢

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "doSomething")
    public DefaultWsdl11Definition defaultValidateWsdl11Definition(XsdSchema validateSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("ValidatePort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.org.com/validate");
        wsdl11Definition.setSchema(validateSchema);
        return wsdl11Definition;
    }

    @Bean(name = "doSecond")
    public DefaultWsdl11Definition defaultActionWsdl11Definition(XsdSchema actionSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("ActionPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.org.com/action");
        wsdl11Definition.setSchema(actionSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema validateSchema() {
        return new SimpleXsdSchema(new ClassPathResource("doSomething.xsd"));
    }

    @Bean
    public XsdSchema actionSchema() {
        return new SimpleXsdSchema(new ClassPathResource("doSecond.xsd"));
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:tns="http://www.org.com/doSomething" 
    targetNamespace="http://www.org.com/doSomething" elementFormDefault="qualified">

    <xs:element name="getActionRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Username" type="xs:string"/>
                <xs:element name="Password" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getDoSomethingResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Code" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

1 个答案:

答案 0 :(得分:2)

正如Tibrogargan所说,它应该在标题中。您可以像这样注入Request

@Autowired
private HttpServletRequest request;

并获取这样的IP地址

protected String getIpAddress() {
    String ipAddress = request.getHeader("X-FORWARDED-FOR");
    if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
    }
    return ipAddress;
}

如果没有代理模糊,您可能有X-FORWARDED-FOR标头获取原始发件人的IP地址。否则getRemoteAddr()应该这样做。