关于后端:Spring-Security-配置-Remember-Me

7次阅读

共计 3309 个字符,预计需要花费 9 分钟才能阅读完成。

1。概述

本教程将展现 如何应用 Spring Security 在 Web 应用程序中启用和配置 Remember Me。之前曾经探讨过设置平安和简略表单登录的 MVC 应用程序。

该机制将可能 跨多个会话辨认用户——首先要理解的是,Remember Me 仅在会话超时后才会启动。默认状况下,用户在 30 分钟不沉闷后会超时,但在 web.xml 中能够配置超时工夫。

留神:本教程重点介绍 基于规范 cookie 的办法。对于长久化办法,请查看 Spring Security — Persistent Remember Me 指南。

2。平安配置

让咱们看看如何应用 Java 设置平安配置:

@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {@Bean("authenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication()
            .withUser("user1").password("{noop}user1Pass").roles("USER")
            .and()
            .withUser("admin1").password("{noop}admin1Pass").roles("ADMIN");
    }

    @Override
    protected void configure(final 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")
            ;
    }
}

如您所见,应用 rememberMe() 的根本配置 非常简单,同时通过附加选项保障可扩展性。key 在这里很重要——它是整个应用程序的公有秘钥,将在生成令牌的内容时应用。

此外,能够应用 tokenValiditySeconds() 令牌无效工夫从默认的 2 周配置为 – 例如 – 一天:

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

咱们还能够看看等价的 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。登录表格

登录表单相似于咱们用于表单登录的那个:

<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>

留神新增加的 checkbox 输出会映射到 remember-me。这个增加的输出足以在 Remember Me 激活的状况下登录。

此默认门路也能够更改为:

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

4。Cookie

该机制将在用户登录时创立一个额定的 cookie——”remember-me” cookie。

Remember Me cookie 蕴含以下数据:

  • 用户名 — 标识登录的主体
  • expirationTime — 使 cookie 过期;默认为 2 周
  • MD5 hash — 前 2 个值的 — usernameexpirationTime,加上 password 和预约义的 key

这里首先要留神的是 usernamepassword 都是 cookie 的一部分——这意味着,如果其中任何一个被更改,cookie 将不再无效。此外,能够从 cookie 中读取 username

此外,如果 Remember Me 的 cookie 被捕捉,此机制可能会受到攻打。cookie 将是无效且可用的,直到它过期或更改凭据。

5。理论应用

要查看 Remember Me 机制的工作状况,您须要:

  • 登录时记得我沉闷
  • 期待会话过期(或删除浏览器中的 JSESSIONID cookie)
  • 刷新页面

如果 Remember Me 曾经生效,cookie 过期后用户应该被 redirected back to the login page。登录当前用户会在 在新令牌 /cookie 的帮忙下放弃登录状态

6。论断

本教程展现了如何在平安配置中设置和配置 Remember Me,并简要形容了记录在 cookie 的数据类型。

具体实现能够在示例 Github 我的项目 — 这是一个基于 Eclipse 的我的项目,所以它应该很容易导入和运行。

当我的项目在本地运行时,能够在 [localhost] 上拜访 login.html(http://localhost:8080/spring-… “Access the project on localhost”)。

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0