分享

spring security 3.1.0 控制用户重复登陆

 飞鹰飞龙飞天 2015-06-08

通过配置我们可以实现两个需求 1、限制不允许第二个用户登录,2、第二个登陆用户踢掉前一个登陆用户 

假设你的spring架构已经可以使用了(其他的主要功能完成),需要增加登录限制功能。

注:这里只写配置不写原理(不懂的就问度娘),其实个人认为先配置好跑起来再研究下原理最好了

  1. 第一步、使用注解加上
    1. @Autowired  
    2. protected SessionRegistry sessionRegistry;  
    通过sessionRegistry可以获取系统当前在线人数和登录用户信息
  2.  applicationContext-security.xml里你需要增加和修改的地方
    1. <session-management invalid-session-url="/timeout.jsp" session-authentication-error-url="/s.jsp" session-authentication-strategy-ref="sas" /> ***  
    2. <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />   
    3.   
    4. <b:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">  
    5.    <b:constructor-arg name="sessionRegistry" ref="sessionRegistry" /> **  
    6.    <b:property name="maximumSessions" value="1" />  
    7. </b:bean>  
    8. <!-- SESSION管理 -->  
    9. <b:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" /> ***  
    10. <b:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">  
    11.      <b:property name="sessionRegistry" ref="sessionRegistry" /> **  
    12.      <b:property name="expiredUrl" value="/timeout.jsp" /><!-- 过期的Url -->  
    13. </b:bean>  
    14. <!-- 登录验证器 -->  
    15. <b:bean id="loginFilter" class="com.oms.core.security.MyUsernamePasswordAuthenticationFilter">  
    16.       <!-- 处理登录的action -->  
    17.    <b:property name="filterProcessesUrl" value="/j_spring_security_check"/>  
    18.       <!-- 验证成功后的处理-->  
    19.    <b:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"/>  
    20.       <!-- 验证失败后的处理-->  
    21.    <b:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"/>  
    22.    <b:property name="authenticationManager" ref="authenticationManager"/>  
    23.        <!-- 注入DAO为了查询相应的用户 -->  
    24.    <b:property name="agentsUserTblDao" ref="agentsUserTblDao"/>  
    25.    <b:property name="passwordEncoder" ref="passwordEncoder"/>  
    26.    <b:property name="sessionAuthenticationStrategy" ref="sas" />**  
    27. </b:bean>  

