Nginx shiro redis 多tomcat共享session

pom.xml:

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   <groupId>com.sheng.webappgroupId>  
  5.   <artifactId>redis_sessionartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>1.0-SNAPSHOTversion>  
  8.   <name>redis_session Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <properties>  
  11.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  12.     <spring.version>4.3.9.RELEASEspring.version>  
  13.   properties>  
  14.   <dependencies>  
  15.     <dependency>  
  16.       <groupId>junitgroupId>  
  17.       <artifactId>junitartifactId>  
  18.       <version>4.12version>  
  19.       <scope>testscope>  
  20.     dependency>  
  21.     <dependency>  
  22.       <groupId>org.springframework.sessiongroupId>  
  23.       <artifactId>spring-session-data-redisartifactId>  
  24.       <version>1.1.1.RELEASEversion>  
  25.       <type>pomtype>  
  26.     dependency>  
  27.       
  28.     <dependency>  
  29.       <groupId>org.springframeworkgroupId>  
  30.       <artifactId>spring-contextartifactId>  
  31.       <version>${spring.version}version>  
  32.     dependency>  
  33.     <dependency>  
  34.       <groupId>org.springframeworkgroupId>  
  35.       <artifactId>spring-context-supportartifactId>  
  36.       <version>${spring.version}version>  
  37.     dependency>  
  38.     <dependency>  
  39.       <groupId>org.springframeworkgroupId>  
  40.       <artifactId>spring-txartifactId>  
  41.       <version>${spring.version}version>  
  42.     dependency>  
  43.     <dependency>  
  44.       <groupId>org.springframeworkgroupId>  
  45.       <artifactId>spring-webartifactId>  
  46.       <version>${spring.version}version>  
  47.     dependency>  
  48.     <dependency>  
  49.       <groupId>org.springframeworkgroupId>  
  50.       <artifactId>spring-webmvcartifactId>  
  51.       <version>${spring.version}version>  
  52.     dependency>  
  53.     <dependency>  
  54.       <groupId>org.springframeworkgroupId>  
  55.       <artifactId>spring-jdbcartifactId>  
  56.       <version>${spring.version}version>  
  57.     dependency>  
  58.     <dependency>  
  59.       <groupId>org.springframeworkgroupId>  
  60.       <artifactId>spring-testartifactId>  
  61.       <version>${spring.version}version>  
  62.       <scope>testscope>  
  63.     dependency>  
  64.       
  65.     <dependency>  
  66.       <groupId>org.apache.shirogroupId>  
  67.       <artifactId>shiro-coreartifactId>  
  68.       <version>1.2.3version>  
  69.     dependency>  
  70.     <dependency>  
  71.       <groupId>org.apache.shirogroupId>  
  72.       <artifactId>shiro-webartifactId>  
  73.       <version>1.2.3version>  
  74.     dependency>  
  75.     <dependency>  
  76.       <groupId>org.apache.shirogroupId>  
  77.       <artifactId>shiro-springartifactId>  
  78.       <version>1.2.3version>  
  79.     dependency>  
  80.     <dependency>  
  81.       <groupId>org.apache.shirogroupId>  
  82.       <artifactId>shiro-ehcacheartifactId>  
  83.       <version>1.2.3version>  
  84.     dependency>  
  85.     <dependency>  
  86.       <groupId>javax.servletgroupId>  
  87.       <artifactId>jstlartifactId>  
  88.       <version>1.2version>  
  89.       <type>jartype>  
  90.       <scope>compilescope>  
  91.     dependency>  
  92.     <dependency>  
  93.       <groupId>log4jgroupId>  
  94.       <artifactId>log4jartifactId>  
  95.       <version>1.2.17version>  
  96.     dependency>  
  97.     <dependency>  
  98.       <groupId>org.slf4jgroupId>  
  99.       <artifactId>slf4j-log4j12artifactId>  
  100.       <version>1.7.25version>  
  101.     dependency>  
  102.   dependencies>  
  103.   <build>  
  104.     <finalName>redis_sessionfinalName>  
  105.   build>  
  106. project>  
applicationContext.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  6.     <context:component-scan base-package="com.sheng.example">  
  7.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  8.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  9.     context:component-scan>  
  10. beans>  
