Abbiamo avuto questo tipo di situazione.
Ecco la soluzione. Non mi è piaciuta molto
@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;
}
o
@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;
}
// In questo caso Primavera chiamerà il metodo statico Isok() dalla classe decisione. Dovrebbe tornare booleano.
Spring injects Oggetto principale principale oggetto autorizzato per il metodo, non devi preoccuparti di questo. Abilita @PreAuthorize
annotazione con
<security:global-method-security pre-post-annotations="enabled" />
secondo utilizzando Aspect. Crea aspetto.
@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;
}
Se hai aspetto si può avere più possedere il tempo di esecuzione
Fare riferimento a questo ulr
Dopo aver provato entrambi gli approcci sono stato più felice con '@ PreAuthorize'. Per chiunque altro si è imbattuto in questo, la sicurezza del req body è stata alquanto complessa per me e ho potuto legare un bean con alcuni servizi. L'EL cambia leggermente quando si chiama un'istanza di bean. Esempio: '@PreAuthorize (" @ decision.isOK (# entry.blog.id, principal) ")' –
Sono felice che abbia aiutato :) '@ PreAuthorize' è esattamente per questo scopo, mentre @Aspect è più generale:) – Elbek
I parametri del metodo di riferimento da Spring-EL in @PreAuthorize devono essere preceduti da un prefisso '' '' # '' '' '. '' '' @PreAuthorize ("Decision.isOK (#entry, #principal)") '' '' invece di '' '' @PreAuthorize ("Decision.isOK (entry, principal)") '' ''. –