我有以下课程,如何获取传入请求源的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>

最佳答案

正如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()应该这样做。

10-01 08:29