applicationContext-shiro.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.       
  7.     <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  8.           
  9.         <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />  
  10.     bean>  
  11.   
  12.       
  13.     <bean id="myRealm" class="com.sheng.example.MyRealm" init-method="setCredentialMatcher">bean>  
  14.       
  15.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  16.         <property name="sessionManager" ref="sessionManager" />  
  17.         <property name="cacheManager" ref="cacheManager" />  
  18.         <property name="realm" ref="myRealm" />  
  19.     bean>  
  20.   
  21.       
  22.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  23.   
  24.       
  25.         <property name="securityManager" ref="securityManager" />  
  26.           
  27.         <property name="loginUrl" value="/login.jsp" />  
  28.           
  29.         <property name="successUrl" value="/shiro-success.jsp" />  
  30.         <property name="unauthorizedUrl" value="/shiro-unauthorized.jsp" />  
  31.           
  32.         <property name="filterChainDefinitions">  
  33.             <value>  
  34.                   
  35.                 /css/**  = anon  
  36.                 /images/**  = anon  
  37.                 /assets/**=anon  
  38.                 /js/**=anon  
  39.                   
  40.                 /=anon  
  41.                 /index.jsp=anon  
  42.                 /login=anon  
  43.                 /user.jsp = roles[user]  
  44.                 /admin.jsp = roles[admin]  
  45.                 /** = authc  
  46.             value>  
  47.         property>  
  48.   
  49.         <property name="filters">  
  50.             <map>  
  51.                 <entry key="authc">  
  52.                     <bean class="com.sheng.example.AuthcLevelFilter">bean>  
  53.                 entry>  
  54.                   
  55.     <bean id="sessionManager"  
  56.         class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">  
  57.         <property name="globalSessionTimeout" value="60000" />  
  58.         <property name="deleteInvalidSessions" value="true" />  
  59.         <property name="sessionValidationSchedulerEnabled" value="true" />  
  60.           
  61.         <property name="sessionDAO" ref="sessionDAO" />  
  62.           
  63.           
  64.           
  65.     bean>  
  66.       
  67.       
  68.     <bean id="sessionDAO" class="com.sheng.example.RedisSessionDao">bean>  
  69.       
  70.           
  71.           
  72.           
  73.       
  74.       
  75.     <bean id="sessionIdGenerator"  
  76.         class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator" />  
  77.       
  78.     <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">  
  79.         <constructor-arg value="sid" />  
  80.         <property name="httpOnly" value="true" />  
  81.         <property name="maxAge" value="86400" />  
  82.         <property name="name" value="daskldj">property>  
  83.         <property name="domain" value="sadf">property>  
  84.     bean>  
  85.   
  86. beans>  

ehcache-shiro.xml

[html]  view plain  copy
  1.   
  2. <ehcache>  
  3.   
  4.       
  5.     <context:component-scan base-package="com.sheng.example" use-default-filters="false">  
  6.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  7.         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  8.     context:component-scan>  
  9.       
  10.       
  11.       
  12.       
  13.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
  14.           depends-on="lifecycleBeanPostProcessor"/>  
  15.     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  16.         <property name="securityManager" ref="securityManager"/>  
  17.     bean>  
  18. beans>  
web.xml

[html]  view plain  copy
  1.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  2.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  3.   
  4. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  5.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">  
  7.     
  8.   <context-param>  
  9.     <param-name>contextConfigLocationparam-name>  
  10.     <param-value>classpath:applicationContext*.xmlparam-value>  
  11.   context-param>  
  12.   <listener>  
  13.     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  14.   listener>  
  15.   <servlet>  
  16.     <servlet-name>dispatcherServletservlet-name>  
  17.     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  18.     <init-param>  
  19.       <param-name>contextConfigLocationparam-name>  
  20.       <param-value>classpath:springmvc.xmlparam-value>  
  21.     init-param>  
  22.     <load-on-startup>1load-on-startup>  
  23.     <async-supported>trueasync-supported>  
  24.   servlet>  
  25.   
  26.   <servlet-mapping>  
  27.     <servlet-name>dispatcherServletservlet-name>  
  28.     <url-pattern>/url-pattern>  
  29.   servlet-mapping>  
  30.   <welcome-file-list>  
  31.     <welcome-file>index.jspwelcome-file>  
  32.   welcome-file-list>  
  33.     
  34.   <filter>  
  35.     <filter-name>shiroFilterfilter-name>  
  36.     <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>  
  37.     <async-supported>trueasync-supported>  
  38.     <init-param>  
  39.       <param-name>targetFilterLifecycleparam-name>  
  40.       <param-value>trueparam-value>  
  41.     init-param>  
  42.   filter>  
  43.   <filter-mapping>  
  44.     <filter-name>shiroFilterfilter-name>  
  45.     <url-pattern>/*url-pattern>  
  46.   filter-mapping>  
  47. web-app>  

AuthcLevelFilter.java

[java]  view plain  copy
  1. package com.sheng.example;  
  2. import org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter;  
  3. import org.apache.shiro.web.util.WebUtils;  
  4.   
  5. import javax.servlet.ServletRequest;  
  6. import javax.servlet.ServletResponse;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import java.io.IOException;  
  10. import java.io.PrintWriter;  
  11. import java.net.URLEncoder;  
  12. import java.util.ArrayList;  
  13. import java.util.HashMap;  
  14. import java.util.Map;  
  15.   
  16. public class AuthcLevelFilter extends PassThruAuthenticationFilter {  
  17.     @SuppressWarnings("deprecation")  
  18.     @Override  
  19.         protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {  
  20.         HttpServletResponse httpResponse ;  
  21.         HttpServletRequest httpRequest;  
  22.         Map map = new HashMap();  
  23.         httpResponse = WebUtils.toHttp(response);   
  24.         httpRequest = WebUtils.toHttp(request);  
  25.   
  26.         if(isLoginRequest(httpRequest, httpResponse)){  
  27.         return true;  
  28.         }else {  
  29.   
  30.             WebUtils.saveRequest(request);  
  31.             String url = httpRequest.getRequestURI();  
  32.             map.put("requestUri", url);  
  33.             PrintWriter out=null;  
  34.             try {  
  35.                 httpResponse.setContentType("text/html; charset=utf-8");  
  36.                 out =  httpResponse.getWriter();  
  37. //              if (url.equals("/logList.jsp")||url.equals("/parkManager.jsp")) {  
  38. //                  httpResponse.setContentType("text/html;charset=utf-8");  
  39. //                  out.append("parent.location='../index.jsp';");  
  40. //              }else if(url.equals("/getParkList")||url.equals("/getLogList_admin")||url.equals("/getLogList_park")){  
  41. //                  PageData pageData=new PageData();  
  42. //                  pageData.setData(new ArrayList<>());  
  43. //                  ReturnData ret = new ReturnData();  
  44. //                  ret.setMessage(URLEncoder.encode("用户未登录,试图访问!","utf-8"));  
  45. //                  pageData.setOtherData(ret);  
  46. //                  httpResponse.setCharacterEncoding("UTF-8");  
  47. //                  httpResponse.setContentType("application/json; charset=utf-8");  
  48. //                  out.append(JSONObject.toJSONString(pageData));  
  49. //              } else if(url.equals("/park/query_coast")) {  
  50. //                  ReturnData ret = new ReturnData();  
  51. //                  ret.setStatus(false);  
  52. //                  ret.setMessage(URLEncoder.encode("用户未登录,试图访问!","utf-8"));  
  53. //                  out.append(JSONObject.toJSONString(ret));  
  54. //              }else{  
  55. //                  System.out.println("requestUri:" + httpRequest.getRequestURI());  
  56. //                  httpResponse.setStatus(200, "redirect");  
  57. //                  httpResponse.setCharacterEncoding("UTF-8");  
  58. //                  httpResponse.setContentType("application/json; charset=utf-8");  
  59. //  
  60. //                      ReturnData ret = new ReturnData();  
  61. //                      ret.setMessage("用户未登录,试图访问!");  
  62. //                      ret.setData(httpResponse.toString());  
  63.                         out.append("用户未登录,试图访问!");  
  64. //  
  65. //  
  66. //              }  
  67.             } catch (IOException e) {  
  68.                 e.printStackTrace();  
  69.             } finally {  
  70.                 if (out != null) {  
  71.                     out.close();  
  72.                 }  
  73.             }  
  74.                 return false;  
  75.   
  76.             }  
  77.         }  
  78.       
  79. }  
MyController.java

[java]  view plain  copy
  1. package com.sheng.example;  
  2.   
  3. import org.apache.shiro.SecurityUtils;  
  4. import org.apache.shiro.authc.AuthenticationException;  
  5. import org.apache.shiro.authc.UsernamePasswordToken;  
  6. import org.apache.shiro.subject.Subject;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.ResponseBody;  
  10. import org.springframework.web.servlet.ModelAndView;  
  11.   
  12. /** 
  13.  * Created by Administrator on 2017/6/30 0030. 
  14.  */  
  15. @Controller  
  16. public class MyController {  
  17.   
  18.     @RequestMapping("test")  
  19.     @ResponseBody  
  20.     public String index(){  
  21.         Subject currentUser = SecurityUtils.getSubject();  
  22.         return "Hello:"+currentUser.getPrincipal().toString()+",我是tomcat2";  
  23.     }  
  24.     @RequestMapping("login")  
  25.     public ModelAndView login(String username,String password){  
  26.         ModelAndView modelAndView=null;  
  27.         Subject subject = SecurityUtils.getSubject();  
  28.         UsernamePasswordToken token = new UsernamePasswordToken(username, password);  
  29.         try {  
  30.             subject.login(token);  
  31.             modelAndView=new ModelAndView("test");  
  32.             modelAndView.addObject("username",username);  
  33.         }catch (AuthenticationException ae){  
  34.             modelAndView=new ModelAndView("/");  
  35.             modelAndView.addObject("message","用户名或密码错误");  
  36.         }  
  37.   
  38.         return modelAndView;  
  39.     }  
  40. }  
