从弹簧控制器访问请求者ip和时间

时间:2014-11-11 07:28:30

标签: java spring

我需要请求者ip和我的弹簧休息控制器中的请求时间。我使用的是Spring 4.0

这是我的控制器的签名:

public  @ResponseBody ReturnData downloadStart(@RequestBody DownloadStartVO startVO)
    {
.....
}

请告诉我该怎么做。感谢

EDIT :: 这些是我的依赖

    <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
 <exclusions>
       <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
       </exclusion>
       </exclusions>   
</dependency>
<!-- LogBack dependencies --> 
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>
<dependency>                                    
    <groupId>org.slf4j</groupId>                
    <artifactId>jcl-over-slf4j</artifactId>     
    <version>${jcloverslf4j.version}</version>  
</dependency>
     <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId><!--  -->
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.5.Final</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jdbc</artifactId>
  <version>7.0.35</version>
</dependency>

<dependency>

  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>sqljdbc4</artifactId>
  <version>4.0</version>
</dependency>
<!-- Apache Commons FileUpload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- Apache Commons IO -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<!-- Spring + Quartz need transaction -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Quartz framework -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.databind-version}</version>
        </dependency>
        <dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate4</artifactId>
    <version>${jackson.databind-version}</version>
</dependency>
        <!-- <dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version> 
</dependency>-->
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-ftp</artifactId>
    <version>${spring.version}</version>
</dependency>

  </dependencies>

2 个答案:

答案 0 :(得分:0)

要从用户那里获取IP地址,请使用HttpServletRequest并调用getRemoteAddr()方法。要访问HttpServletRequest,只需将其作为参数添加到方法中。

@ResponseBody
public ReturnData downloadStart(@RequestBody DownloadStartVO startVO, HttpServletRequest request) {

    String remoteAddress = request.getRemoteAddr(); // IP/Host of remote user
    long currentTime = System.currentTimeMillis(); // Current date/time in millis
}

像上面这样的东西应该可以解决问题。

有关受支持的方法参数的列表,请检查Spring Reference指南的this section

答案 1 :(得分:0)

我在我的pom中添加了以下依赖项

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

现在我可以将HttpServletRequest参数添加到我的控制器方法中并使用它来检索请求。

我认为它将成为spring-webmvc或spring-web依赖

的一部分

感谢您的帮助和指导