1. 概要

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

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です。 これが実行機能のトリガーです。プレフィックスが原因でフレームワークが異なる方法で処理するためです。

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

  • tryRunAs()メソッドの前の現在のユーザー権限は[ROLE_USER]です。
  • tryRunAs()メソッド内の現在のユーザー権限は[ ROLE_USER、ROLE_RUN_AS_REPORTER]です。
  • 一時的なAuthenticationオブジェクトは、 tryRunAS()メソッドの呼び出しの間のみ、既存のAuthenticationオブジェクトを置き換えます

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. フロントエンド

次に、単純なフロントエンドを使用して、実行機能をテストします。

<html>
<body>
Current user authorities: 
    <span sec:authentication="principal.authorities">user</span>
<br/>
<span id="temp"></span>
<a href="#" onclick="tryRunAs()">Generate Report As Super User</a>
             
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function tryRunAs(){
    $.get( "/runas" , function( data ) {
         $("#temp").html(data);
    });
}
</script>
</body>
</html>

したがって、ユーザーが「スーパーユーザーとしてレポートを生成」アクションをトリガーすると、一時的なROLE_RUN_AS_REPORTER権限を取得します。

7. 結論

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

このチュートリアルは、GitHubで利用可能なコードベースに基づいています。