MyRealm.java

[java]  view plain  copy
  1. package com.sheng.example;  
  2.   
  3. import org.apache.shiro.authc.AuthenticationException;  
  4. import org.apache.shiro.authc.AuthenticationInfo;  
  5. import org.apache.shiro.authc.AuthenticationToken;  
  6. import org.apache.shiro.authc.SimpleAuthenticationInfo;  
  7. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;  
  8. import org.apache.shiro.authz.AuthorizationInfo;  
  9. import org.apache.shiro.authz.SimpleAuthorizationInfo;  
  10. import org.apache.shiro.crypto.hash.Md5Hash;  
  11. import org.apache.shiro.crypto.hash.SimpleHash;  
  12. import org.apache.shiro.realm.AuthorizingRealm;  
  13. import org.apache.shiro.subject.PrincipalCollection;  
  14. import org.apache.shiro.util.ByteSource;  
  15.   
  16. /** 
  17.  * Created by Administrator on 2017/6/30 0030. 
  18.  */  
  19. public class MyRealm extends AuthorizingRealm {  
  20.     /** 
  21.      * 授权方法: 
  22.      * 1. 实际返回的是 SimpleAuthorizationInfo 类的实例 
  23.      * 2. 可以调用 SimpleAuthorizationInfo 的 addRole 来添加当前登录 user 的权限信息. 
  24.      * 3. 可以调用 PrincipalCollection 参数的 getPrimaryPrincipal() 方法来获取用户信息 
  25.      */  
  26.     @Override  
  27.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {  
  28.         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();  
  29.   
  30.         Object principal = principalCollection.getPrimaryPrincipal();  
  31. //        User user = userService.getAdmin(principal.toString());  
  32. //      if("admin".equals(principal)){  
  33. //        info.addRole(user.getRole().getName());  
  34. //          System.out.println("用户角色admin");  
  35. //          info.addRole("list");  
  36. //      }  
  37. //      if("user".equals(principal)){  
  38. //          info.addRole("list");  
  39. //      }  
  40.   
  41. //      info.addRole("user");  
  42.   
  43.         return info;  
  44.     }  
  45.   
  46.     /** 
  47.      * 认证方法 
  48.      * 1. 编写表单: 表单的 action、和 username、password 的参数都是什么 ? 
  49.      * 回答: 提交到你想提交的地方, username 和 password 也参数名称都任意. 
  50.      * 2. 例如, 提交到了一个 SpringMVC 的 handler: 
  51.      * 1). 获取用户名、密码 
  52.      * 2). 
  53.      * Subject currentUser = SecurityUtils.getSubject(); 
  54.      * UsernamePasswordToken token = new UsernamePasswordToken(username, password); 
  55.      * currentUser.login(token); 
  56.      * 3. 当 Subject 调用 login 方法时, 即会触发当前的 doGetAuthenticationInfo 方法. 且把 
  57.      * UsernamePasswordToken 对象传入, 然后再该方法中执行真正的认证: 访问数据库进行比对. 
  58.      * 1). 获取用户名和密码 
  59.      * 2). 
  60.      */  
  61.     @Override  
  62.     protected AuthenticationInfo doGetAuthenticationInfo(  
  63.             AuthenticationToken token) throws AuthenticationException {  
  64.         System.out.println("doGetAuthenticationInfo------->"+token.getPrincipal());  
  65.         System.out.println("doGetAuthenticationInfo------->"+token.getCredentials());  
  66.   
  67.         //1. 从 token 中获取登录的 username! 注意不需要获取 password.  
  68.   
  69.         //2. 利用 username 查询数据库得到用户的信息.  
  70.   
  71.         //3. 创建 SimpleAuthenticationInfo 对象并返回. 注意该对象的凭证式从数据库中查询得到的.  
  72.         //而不是页面输入的. 实际的密码校验可以交由 Shiro 来完成  
  73.   
  74.         //4. 关于密码加密的事: shiro 的密码加密可以非常非常的复杂, 但实现起来却可以非常简单.  
  75.         //1). 可以选择加密方式: 在当前的 realm 中编写一个 public 类型的不带参数的方法, 使用 @PostConstruct  
  76.         //注解进行修饰, 在其中来设置密码的匹配方式.  
  77.         //2). 设置盐值: 盐值一般是从数据库中查询得到的.  
  78.         //3). 调用 new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName)  
  79.         //构造器返回 SimpleAuthenticationInfo 对象:  credentialsSalt 为  
  80.         //ByteSource credentialsSalt = new Md5Hash(source);  
  81.   
  82.         //登陆的主要信息: 可以是一个实体类的对象, 但该实体类的对象一定是根据 token 的 username 查询得到的.  
  83.         Object principal = token.getPrincipal();//获取登录名  
  84.         //认证信息: 从数据库中查询出来的信息. 密码的比对交给 shiro 去进行比较  
  85.         String credentials = "789ecdde95405b37ffafd9c4e460b4a9";  
  86.         //设置盐值:  
  87.         String source = "abcdefg";  
  88.         ByteSource credentialsSalt = new Md5Hash(source);  
  89. //      System.out.println(credentialsSalt);  
  90. //        User atx_admin=null;  
  91. //        try {  
  92. //            atx_admin = userService.getAdmin(token.getPrincipal().toString());  
  93. //            if(atx_admin.getRole().getName().equals("park_station")){//数据库不加密前台人员密码  
  94. //                atx_admin.setPassword(strToMD5(atx_admin.getPassword()));  
  95. //            }  
  96.             System.out.println("数据库密码:" + atx_admin.getPassword());  
  97. //        }catch (Exception e){  
  98. //            e.printStackTrace();  
  99. //        }  
  100. //        if(atx_admin==null)  
  101. //            throw new AuthenticationException("用户名不存在");  
  102.         //当前 Realm 的 name  
  103.         String realmName = getName();  
  104. //      System.out.println("realmName:"+realmName);  
  105.         SimpleAuthenticationInfo info =  
  106.                 new SimpleAuthenticationInfo(principal, credentials,  
  107.                         credentialsSalt, realmName);  
  108.   
  109.         return info;  
  110.     }  
  111.   
  112.     //@PostConstruct: 相当于 bean 节点的 init-method 配置.  
  113.     public void setCredentialMatcher(){  
  114.         HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();  
  115.   
  116.         credentialsMatcher.setHashAlgorithmName("MD5");  
  117.         credentialsMatcher.setHashIterations(1024);  
  118.   
  119.         setCredentialsMatcher(credentialsMatcher);  
  120.     }  
  121.     public static String strToMD5(String str){  
  122.         String saltSource = "abcdefg";  
  123.   
  124.         String hashAlgorithmName = "MD5";//加密方式  
  125.         Object salt = new Md5Hash(saltSource);//盐值  
  126.         int hashIterations = 1024;//加密次数  
  127.   
  128.         //加密后的密码  
  129.         Object result = new SimpleHash(hashAlgorithmName, str, salt, hashIterations);  
  130.         return result.toString();  
  131.     }  
  132.     public static void main(String[] args) {  
  133.         String saltSource = "abcdefg";  
  134.   
  135.         String hashAlgorithmName = "MD5";//加密方式  
  136.         String credentials = "123";//密码  
  137.         Object salt = new Md5Hash(saltSource);//盐值  
  138.         int hashIterations = 1024;//加密次数  
  139.   
  140.         //加密后的密码  
  141.         Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);  
  142.         System.out.println(result);  
  143.         System.out.println("68f3139a38b232392cc9d3b6ddd762f7".equals(result.toString()));  
  144.     }  
  145. }  
