Spring Security Remember Me – Spring的安全 记住我

最后修改: 2013年 8月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This tutorial will show how to enable and configure Remember Me functionality in a web application with Spring Security. Setting up the MVC application with security and a simple form login has already been discussed.

本教程将展示如何使用Spring Security在Web应用程序中启用和配置Remember Me功能。前面已经讨论了设置具有安全性的MVC应用程序和简单的表单登录

The mechanism will be able to identify the user across multiple sessions – so the first thing to understand is that Remember Me only kicks in after the session times out. By default, this happens after 30 minutes of inactivity, but timeout can be configured in the web.xml.

该机制将能够在多个会话中识别用户–因此,首先要了解的是,记住我只在会话超时后启动。默认情况下,这发生在不活动的30分钟之后,但可以在web.xml中配置超时。

Note: this tutorial focuses on the standard cookie-based approach. For the persistent approach, have a look at the Spring Security – Persistent Remember Me guide.

注意:本教程侧重于基于cookie的标准方法。对于持久性方法,请查看Spring Security – Persistent Remember Me指南。

2. The Security Configuration

2.安全配置

Let’s see how to set up the security configuration using Java:

让我们看看如何使用Java设置安全配置。

@Configuration
@EnableWebSecurity
public class SecSecurityConfig {

    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
        return http.getSharedObject(AuthenticationManagerBuilder.class)
            .build();
    }

    @Bean
    public InMemoryUserDetailsManager userDetailsService() {
        UserDetails user = User.withUsername("user1")
            .password("{noop}user1Pass")
            .authorities("ROLE_USER")
            .build();
        UserDetails admin = User.withUsername("admin1")
            .password("{noop}admin1Pass")
            .authorities("ROLE_ADMIN")
            .build();
        return new InMemoryUserDetailsManager(user, admin);
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/anonymous*")
            .anonymous()
            .antMatchers("/login*")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login.html")
            .loginProcessingUrl("/login")
            .failureUrl("/login.html?error=true")
            .and()
            .logout()
            .deleteCookies("JSESSIONID")
            .and()
            .rememberMe()
            .key("uniqueAndSecret");
        return http.build();
    }
}

As you can see, the basic configuration using the rememberMe() method is extremely simple while remaining very flexible through additional options. The key is important here – it is a private value secret for the entire application and it will be used when generating the contents of the token.

正如你所看到的,使用rememberMe()方法的基本配置非常简单,同时通过附加选项保持非常灵活。key在这里很重要–它是整个应用程序的私有价值秘密,它将在生成令牌内容时使用。

Additionally, the time the token is valid can be configured from the default of 2 weeks to – for example – one day using tokenValiditySeconds():

此外,可以使用tokenValiditySeconds()令牌的有效时间从默认的2周配置为–例如1天。

rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)

We can also have a look at the equivalent XML configuration:

我们也可以看一下相应的XML配置。

<http use-expressions="true">
    <intercept-url pattern="/anonymous*" access="isAnonymous()" />
    <intercept-url pattern="/login*" access="permitAll" />
    <intercept-url pattern="/**" access="isAuthenticated()" />

    <form-login login-page='/login.html' 
      authentication-failure-url="/login.html?error=true" />
    <logout delete-cookies="JSESSIONID" />

    <remember-me key="uniqueAndSecret"/>
</http>

<authentication-manager id="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="user1" password="{noop}user1Pass" authorities="ROLE_USER" />
            <user name="admin1" password="{noop}admin1Pass" authorities="ROLE_ADMIN" />
        </user-service>
    </authentication-provider>
</authentication-manager>

3. The Login Form

3.登录表格

The login form is similar to the one we used for form login:

登录表单类似于我们用于表单登录的表单

<html>
<head></head>

<body>
    <h1>Login</h1>

    <form name='f' action="login" method='POST'>
        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='username' value=''></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='password' /></td>
            </tr>
            <tr>
                <td>Remember Me:</td>
                <td><input type="checkbox" name="remember-me" /></td>
            </tr>
            <tr>
                <td><input name="submit" type="submit" value="submit" /></td>
            </tr>
        </table>
    </form>

</body>
</html>

Notice the newly added checkbox input – mapping to remember-me. This added input is enough to log in with remember me active.

注意新增加的checkbox输入–映射到remember-me。这个新增的输入足以在记住我的情况下进行登录。

This default path can also be changed as follows:

这个默认路径也可以按以下方式改变。

.rememberMe().rememberMeParameter("remember-me-new")

4. The Cookie

4.饼干

The mechanism will create an additional cookie – the “remember-me” cookie – when the user logs in.

该机制将在用户登录时创建一个额外的cookie–“记住我 “cookie。

The Remember Me cookie contains the following data:

Remember Me cookie包含以下数据。

  • username – to identify the logged-in principal
  • expirationTime – to expire the cookie; default is 2 weeks
  • MD5 hash – of the previous 2 values – username and expirationTime, plus the password and the predefined key

The first thing to notice here is that both the username and the password are part of the cookie – this means that, if either is changed, the cookie is no longer valid. Also, the username can be read from the cookie.

首先要注意的是,用户名密码都是cookie的一部分–这意味着,如果其中一个被改变,cookie就不再有效。此外,用户名可以从cookie中读取。

Additionally, it is important to understand that this mechanism is potentially vulnerable if the remember me cookie is captured. The cookie will be valid and usable until it expires or the credentials are changed.

此外,重要的是要了解,如果记住我的cookie被捕获,这种机制就有潜在的脆弱性。该cookie将是有效和可用的,直到它过期或凭证被改变。

5. In Practice

5.在实践中

To easily see the remember me mechanism working, you can:

要轻松地看到记住我的机制在工作,你可以。

  • log in with remember me active
  • wait for the session to expire (or remove the JSESSIONID cookie in the browser)
  • refresh the page

Without remember me active, after the cookie expires the user should be redirected back to the login page. With remember me, the user now stays logged in with the help of the new token/cookie.

如果没有激活 “记住我”,在cookie过期后,用户应该被重新引导到登录页面。在记住我的情况下,用户现在在新令牌/cookie的帮助下保持登录状态

6. Conclusion

6.结论

This tutorial showed how to set up and configure Remember Me functionality in the security configuration, and briefly described what kind of data goes into the cookie.

本教程展示了如何在安全配置中设置和配置 “记住我 “功能,并简要介绍了什么样的数据会进入cookie。

The implementation can be found in the example Github project – this is an Eclipse-based project, so it should be easy to import and run as it is.

可以在Github 示例项目中找到该实现 – 这是一个基于 Eclipse 的项目,因此应该很容易导入并按原样运行。

When the project runs locally, the login.html can be accessed on localhost.

当项目在本地运行时,login.html可以在localhost访问。