1.引入jar包
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>x.x.x</version>
</dependency>
2.配置web.xml
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.配置spring文件
<!-- Spring session 的配置类 -->
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
<!-- cookie相关配置 -->
<bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="domainName" value="localhost" />
<property name="useHttpOnlyCookie" value="true" />
<property name="cookiePath" value="/" />
<property name="cookieMaxAge" value="1800" />
</bean>
<!-- 配置redis连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
</bean>
<!-- 配置jedis连接工厂,用于连接redis -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
3.编写方法
@RequestMapping(value = "login",method = RequestMethod.GET)
@ResponseBody
public String login(HttpSession session) {
JSONObject object = new JSONObject();
object.put("name","'zhangsan'");
object.put("pwd",123);
session.setAttribute("user",object);
return "Tomcat1登录成功";
}
@RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
@ResponseBody
public String getUserInfo(HttpSession session) {
Object user = session.getAttribute("user");
if (user==null){
return "请重新登录!";
}
JSONObject object = JSONObject.parseObject(user.toString());
object.put("server","Tomcat1");
return object.toJSONString();
}
@RequestMapping(value = "logout",method = RequestMethod.GET)
@ResponseBody
public String logout(HttpSession session) {
session.removeAttribute("user");
return "Tomcat1注销成功";
}
4.登录
5.查看redis
6.获取用户信息
7.注销删除session
8.注销状态获取用户信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137153.html