RedisClient.java

[java]  view plain  copy
  1. package com.sheng.example;  
  2.   
  3. import org.apache.shiro.codec.Base64;  
  4. import org.apache.shiro.session.Session;  
  5. import redis.clients.jedis.Jedis;  
  6. import redis.clients.jedis.JedisPool;  
  7. import redis.clients.jedis.JedisPoolConfig;  
  8.   
  9. import java.io.*;  
  10.   
  11. /** 
  12.  * Created by Administrator on 2017/6/30 0030. 
  13.  */  
  14. public class RedisClient {  
  15.     private static JedisPool pool;  
  16.     private static String redisServerIp="192.168.31.7";  
  17.   
  18.     /** 
  19.      * 建立连接池 真实环境,一般把配置参数缺抽取出来。 
  20.      * 
  21.      */  
  22.     private static void createJedisPool() {  
  23.   
  24.         // 建立连接池配置参数  
  25.         JedisPoolConfig config = new JedisPoolConfig();  
  26.         config.setMaxWaitMillis(1000);  
  27.         // 设置最大连接数  
  28. //        config.setMaxActive(1000);  
  29.   
  30.         // 设置最大阻塞时间,记住是毫秒数milliseconds  
  31. //        config.setMaxWait(1000);  
  32.         // 设置空间连接  
  33.         config.setMaxIdle(10);  
  34.         config.setMaxTotal(100);  
  35.         config.setMinIdle(2);  
  36.         // 创建连接池  
  37.         pool = new JedisPool(config, redisServerIp, 6379);  
  38.   
  39.     }  
  40.   
  41.     /** 
  42.      * 在多线程环境同步初始化 
  43.      */  
  44.     private static synchronized void poolInit() {  
  45.         if (pool == null)  
  46.             createJedisPool();  
  47.     }  
  48.   
  49.     /** 
  50.      * 获取一个jedis 对象 
  51.      * 
  52.      * @return 
  53.      */  
  54.     private static Jedis getJedis() {  
  55.   
  56.         if (pool == null)  
  57.             poolInit();  
  58.         return pool.getResource();  
  59.     }  
  60.   
  61.     /** 
  62.      * 归还一个连接 
  63.      * 
  64.      * @param jedis 
  65.      */  
  66.     private static void returnRes(Jedis jedis) {  
  67.         pool.returnResource(jedis);  
  68.     }  
  69.   
  70.     void set(String sessionId, Session session) {  
  71.         jedis = getJedis();  
  72.         jedis.append(sessionId, serialize(session));  
  73.         returnRes(jedis);  
  74.     }  
  75.   
  76.     void replace(String sessionId, Session session) {  
  77.         set(sessionId, session);  
  78.   
  79.     }  
  80.   
  81.     Jedis jedis = null;  
  82.   
  83.     void delete(String sessionId) {  
  84.         jedis = getJedis();  
  85.         jedis.del(sessionId);  
  86.         returnRes(jedis);  
  87.     }  
  88.   
  89.     Object get(String sessionId) {  
  90.         jedis = getJedis();  
  91.         Object obj = deserialize(jedis.get(sessionId));  
  92.         returnRes(jedis);  
  93.         return obj;  
  94.     }  
  95.   
  96.     private static Object deserialize(String str) {  
  97.         ByteArrayInputStream bis = null;  
  98.         ObjectInputStream ois = null;  
  99.         try {  
  100.             bis = new ByteArrayInputStream(Base64.decode(str));  
  101.             ois = new ObjectInputStream(bis);  
  102.             return ois.readObject();  
  103.         } catch (Exception e) {  
  104.             throw new RuntimeException("deserialize session error", e);  
  105.         } finally {  
  106.             try {  
  107.                 ois.close();  
  108.                 bis.close();  
  109.             } catch (IOException e) {  
  110.                 e.printStackTrace();  
  111.             }  
  112.   
  113.         }  
  114.     }  
  115.   
  116.     private static String serialize(Object obj) {  
  117.         ByteArrayOutputStream bos = null;  
  118.         ObjectOutputStream oos = null;  
  119.         try {  
  120.             bos = new ByteArrayOutputStream();  
  121.             oos = new ObjectOutputStream(bos);  
  122.             oos.writeObject(obj);  
  123.             return Base64.encodeToString(bos.toByteArray());  
  124.         } catch (Exception e) {  
  125.             throw new RuntimeException("serialize session error", e);  
  126.         } finally {  
  127.             try {  
  128.                 oos.close();  
  129.                 bos.close();  
  130.             } catch (IOException e) {  
  131.                 e.printStackTrace();  
  132.             }  
  133.   
  134.         }  
  135.     }  
  136.     public static void main(String[] args){  
  137.         Jedis jedisCli = new Jedis("192.168.31.7"6379); //新建Jedis对象  
  138.         jedisCli.select(2); //切换Redis数据库  
  139.         jedisCli.set("firstJedis""hello,Jedis"); //与Redis命令行操作基本一致  
  140.     }  
  141. }  

