Spring Security - Run-As認証

Spring Security – Run-As認証

1. 概要

このチュートリアルでは、SpringSecurityで実行認証を使用する方法を簡単なシナリオで説明します。

Run-Asの非常に高度な説明は次のとおりです。ユーザーは、異なる特権を持つ別のプリンシパルとしてロジックの一部を実行できます。

2. RunAsManager

最初に行う必要があるのは、GlobalMethodSecurityを設定し、RunAsManagerを挿入することです。

これは、一時的なAuthenticationオブジェクトに追加の特権を与える責任があります。

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    @Override
    protected RunAsManager runAsManager() {
        RunAsManagerImpl runAsManager = new RunAsManagerImpl();
        runAsManager.setKey("MyRunAsKey");
        return runAsManager;
    }
}

runAsManagerをオーバーライドすることで、基本クラスのデフォルトの実装を置き換えます。これは単にnullを返します。

また、keyプロパティにも注意してください。フレームワークはこれを使用して、一時的なAuthenticationオブジェクト(このマネージャーを介して作成)を保護/検証します。

最後に–結果のAuthenticationオブジェクトはRunAsUserTokenです。 __

3. セキュリティ構成

一時的なAuthenticationオブジェクトを認証するために、RunAsImplAuthenticationProviderを設定します。

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    ...
    auth.authenticationProvider(runAsAuthenticationProvider());
}

@Bean
public AuthenticationProvider runAsAuthenticationProvider() {
    RunAsImplAuthenticationProvider authProvider = new RunAsImplAuthenticationProvider();
    authProvider.setKey("MyRunAsKey");
    return authProvider;
}

もちろん、マネージャーで使用したのと同じキーを使用してこれを設定します。これにより、プロバイダーは、RunAsUserToken認証オブジェクトが同じキーを使用して作成されていることを確認できます。

4. @Securedのコントローラー

では、実行認証の置換を使用する方法を見てみましょう。

@Controller
@RequestMapping("/runas")
class RunAsController {

    @Secured({ "ROLE_USER", "RUN_AS_REPORTER" })
    @RequestMapping
    @ResponseBody
    public String tryRunAs() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return "Current User Authorities inside this RunAS method only " +
          auth.getAuthorities().toString();
    }

}

ここで重要なのは、新しい役割–RUN_AS_REPORTERです。 これはRun-As機能のトリガーです。これは、プレフィックスのためにフレームワークが異なる方法で処理するためです。

このロジックを介してリクエストを実行すると、次のようになります。

  • tryRunAs()メソッドの前の現在のユーザー権限は[ROLE_USER]です

  • tryRunAs()メソッド内の現在のユーザー権限は[ROLE_USER, ROLE_RUN_AS_REPORTER]です。

  • 一時的なAuthenticationオブジェクトは、tryRunAS()メソッドの呼び出しの間のみ、既存の認証オブジェクトを置き換えます

5. サービス

最後に、実際のロジックを実装しましょう。これもセキュリティで保護されたシンプルなサービスレイヤーです。

@Service
public class RunAsService {

    @Secured({ "ROLE_RUN_AS_REPORTER" })
    public Authentication getCurrentUser() {
        Authentication authentication =
          SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }
}

ご了承ください:

  • getCurrentUser()メソッドにアクセスするには、ROLE_RUN_AS_REPORTERにアクセスする必要があります

  • したがって、tryRunAs()コントローラーメソッド内でのみgetCurrentUser()メソッドを呼び出すことができます

6. フロントエンド

次に、シンプルなフロントエンドを使用してRun-As機能をテストします。



Current user authorities:
    user

Generate Report As Super User

したがって、ユーザーが「Generate Report As Super User」アクションをトリガーすると、一時的なROLE_RUN_AS_REPORTER権限が取得されます。

7. 結論

このクイックチュートリアルでは、Spring SecurityRun-As authentication replacement機能を使用した簡単な例について説明しました。

このチュートリアルは、codebase available on GitHubに基づいています。