Spring Security @PreAuthorization pass enums in directly

前端 未结 4 1564
清歌不尽
清歌不尽 2021-02-07 03:29

My question is a duplicate of Custom annotation with spring security but it went unanswered and I believe there should be a simple solution to the problem.

Basically ins

4条回答
  •  轮回少年
    2021-02-07 03:48

    You can create static annotations like this:

    @ReadPermission
    

    By moving @PreAuthorize annotation to @ReadPermissiondefinition:

    @Inherited    
    @PreAuthorize("hasRole(T(fully.qualified.Permission).READ.roleName())")
    public @interface ReadPermission {
    
    }
    

    Benefit of this is, that you can then change Spring SPEL expression in one place, instead of modifying it on every method. One more plus is, that you can use this annotation on Class level - every method then would be secured with this annotation. It's useful for AdminControllers etc..

提交回复
热议问题