1、业务代码,login.do写入,loginout.do删除,get_user_info.do读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 package com.mmall.controller.portal;import com.mmall.common.Const;import com.mmall.common.ServerResponse;import com.mmall.pojo.User;import com.mmall.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@Controller @RequestMapping ("/user/springsession/" )public class UserSpringSessionController { @Autowired private IUserService iUserService; @RequestMapping (value = "login.do" ,method = RequestMethod.GET) @ResponseBody public ServerResponse<User> login (String username, String password, HttpSession session, HttpServletResponse httpServletResponse) { ServerResponse<User> response = iUserService.login(username,password); if (response.isSuccess()){ session.setAttribute(Const.CURRENT_USER,response.getData()); JsonUtil.obj2String(response.getData()),Const.RedisCacheExtime.REDIS_SESSION_EXTIME); } return response; } @RequestMapping (value = "logout.do" ,method = RequestMethod.GET) @ResponseBody public ServerResponse<String> logout (HttpSession session,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) { session.removeAttribute(Const.CURRENT_USER); return ServerResponse.createBySuccess(); } @RequestMapping (value = "get_user_info.do" ,method = RequestMethod.GET) @ResponseBody public ServerResponse<User> getUserInfo (HttpSession session, HttpServletRequest httpServletRequest) { User user = (User)session.getAttribute(Const.CURRENT_USER); if (user != null ){ return ServerResponse.createBySuccess(user); } return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息" ); } }
2、pom.xml里面引入jar包依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency > <groupId > org.springframework.session</groupId > <artifactId > spring-session-data-redis</artifactId > <version > 1.2.0.RELEASE</version > </dependency > <dependency > <groupId > org.redisson</groupId > <artifactId > redisson</artifactId > <version > 2.9.0</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.dataformat</groupId > <artifactId > jackson-dataformat-avro</artifactId > <version > 2.9.0</version > </dependency >
3、spring配置文件applicationContext.xml里面注入bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <bean id ="redisHttpSessionConfiguration" class ="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" > <property name ="maxInactiveIntervalInSeconds" value ="1800" /> </bean > <bean id ="defaultCookieSerializer" class ="org.springframework.session.web.http.DefaultCookieSerializer" > <property name ="domainName" value ="bytenote.cn" /> <property name ="useHttpOnlyCookie" value ="true" /> <property name ="cookiePath" value ="/" /> <property name ="cookieMaxAge" value ="31536000" /> </bean > <bean id ="jedisPoolConfig" class ="redis.clients.jedis.JedisPoolConfig" > <property name ="maxTotal" value ="20" /> </bean > <bean id ="jedisConnectionFactory" class ="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name ="hostName" value ="127.0.0.1" /> <property name ="port" value ="6380" /> <property name ="poolConfig" ref ="jedisPoolConfig" /> </bean >
4、web.xml里面引入springSession过滤器
1 2 3 4 5 6 7 8 9 <filter > <filter-name > sessionExpireFilter</filter-name > <filter-class > com.mmall.controller.common.SessionExpireFilter</filter-class > </filter > <filter-mapping > <filter-name > sessionExpireFilter</filter-name > <url-pattern > *.do</url-pattern > </filter-mapping >
欢迎访问 chenyawei 的博客, 若有问题或者有好的建议欢迎留言,笔者看到之后会及时回复。 评论点赞需要github账号登录,如果没有账号的话请点击 github 注册, 谢谢 !
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !