Spring Security – Run-As認証
1概要
このチュートリアルでは、Spring SecurityでRun-As認証を使用する方法を簡単なシナリオで説明します。
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
を持つコントローラ
それでは、Run-As認証の置き換えを使用する方法を見てみましょう。
@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()
メソッドより前の現在のユーザー権限は
-
tryRunAs()
メソッド内の現在のユーザー権限は
-
一時的な
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機能をテストするために単純なフロントエンドを使用します。
<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="https://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 Securityのhttp://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#runas[Run-As認証置換を使用した簡単な例を調べました]機能。
このチュートリアルはhttps://github.com/eugenp/learn-spring-security/tree/module7/m7-lesson3[GitHubで入手可能なコードベース]に基づいています。