We had such a situation.
Here are two solutions. I did not like much
@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;
}
or
@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 this case, Spring will call your static isOk () method from the Decision class. It should return a boolean value.
Spring introduces the main basic permitted object for the method, you do not need to worry about it. Enable annotation @PreAuthorizewith
<security:global-method-security pre-post-annotations="enabled" />
The second use of the aspect. Create an aspect.
@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 {
Method method = ((MethodSignature)joinPoint.getMethodSignature()).getMethod();
}
}
@RequestMapping(value="/api/entry", method=RequestMethod.POST)
@ResponseBody
@Protector
public Entry save(@Valid @RequestBody Entry entry, Principal principal) {
this.entryService.save(entry);
return entry;
}
If you have an aspect, you can have more ownership at runtime.
Also see this ulr
source
share