@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からの抜粋です:
は、悪質なコードが生成されたすべてのトークンに格納されているキーのRunAsUserToken
とRunAsImplAuthenticationProvider
によって保証受諾のために本 それを、 ハッシュを作成しないようにするには。 RunAsManagerImpl
とRunAsImplAuthenticationProvider
が同じキーを持つ ビーンコンテキストで作成される: 同じキーを使用して
<bean id="runAsManager"
class="org.springframework.security.access.intercept.RunAsManagerImpl">
<bean id="runAsAuthenticationProvider"
class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider">
、各RunAsUserToken
は、それが作成された検証することができます 承認済みRunAsManagerImpl
セキュリティ上の理由から の作成後にRunAsUserToken
が不変です。
「my_run_as_key」は正確には何ですか? - 私はこれを理解できません。私はおそらくあなたがここで引用した同じガイドを読んでいますが、この鍵はちょうど私には意味をなさない。 –
@DanielBo値は任意の値にすることができます。基本的にパスワードです。 私は、なぜSpring文書が必要なのかを引用する答えを更新しました。 – kaqqao