このような状況がありました。
ここに2つの解決策があります。私は春が決定クラスから静的ISOK()メソッドを呼び出しますその場合、//
または
@RequestMapping(value="/api/entry", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("Decision.isOK(entry, principal)")
public Entry save(@Valid @RequestBody Entry entry, Principal principal) {
this.entryService.save(entry);
return entry;
}
@RequestMapping(value="/api/entry", method=RequestMethod.POST)
@ResponseBody
@PreAuthorize("#entry.author.name == principal.name)"
public Entry save(@Valid @RequestBody Entry entry, Principal principal) {
this.entryService.save(entry);
return entry;
}
あまり好きではありませんでした。ブール値を返す必要があります。
Springは、プリンシパルプリンシパルプリンシパルの認証されたオブジェクトをメソッドに挿入しますが、心配する必要はありません。 アスペクトを使用して
<security:global-method-security pre-post-annotations="enabled" />
セカンドで@PreAuthorize
注釈を有効にします。アスペクトを作成する。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Protector {
}
@Aspect
@Component
public class MyAspect {
@Before("@annotation(com.xyz.Protector)")
public void before(JoinPoint joinPoint) throws Throwable {
//u can get method object from joinPoint object,
Method method = ((MethodSignature)joinPoint.getMethodSignature()).getMethod();
//As long as you have Method object you can read the parameter objects (Entry and Principal) with reflection.
//So Compare here: If entry.getOwner().getId().equal(principal.getName()) blah blah blah
}
}
@RequestMapping(value="/api/entry", method=RequestMethod.POST)
@ResponseBody
@Protector
public Entry save(@Valid @RequestBody Entry entry, Principal principal) {
this.entryService.save(entry);
return entry;
}
あなたは側面を持っている場合は、ランタイム
上でより多くの所有を持つことができます。また、私は `PreAuthorize` @で最も幸せだった両方のアプローチを試みた後、このulr
を参照してください。誰かがこれを見て、reqのボディを確保することは私のためにやや複雑で、私はいくつかのサービスでBeanを結びつけることができました。 Beanインスタンスを呼び出すとELが少し変化します。例: '@PreAuthorize(" @ entry.blog.id、principal) ")' –
'@ PreAuthorize'はまさにこの目的のためのものですが、@Aspectはより一般的です。 ) – Elbek
@PreAuthorizeのSpring-ELからの参照メソッドのパラメータには、シャープな '' '' '' '' ''という接頭辞を付ける必要があります。 @PreAuthorize( "Decision.isOK(entry、principal)") '' ''の代わりに '' Decision.isOK(#entry、#principal)」) '' ''の代わりに '' Decision.isOK –