spring security实现单用户登录

建议先看完spring security的基础配置,再看下面的配置流程就会很明白了。

https://blog.csdn.net/u013984781/article/details/79755131    

在完成spring基础配置之后,可以在配置限制单个用户只能一处登录应用程序的功能,Spring Security支持这种开箱即用的功能。

    第一步:使用自定义过滤器,配置ConcurrentSessionFilter,用来判断session是否过期以及更新最新访问时间;

    在配置收到HTTP请求时的安全验证中添加以下配置:

    并配置相应的


    
    

    第二步:注入SessionAuthenticationStrategy到CustomUsernamePasswordAuthenticationFilter中,否则默认使用的是NullAuthenticatedSessionStrategy,则获取不到登录用户数;



    
    
    
    
    
    

其中“sas”配置为:


	
	    
	        
	        
		    
		    
		    
		    
	        
	        
		
			
	        
	    
	

“sessionRegistry”配置为:

配置完成后,重跑程序发现功能并没有实现,在调试过程中发现,判断当前用户是否重复时,这里的sessionCount总是0

spring security实现单用户登录_第1张图片

于是,我点开sessionReistry.getAllSessions方法

spring security实现单用户登录_第2张图片

发现这里取principle是根据HashMap取值的,hashMap判断相等需要hashCode相同,因此两个相同用户登录的时候需要相同的hashCode。在实现userDetails接口的用户信息中添加以下代码

     /**
     * 当同一用户登录多次时,获取的用户不是同一个用户
     * 所以需要重写hashcode和equals方法
     */
    @Override
    public boolean equals(Object rhs) {
        if (rhs instanceof CustomSecurityUser) {
            return getUsername().equals(((CustomSecurityUser) rhs).getUsername());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return getUsername().hashCode();
    }

 

你可能感兴趣的:(spring security实现单用户登录)