2012-07-26 25 views
6

現在、私は春のセキュリティと@PreAuthorize注釈を使用してメソッド呼び出しを保護しています。今私は、春のセキュリティのrun-as authentication replacementのようなメソッド呼び出しの認証トークンを変更したいと思います。春のセキュリティでメソッド呼び出しのセキュリティコンテキストを変更する

メソッドベースごとに置換を設定できますか?アノテーションごとに、SpELの式.... もしそうでなければ、runAsManagerでどのようなメソッドが呼び出されるのでしょうか? セキュリティで保護されたオブジェクトのセキュリティ設定の属性を設定するにはどうすればよいですか?

答えて

1

私は、自分でRunAsManagerを実装することでこれを解決しました。このメソッドは、呼び出されたメソッドのカスタム注釈をチェックし、適切なトークンを返します。

は素晴らしいです。

6

@PreAuthorizeと組み合わせてRun-Asを実装すると、a detailed articleが投稿されました。

1)Authenticationを作成し、任意のカスタムロジックに基づくメソッド実行中に使用する独自のRunAsManagerを実装します。この実装は、保護された方法(例えば@RunAsRole("ROLE_AUDITOR"))にカスタム@RunAsRole注釈を探します

public class AnnotationDrivenRunAsManager extends RunAsManagerImpl { 

     @Override 
     public Authentication buildRunAs(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) { 
      if(!(object instanceof ReflectiveMethodInvocation) || ((ReflectiveMethodInvocation)object).getMethod().getAnnotation(RunAsRole.class) == null) { 
       return super.buildRunAs(authentication, object, attributes); 
      } 

      String roleName = ((ReflectiveMethodInvocation)object).getMethod().getAnnotation(RunAsRole.class).value(); 

      if (roleName == null || roleName.isEmpty()) { 
       return null; 
      } 

      GrantedAuthority runAsAuthority = new SimpleGrantedAuthority(roleName); 
      List<GrantedAuthority> newAuthorities = new ArrayList<GrantedAuthority>(); 
      // Add existing authorities 
      newAuthorities.addAll(authentication.getAuthorities()); 
      // Add the new run-as authority 
      newAuthorities.add(runAsAuthority); 

      return new RunAsUserToken(getKey(), authentication.getPrincipal(), authentication.getCredentials(), 
        newAuthorities, authentication.getClass()); 
     } 
    } 

と、見つかった場合は、与えられた権限(中ROLE_AUDITORを追加します。以下の例では、余分な役割を提供するカスタムアノテーションを使用していますこの場合)を許可された権限のリストに追加します。 RunAsRole自体は単なるカスタムアノテーションです。

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface RunAsRole { 
    String value(); 
} 

2)マネージャをインスタンス化:

<bean id="runAsManager" 
    class="org.springframework.security.access.intercept.RunAsManagerImpl"> 
    <property name="key" value="my_run_as_key"/> 
</bean> 

3)を登録しますコントローラで

<global-method-security pre-post-annotations="enabled" run-as-manager-ref="runAsManager"> 
    <expression-handler ref="expressionHandler"/> 
</global-method-security> 

4)使用例:

@Controller 
public class TransactionLogController { 

    @PreAuthorize("hasRole('ROLE_REGISTERED_USER')") //Authority needed to access the method 
    @RunAsRole("ROLE_AUDITOR") //Authority added by RunAsManager 
    @RequestMapping(value = "/transactions", method = RequestMethod.GET) //Spring MVC configuration. Not related to security 
    @ResponseBody //Spring MVC configuration. Not related to security 
    public List<Transaction> getTransactionLog(...) { 
    ... //Invoke something in the backend requiring ROLE_AUDITOR 
    { 

    ... //User does not have ROLE_AUDITOR here 
} 

EDIT: keyの値はRunAsManagerImplになります。ここでは、その使用上のSpring docsからの抜粋です:

は、悪質なコードが生成されたすべてのトークンに格納されているキーのRunAsUserTokenRunAsImplAuthenticationProviderによって保証受諾のために本 それを、 ハッシュを作成しないようにするには。 RunAsManagerImplRunAsImplAuthenticationProviderが同じキーを持つ ビーンコンテキストで作成される: 同じキーを使用して

<bean id="runAsManager" 
    class="org.springframework.security.access.intercept.RunAsManagerImpl"> 

<bean id="runAsAuthenticationProvider" 
    class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider"> 

、各RunAsUserTokenは、それが作成された検証することができます 承認済みRunAsManagerImplセキュリティ上の理由から の作成後にRunAsUserTokenが不変です。

+0

「my_run_as_key」は正確には何ですか? - 私はこれを理解できません。私はおそらくあなたがここで引用した同じガイドを読んでいますが、この鍵はちょうど私には意味をなさない。 –

+0

@DanielBo値は任意の値にすることができます。基本的にパスワードです。 私は、なぜSpring文書が必要なのかを引用する答えを更新しました。 – kaqqao

関連する問題