加*号的地方是需要注意的
附上我的applicationContext-security.xml配置信息或许对你有帮助
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <b:beans xmlns="http://www./schema/security"  
  3.     xmlns:b="http://www./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd  
  6.                         http://www./schema/security http://www./schema/security/spring-security-3.1.xsd">  
  7.     <global-method-security pre-post-annotations="enabled" />     
  8.       
  9.       <!-- 不要过滤图片等静态资源,其中**代表可以跨越目录,*不可以跨越目录。 -->  
  10.     <http pattern="/static/**" security="none"/>   
  11.     <http pattern="/commons/**" security="none" />  
  12.     <http pattern="/images/**" security="none" />  
  13.     <http pattern="/js/**" security="none" />  
  14.     <http pattern="/themes/**" security="none" />  
  15.   
  16.       
  17.     <http use-expressions="true"     entry-point-ref="authenticationProcessingFilterEntryPoint">  
  18.         <logout  logout-url="/j_logout" logout-success-url="/user/login.do"/>    
  19.         <!-- 实现免登陆验证 -->    
  20.         <remember-me />    
  21.           
  22.         <!--     
  23.          error-if-maximum-exceeded   true限制不允许第二个用户登录,false第二个登陆用户踢掉前一个登陆用户   
  24.          session-fixation-protection  防止伪造sessionid攻击,用户登录成功后会销毁用户当前的session。    
  25.           限制用户的最大登陆数,防止一个账号被多人使用 -->  
  26.         <custom-filter ref="loginFilter" position="FORM_LOGIN_FILTER"  />    
  27.   <session-management invalid-session-url="/timeout.jsp" session-authentication-error-url="/s.jsp" session-authentication-strategy-ref="sas" />  
  28.           
  29.         <!-- 自定义的过滤器,要在FILTER_SECURITY_INTERCEPTOR过滤器之前 -->  
  30.         <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR" />  
  31.          <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />    
  32.     </http>  
  33.       
  34.   
  35.       
  36.      <b:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">  
  37.           <b:constructor-arg name="sessionRegistry" ref="sessionRegistry" />  
  38.           <b:property name="maximumSessions" value="1" /><!--value="-1" 的时候不限制数量-->  
  39.      </b:bean>  
  40.          
  41.   <authentication-manager alias="authManager">    
  42.     <authentication-provider user-service-ref="myUserDetailService ">    
  43.         <password-encoder hash="md5">    
  44.         </password-encoder>    
  45.     </authentication-provider>    
  46.   </authentication-manager>    
  47. <!-- SESSION管理 -->  
  48.     <b:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />  
  49.     <b:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">  
  50.         <b:property name="sessionRegistry" ref="sessionRegistry"  />  
  51.         <b:property name="expiredUrl" value="/timeout.jsp" /><!-- 过期的Url -->  
  52.     </b:bean>  
  53.   
  54. <!-- 登录验证器 -->    
  55.     <b:bean id="loginFilter"          class="com.web.security.MyUsernamePasswordAuthenticationFilter">    
  56.         <!-- 处理登录的action -->    
  57.         <b:property name="filterProcessesUrl" value="/j_spring_security_check"/>    
  58.                 <!-- 验证成功后的处理-->    
  59.         <b:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"/>    
  60.                 <!-- 验证失败后的处理-->    
  61.         <b:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"/>    
  62.         <b:property name="authenticationManager" ref="authenticationManager"/>  
  63.         <!-- 注入DAO为了查询相应的用户 -->    
  64.         <b:property name="userDao" ref="userDao"/>    
  65.         <b:property name="passwordEncoder" ref="passwordEncoder"/>    
  66.           
  67.         <b:property name="sessionAuthenticationStrategy"   ref="sas" />  
  68.     </b:bean>    
  69.      <b:bean id="loginLogAuthenticationSuccessHandler"  class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler" >    
  70.         <b:property  name="defaultTargetUrl"  value="/user/login.do"/>    
  71.     </b:bean>    
  72.     <b:bean id="simpleUrlAuthenticationFailureHandler"         class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">    
  73.         <!-- 可以配置相应的跳转方式。属性forwardToDestination为true采用forward false为sendRedirect -->    
  74.         <b:property name="defaultFailureUrl"  value="/user/login.do?errorType=100"/>    
  75.     </b:bean>    
  76.     <!-- 认证过滤器 -->    
  77.     <!-- 自定义过滤器 :认证器、决策器、资源权限加载 -->  
  78.     <b:bean id="myFilter"    class="com.oms.core.security.MyFilterSecurityInterceptor">  
  79.      <!-- 用户拥有的权限 -->    
  80.         <b:property name="authenticationManager" ref="authenticationManager"/>  
  81.      <!-- 用户是否拥有所请求资源的权限 -->    
  82.         <b:property name="accessDecisionManager" ref="accessDecisionManager"/>  
  83.      <!-- 资源与权限对应关系 -->    
  84.         <b:property name="securityMetadataSource" ref="securityMetadataSource"/>  
  85.     </b:bean>  
  86.   
  87.     <b:bean id="hibernateTemplate"   class="org.springframework.orm.hibernate3.HibernateTemplate">  
  88.         <b:property name="sessionFactory" ref="sessionFactory"></b:property>  
  89.     </b:bean>  
  90.   
  91.     <!-- 认证管理器:实现了UserDetailService接口主要根据用户名查找用户信息, -->  
  92.     <!--然后把用户信息传给和资源加载信息一块传给决策器进行判断 -->  
  93.     <authentication-manager alias="authenticationManager">  
  94.         <authentication-provider user-service-ref="myUserDetailService">  
  95.         </authentication-provider>  
  96.     </authentication-manager>  
  97.     <b:bean id="myUserDetailService" class="com.web.security.MyUserDetailService" />  
  98.     <!-- 决策管理器 -->  
  99.     <b:bean id="accessDecisionManager"   class="com.web.security.MyAccessDecisionManager"></b:bean>  
  100.     <!-- 加载资源 和权限的关系数据 -->  
  101.     <b:bean id="securityMetadataSource"  class="com.web.security.MyInvocationSecurityMetadataSource">  
  102.             <b:constructor-arg name="userService" ref="userService"></b:constructor-arg>  
  103.     </b:bean>  
  104.   
  105.    <!-- 未登录的切入点 -->    
  106.     <b:bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">    
  107.         <b:property name="loginFormUrl" value="/user/login.do"></b:property>    
  108.     </b:bean>  
  109.   
  110.     <!-- 国际化文件 -->  
  111.     <b:bean id="messageSource"  
  112.         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  113.         <b:property name="basename"  
  114.             value="classpath:org/springframework/security/messages_zh_CN"/>  
  115.     </b:bean>  
  116. </b:beans>  
参考文章:http://forum./forum/spring-projects/security/130963-sessionregistry-getallprincipals-return-empty
                     http://dead-knight./blog/1517582

以上博客地址可以很好的帮助你理解

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多