RedisSessionDao.java

[java]  view plain  copy
  1. package com.sheng.example;  
  2.   
  3. import org.apache.shiro.session.Session;  
  4. import org.apache.shiro.session.UnknownSessionException;  
  5. import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;  
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8.   
  9. import java.io.Serializable;  
  10. import java.util.Collection;  
  11. import java.util.Collections;  
  12.   
  13. /** 
  14.  * Created by Administrator on 2017/6/30 0030. 
  15.  */  
  16. public class RedisSessionDao extends AbstractSessionDAO {  
  17.     private RedisClient sessionCacheClient=new RedisClient();  
  18.   
  19.     Logger log= LoggerFactory.getLogger(getClass());  
  20.   
  21.     public void update(Session session) throws UnknownSessionException {  
  22.         log.info("更新seesion,id=[{}]",session.getId().toString());  
  23.         try {  
  24.             sessionCacheClient.replace(session.getId().toString(), session);  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.   
  30.     public void delete(Session session) {  
  31.         log.info("删除seesion,id=[{}]",session.getId().toString());  
  32.         try {  
  33.             sessionCacheClient.delete(session.getId().toString());  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.   
  38.     }  
  39.   
  40.     public Collection getActiveSessions() {  
  41.         System.out.println("getActiveSessions");  
  42.         log.info("获取存活的session");  
  43.         return Collections.emptySet();  
  44.     }  
  45.   
  46.     @Override  
  47.     protected Serializable doCreate(Session session) {  
  48.         Serializable sessionId = generateSessionId(session);  
  49.         assignSessionId(session, sessionId);  
  50.         log.info("创建seesion,id=[{}]",session.getId().toString());  
  51.         try {  
  52.             sessionCacheClient.set(sessionId.toString(),  session);  
  53.         } catch (Exception e) {  
  54.             log.error(e.getMessage());  
  55.         }  
  56.         return sessionId;  
  57.     }  
  58.   
  59.     @Override  
  60.     protected Session doReadSession(Serializable sessionId) {  
  61.         log.info("获取seesion,id=[{}]",sessionId.toString());  
  62.         Session session = null;  
  63.         try {  
  64.             session = (Session) sessionCacheClient.get(sessionId.toString());  
  65.         } catch (Exception e) {  
  66.             log.error(e.getMessage());  
  67.         }  
  68.         return session;  
  69.     }  
  70. }  

index.jsp

[html]  view plain  copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. >  
  4. <html>  
  5. <body>  
  6. <h2>Hello World!h2>  
  7. <form action="login">  
  8.     <input type="text" name="username">  
  9.     <input type="password" name="password">  
  10.     <input type="submit" value="提交">  
  11. form>  
  12. <%=session.getId()%>  
  13. <script>  
  14.     <c:if test="${not empty message}">  
  15.         alert("${message}");  
  16.     c:if>  
  17. script>  
  18. body>  
  19. html>  


打包后放在不同tomcat下 即可实现session共享

你可能感兴趣的:(Nginx shiro redis 多tomcat共享session)