企业型网站制作,wordpress不显示首页登录,域名有没有被注册哪个网站最好,wordpress游戏插件下载Redis实现Session共享这几天在做session共享这么一个小模块#xff0c;也查了好多资料#xff0c;给我的感觉#xff0c;就是太乱了#xff0c;一直找不到我想要的东西#xff0c;几乎全部实现方法都与我的想法不一样#xff0c;在这里#xff0c;我总结一下自己是如何用…Redis实现Session共享这几天在做session共享这么一个小模块也查了好多资料给我的感觉就是太乱了一直找不到我想要的东西几乎全部实现方法都与我的想法不一样在这里我总结一下自己是如何用Redis实现session共享的方便自己以后查询也希望能给有这方面需求的朋友一些帮助。相关专题推荐php session (包含图文、视频、案例)先说一下我的开发环境nginx、redis、tomcat用moven构建项目jetty服务器运行所以在这里下面也会涉及一下如何用maven打war包部署在tomcat上运行。redis是一个key-value数据库存值取值全靠这个key了这里啰嗦一句因为原创专业的介绍我就不粘贴了想了解的官方介绍的可以自行search.pom.xml中配置redis.clientsjedis2.8.1org.springframework.dataspring-data-redis1.7.2.RELEASEaplicationContext-redis.xml中配置配置完毕后开始代码实现在LoginController里第一步引入RedisTemplateAutowiredQualifier(writeRedisTemplate)private StringRedisTemplate writeTemplate;这里只需要引入writeRedisTemplate即可在登陆的时候只负责写只有在再次刷新的时候经过过滤器才需要读第二步正常登陆流程登陆成功之后request还要保存session信息第三步设置cookie值把作为保存userSession信息在redis中的key值存入cookie刷新浏览器的时候过滤器可以从cookie中取到key值进而去redis取对应的value值即userSessionString domain request.getServerName();String cookieIdMD5Util.MD5Encode(uasLoginer, UTF-8);//生成token用作session在redis存储中的key值StringredisSessionKey UUID.randomUUID().toString();Cookie uasLoginer new Cookie(cookieId, redisSessionKey);if (domain.startsWith(uas.)) {uasLoginer.setDomain(domain.substring(4,domain.length()));}else {uasLoginer.setDomain(domain);}uasLoginer.setMaxAge(60000);uasLoginer.setPath(/);response.addCookie(uasLoginer);这里cookie跨域setDomain和setPath设置第四步把userSession信息存入redis中RedisTemplate中写入redis的值要为String类型需要把userSession对象转成Json字符串userSessionString JSON.toJSONString(userSession);在转Json的时候遇到问题导入import com.alibaba.fastjson.JSON;一直失败发现pom中没有依赖Json的关系如果有遇到相同的问题可以检查下在pom.xml中是否有关于json的依赖关系没的话在pom.xml中导入json的依赖关系如下net.sf.json-libjson-lib2.3jdk15写入redis的代码如下writeTemplate.opsForHash().put(UasContants.REDIS_USER_SESSION_KEY_redisSessionKey,redisSessionKey, userSessionString);writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY_redisSessionKey, 1800L, TimeUnit.SECONDS);//设置redis中值的有效期完成这一操作用户的session信息已经存入到redis中可在redis中查看是否存入。第五步进入页面后刷新页面请求会经过过滤器在Filter.Java中读取redis的值并进行一些处理在过滤器这里就无法通过注解的方式引入redisTemplate可以通过如下的方式引入BeanFactory beans WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());StringRedisTemplate readTemplate (StringRedisTemplate) beans.getBean(readRedisTemplate);StringRedisTemplate writeTemplate (StringRedisTemplate) beans.getBean(writeRedisTemplate);过滤器从cookie中取出redis的key值用readTemplate读出value值String cookidMD5Util.MD5Encode(uasLoginer, UTF-8);Cookie[] cookies req.getCookies();String redisSessionKey ;if(cookies ! null){for (Cookie cookie : cookies) {if(cookie.getName().equals(cookid)){redisSessionKey cookie.getValue() ;}}}UserSession userSession null;String userSessionString (String) readTemplate.boundHashOps(UasContants.REDIS_USER_SESSION_KEY_redisSessionKey).get(redisSessionKey);if(null ! userSessionString ){SuppressWarnings(static-access)JSONObject obj new JSONObject().fromObject(userSessionString);//将json字符串转换为json对象userSession (UserSession)JSONObject.toBean(obj,UserSession.class);writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY_redisSessionKey, 1800L, TimeUnit.SECONDS);request.getSession().setAttribute(UasContants.USER_SESSION, userSession);}if (userSession ! null) {chain.doFilter(req, res);return;}else {res.sendRedirect(UasContants.LOGIN_URL);return;}在这里另外附上关于web.xml关于LoginFilter的配置有需要的可以参考下org.springframework.web.context.ContextLoaderListenerloginFiltercom.sfbest.uas.filter.LoginFilterexcludePaths/login,/user/login,/user/authloginFilter/*按照上面的配置就可以用redis实现session共享的功能但我在开发的时候遇到一个蛋疼的问题在测试环境上把项目部署在两台tomcat服务器上的时候cookie里一直存不进去redis的key值单台可以存进去经过长期的检测终于发现是nginx配置出的问题引以为戒深深的阴影。下面我贴出我正常运行时nginx的配置代码upstream uassessiontest.d.com {server 10.103.16.226:8088;server 10.103.16.226:8089;}server {log_format sf_uastest $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_cookie;listen 80;server_name uassessiontest.d.com;access_log /var/log/nginx/uassessiontest.log sf_uastest;location / {rewrite ^/$ /uas/ break;proxy_pass http://uassessiontest.d.com;}}红色的为当初少配的部分这些部分是的作用是往浏览器端写入cookie值。