`
json20080301
  • 浏览: 162467 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

spring security 实现remeber me(免登陆功能)的原理

 
阅读更多

spring security 实现免登陆功能大体也是基于COOKIE来实现的。

主要配置信息:

 <remember-me  data-source-ref="dataSource" key="rememberMeCookie"
   authentication-success-handler-ref="authenticationSuccessHandler"
    services-alias="rememberMeServices" />

 

1.首先登陆表单要Post URL: /j_spring_security_check 同时_spring_security_remember_me要等于yes,这时登陆后会记录cookie到数据库中;

/j_spring_security_check?_spring_security_remember_me=yes

代码逻辑:

UsernamePasswordAuthenticationFilter 登陆验证过滤器拦截/j_spring_security_check同时调用AbstractRememberMeServices 接口实现

 this.rememberMeServices.loginSuccess(request, response, authResult);

 

2.当会话失效时,这个时候RememberMeAuthenticationFilter 过滤器会调用this.rememberMeServices.autoLogin(request, response);自动登陆;

同时successHandler.onAuthenticationSuccess(request, response, rememberMeAuth); 可以进行一些会话信息加载,这个地方需要根据项目的需要进行改造。

 

AbstractRememberMeServices

   public final Authentication autoLogin(HttpServletRequest request, HttpServletResponse response) {

1.根据spring security 的cookiename获取spring security 保存的cookie

        String rememberMeCookie = extractRememberMeCookie(request);

 。。。。。

 

2.      解析rememberMeCookie的信息

         UserDetails user = null;

             String[] cookieTokens = decodeCookie(rememberMeCookie);

3.     获取cookie中信息,并生成登陆的session
            user = processAutoLoginCookie(cookieTokens, request, response);
            userDetailsChecker.check(user);

            logger.debug("Remember-me cookie accepted");

            return createSuccessfulAuthentication(request, user);
    
        cancelCookie(request, response);
        return null;
    }

 

processAutoLoginCookie方法由子类PersistentTokenBasedRememberMeServices  ,TokenBasedRememberMeServices来进行实现;

 

PersistentTokenBasedRememberMeServices 在登陆时保存登陆时的cookie备份,在处理processAutoLoginCookie时会首先比对cookie是否属于伪造的,

不是伪造的才可以获取登陆信息,进行登陆;这一点非常重要!!!

 

PersistentTokenBasedRememberMeServices 会实例化加密后的cookie信息到PersistentTokenRepository 接口的实现中,

private PersistentTokenRepository tokenRepository = new InMemoryTokenRepositoryImpl();

InMemoryTokenRepositoryImpl  的存储方式:Map<String, PersistentRememberMeToken> seriesTokens = new HashMap<String, PersistentRememberMeToken>();

JdbcTokenRepositoryImpl 的存储方式数据库表:推荐使用JdbcTokenRepositoryImpl 方式,这样集群环境下也可以实现cookie的信息的机器备份;

public static final String CREATE_TABLE_SQL =
            "create table persistent_logins (username varchar(64) not null, series varchar(64) primary key, " +
                    "token varchar(64) not null, last_used timestamp not null)";

 

官方配置reference参考:

<remember-me> Attributes
  • authentication-success-handler-ref Sets the authenticationSuccessHandler property on theRememberMeAuthenticationFilter if custom navigation is required. The value should be the name of aAuthenticationSuccessHandler bean in the application context.

  • data-source-ref A reference to a DataSource bean. If this is set, PersistentTokenBasedRememberMeServices will be used and configured with a JdbcTokenRepositoryImpl instance.

  • remember-me-parameter The name of the request parameter which toggles remember-me authentication. Defaults to "_spring_security_remember_me". Maps to the "parameter" property of AbstractRememberMeServices.

  • key Maps to the "key" property of AbstractRememberMeServices. Should be set to a unique value to ensure that remember-me cookies are only valid within the one application [26]. If this is not set a secure random value will be generated. Since generating secure random values can take a while, setting this value explicitly can help improve startup times when using the remember me functionality.

  • services-alias Exports the internally defined RememberMeServices as a bean alias, allowing it to be used by other beans in the application context.

  • services-ref Allows complete control of the RememberMeServices implementation that will be used by the filter. The value should be the id of a bean in the application context which implements this interface. Should also implement LogoutHandler if a logout filter is in use.

  • token-repository-ref Configures a PersistentTokenBasedRememberMeServices but allows the use of a customPersistentTokenRepository bean.

  • token-validity-seconds Maps to the tokenValiditySeconds property of AbstractRememberMeServices. Specifies the period in seconds for which the remember-me cookie should be valid. By default it will be valid for 14 days.

  • use-secure-cookie It is recommended that remember-me cookies are only submitted over HTTPS and thus should be flagged as "secure". By default, a secure cookie will be used if the connection over which the login request is made is secure (as it should be). If you set this property to false, secure cookies will not be used. Setting it to true will always set the secure flag on the cookie. This attribute maps to the useSecureCookie property of AbstractRememberMeServices.

  • user-service-ref The remember-me services implementations require access to a UserDetailsService, so there has to be one defined in the application context. If there is only one, it will be selected and used automatically by the namespace configuration. If there are multiple instances, you can specify a bean id explicitly using this attribute.

分享到:
评论

相关推荐

    Remeber-Me-:瓜达·卡洛托斯(Guarda Contactos)

    记住账号! ... Nome 井出 Numero de Telefone } Ele usa apenas ummódulo: PyQt5 ea ferramenta(nãonecessáriaparautilização): QtDesign ... 基本信息.txt ,是不提供任何菜单信息的替代品。...

    Shiro反序列化漏洞,Shiro版本升级资源

    shiro版本时,其参数remeberMe存在硬编码,它对于cookie的处理流程是,首先获取rememberMe的cookie值,然后进行Base64解码,再进行AES解密,最后反序列化。但在这个过程中,其AES的Key硬编码,导致反序列化漏洞的...

    接收机抽取的Matlab仿真代码-Data-Center-Simulation:数据中心的Incast拥塞控制

    (按“名称”或“我的代码”)每个文件的功能如下所示: 修改后的文件: 接收窗口,吞吐量测量值添加到:tcp-socket-base.h tcp-socket-base.cc 慢启动和拥塞控制已添加到:tcp-congestion-ops.h tcp-congestion-ops...

    C#贪吃蛇源代码+设计说明

    当打开游戏界面时,音乐会自动播放,默认的音乐是:Remeber。 若玩家要修改或关闭音乐,可以通过:选项-&gt;音乐 或是 F5快捷键 打开窗口。 若玩家不想播放音乐,请点击关闭。 若玩家要播放选中的音乐,请点击确定...

    C++播放音乐:使用EGE图形库

    这是一首歌,你不搞本次活动也可以,至少听听歌吧。 这首歌是张韶涵唱的一手英文歌,叫做《Remeber Our Summer》,意思是记住我们的夏天,可我更喜欢叫它忆夏。

    AzureMLCapstoneProject:Udacity Azure机器学习纳米学位的项目部分

    待办事项:向您解释该数据集要解决的任务以及将要使用的功能。 使用权 TODO :说明您如何访问工作空间中的数据。 自动化机器学习 待办事项:概述用于此实验的automl设置和配置 结果 待办事项:使用自动ML模型得到的...

    顶峰计划

    注意:此文件是一个模板,可用于为您的项目创建自述文件。 下面的TODO注释将突出显示您应确保包含的信息。... TODO Remeber提供RunDetails小部件的屏幕截图,以及使用其参数训练的最佳模型的屏幕截图。 超参数调整 TO

    Microsoft_Udacity_Scholarship_Capstone_Project

    注意:此文件是一个模板,可用于为您的项目创建自述文件。 下面的TODO注释将突出显示您应确保包含的信息。... TODO Remeber提供RunDetails小部件的屏幕截图,以及使用其参数训练的最佳模型的屏幕截图。 超参数调整 TO

    AzureML-Capstone:用于Udacity的Capstone项目课程-具有Microsoft Azure的ML工程师

    待办事项:向您解释该数据集要解决的任务以及将要使用的功能。 使用权 TODO :说明您如何访问工作空间中的数据。 自动化机器学习 待办事项:概述用于此实验的automl设置和配置 结果 待办事项:使用自动ML模型得到的...

    machine_learning_azure_capstone:在Azure Project 3上进行Udacity机器学习

    任务待办事项:向您解释该数据集要解决的任务以及将要使用的功能。使用权TODO :说明您如何访问工作空间中的数据。自动化机器学习待办事项:概述用于此实验的automl设置和配置结果待办事项:使用自动ML模型得到的...

    Azure-Machine-Learning-Capstone-Project

    任务待办事项:向您解释该数据集要解决的任务以及将要使用的功能。使用权TODO :说明您如何访问工作空间中的数据。自动化机器学习待办事项:概述用于此实验的automl设置和配置结果待办事项:使用自动ML模型得到的...

    Login Control

    Show me if you have a sample project to do so). 10. Now you are ready to use the control in your Web form. Just drag the LoginControl icon in the ToolBox pane and drop it to the Web form. The ...

    grt-scaffold

    GRT支架它与写与框架。 要在您的项目上进行安装,只需使用npm添加: $ cd your project$ npm install grt-scaffold如何... [option value] typeAttention: Remeber to enable the Bootstrap in your layoutOptions: --v

    切记:捕获,编辑和保存

    Remeber使用firestore存储用户注释,并使用Editor.js库编辑注释。 记住,使用三个窗格布局来组织,编辑和保存笔记。 核心依赖 包 对于 Redux工具包 国家管理 火力基地 Firebase SDK Editor.js和插件 编辑笔记 ...

    gnavi-mongo:Node.js和MongDB上的3层单页应用程序

    欢迎来到gnavi-mongo演示! 概述 这是一个3层SPA或单页应用程序演示,它在 (一种 PaaS...Remeber安装 ,然后第一。 然后,安装依赖项: npm install 然后,运行该应用程序: npm start 您可以在以下位置访问您的

    Operationalizing_Machine_Learning

    TODO Remeber提供RunDetails小部件的屏幕截图以及使用其参数训练的最佳模型的屏幕截图。 屏幕录像 TODO提供指向正在执行的项目的屏幕录像的链接。 请记住,截屏视频应演示: 突出建议 TODO(可选):在这里您可以...

    crtmpserver源码

    Remeber to omit the .flv if it's an FLV file * Open a web browser and point it to to the web server IP/Hostname and the directory you installed the player (example: http://127.0.0.1/player) * You ...

    labview_Demo 傅里叶级数.zip

    labview_傅里叶级数

Global site tag (gtag.js) - Google Analytics