Are multiple roles allowed in the @Secured annotation with 'or' condition in Spring Security

前端 未结 3 949
忘掉有多难
忘掉有多难 2020-12-31 02:09

I am using spring and spring security 4 in my project. I have to call my dao method with ROLE_USER or ROLE_TIMER_TASK.

Currently I am using this annotation -

<
3条回答
  •  余生分开走
    2020-12-31 02:55

    For or, use a @PreAuthorize annotation instead:

    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_TIMER_TASK')")
    

    In Spring Security version 4 the ROLE_ prefix can be omitted:

    @PreAuthorize("hasRole('USER') or hasRole('TIMER_TASK')")
    

    Make sure you have pre- and post-annotations enabled in your security config.

提交回复
热议问题