当前位置: 动力学知识库 > 问答 > 编程问答 >

How to save request ip info to redis session when session is created in java spring boot

问题描述:

I refer to the document

http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html

and it works fine.

Now I want to save request ip info in session when session is created.

I try to implement an ApplicationListener below to listener SessionCreatedEvent

@Component

public class SessionCreatedEventListener implements ApplicationListener<SessionCreatedEvent> {

//@Autowired

private HttpSession session;

@Override

public void onApplicationEvent(SessionCreatedEvent event) {

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

//do something...

}

}

but get error at this line:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

error message is :

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

How should I do?

=====update my source code====

Here is my code look like:

src/main/java/url/genchi/configs/RedisSessionConfig.java

@EnableRedisHttpSession

public class RedisSessionConfig {

}

src/main/java/url/genchi/listener/SessionCreatedEventListener.java

@Component

public class SessionCreatedEventListener implements ApplicationListener<SessionCreatedEvent> {

private final int sessionTimeToLiveMins = 1;

@Override

public void onApplicationEvent(SessionCreatedEvent event) {

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

System.out.println(request.getRemoteAddr());

}

}

src/main/java/url/genchi/Application.java

@SpringBootApplication

@RestController

@ComponentScan(basePackages = {"url.genchi.listener"})

@EnableAutoConfiguration

public class Application {

@RequestMapping(value = "/", method = RequestMethod.GET)

public String index(HttpServletRequest request){

//do something

return "test"

}

@Bean

public CookieSerializer cookieSerializer() {

DefaultCookieSerializer serializer = new DefaultCookieSerializer();

serializer.setCookieName("id");

serializer.setCookieMaxAge(-1);

serializer.setUseHttpOnlyCookie(true);

serializer.setDomainName("localhost");

//serializer.setUseSecureCookie(true); //for https

return serializer;

}

public static void main(String[] args) throws Throwable {

SpringApplication.run(Application.class, args);

}

}

And i add src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

</listener>

</web-app>

And it still didn't work?

Is there something wrong?

分享给朋友:
您可能感兴趣的文章:
随机阅读: