spring boot session

spring boot中如何使用session?

1、配置文件中引入相关jar包

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

2、添加配置class

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SessionConfig {
@Value("${redis.hostname:localhost}")
String HostName;
@Value("${redis.port:6379}")
int Port;
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setPort(Port);
connection.setHostName(HostName);
return connection;
}
}

3、使用session

public Map<String, Object> Logout(HttpServletRequest request) throws Exception {
Map<String, Object> resultMap = new HashMap<>();
try {
HttpSession session = request.getSession();
session.removeAttribute("userName");
session.removeAttribute("id");
} catch (Exception e) {
}
return resultMap;
}
posted @ 2018-08-20 16:09  乡村骑士2  阅读(219)  评论(0)    收藏  举报