Spring Securityは私を覚えている

Spring Security Remember Me

1. 概要

このチュートリアルでは、Spring Securityを使用したhow to enable and configure Remember Me functionality in a web applicationを示します。 the MVC application with security and a simple form loginの設定についてはすでに説明しました。

メカニズムはidentify the user across multiple sessionsを実行できるようになります。したがって、最初に理解することは、Remember Meはafter the session times outでのみキックするということです。 デフォルトでは、これは30分間非アクティブになった後に発生しますが、timeout can be configuredweb.xmlに含まれます。

[.iframe-fluid] ##

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")
            ;
    }
}

ご覧のとおり、the basic configuration using the rememberMe() methodは非常に単純ですが、追加のオプションはありますが非常に柔軟です。 keyはここで重要です。これはアプリケーション全体のプライベート値シークレットであり、トークンのコンテンツを生成するときに使用されます。

さらに、time the token is valid can be configuredは、デフォルトの2週間から、たとえば、tokenValiditySeconds()を使用した1日までです。

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

同等のXML構成を確認することもできます。


    
    
    

    
    

    



    
        
            
            
        
    

3. ログインフォーム

ログインフォームはthe one we used for form loginに似ています。





    

Login

User:
Password:
Remember Me:

新しく追加されたcheckbox入力に注意してください–remember-meへのマッピング。 この追加された入力は、私を記憶した状態でログインするのに十分です。

このデフォルトパスは、次のように変更することもできます。

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

このメカニズムは、ユーザーがログインしたときに追加のCookie(「remember-me」Cookie)を作成します。

Remember Me cookieには、次のデータが含まれています。

  • username –ログインしたプリンシパルを識別するため

  • expirationTime –Cookieを期限切れにします。デフォルトは2週間です

  • MD5 hash –前の2つの値の–usernameexpirationTime、およびpasswordと事前定義されたkey

ここで最初に注意することは、usernamepasswordの両方がCookieの一部であるということです。つまり、どちらかが変更されると、Cookieは無効になります。 また、usernameはCookieから読み取ることができます。

さらに、remember me cookieがキャプチャされた場合、このメカニズムは潜在的に脆弱であることを理解することが重要です。 有効期限が切れるまで、または資格情報が変更されるまで、The cookie will be valid and usable

5. 実際には

Remember Meメカニズムが機能していることを簡単に確認するには、次のようにします。

  • アクティブな私を覚えてログイン

  • セッションが期限切れになるのを待ちます(またはブラウザでJSESSIONID Cookieを削除します)

  • ページを更新

私がアクティブであることを覚えていない場合、Cookieの有効期限が切れると、ユーザーはredirected back to the login pageになります。 私のことを思い出してください。ユーザーは新しいトークン/ Cookieの助けを借りてstays logged inになりました。

6. 結論

このチュートリアルでは、セキュリティ構成でRemember Me機能を設定および構成する方法を示し、Cookieに入力されるデータの種類について簡単に説明しました。

実装はthe example github projectにあります。これはEclipseベースのプロジェクトであるため、そのままインポートして実行するのは簡単です。

プロジェクトがローカルで実行されている場合、localhostlogin.htmlにアクセスできます。