使用@PreAuthorize 的多个角色
Multiple roles using @PreAuthorize
检查多个角色是否具有方法级别的访问权限
我已经使用@PreAuthorize 注解来检查角色
@PreAuthorize("hasRole(\"" + AuthoritiesConstants.USER + "\",)" )
如何使用@PreAuthorize annotaion检查多个角色?
通过在 SpEL 表达式中使用 &&
或 ||
简单地组合角色
@PreAuthorize("hasRole('" + AuthoritiesConstants.USER + "')" +
" && hasRole('" + AuthoritiesConstants.ADMIN + "')" )
您可以创建自定义注释来验证许多角色和条件。 P.e.:
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_AGENT) " +
"|| hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_ADMIN)" +
"|| (hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_CUSTOMER) && #userId == principal.username)")
public @interface IsAuthenticatedAsAgentOrCustomerIsUserId {
}
然后,你可以使用这个注解如下:
@IsAuthenticatedAsAgentOrCustomerIsUserId
Folder findByUserIdAndType(@Param("userId") String userId, @Param("typeId") FolderType id);
此注释验证用户登录为角色 AGENT 或 ADMIN。如果用户具有 CUSTOMER 角色验证 userId
参数是否等于用户登录
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
hasAnyRole()
When you need to support multiple roles, you can use the hasAnyRole() expression.
@PreAuthorize("hasAnyRole('ADMIN','DB-ADMIN')")
https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html
https://www.appsdeveloperblog.com/spring-security-preauthorize-annotation-example/
检查多个角色是否具有方法级别的访问权限
我已经使用@PreAuthorize 注解来检查角色
@PreAuthorize("hasRole(\"" + AuthoritiesConstants.USER + "\",)" )
如何使用@PreAuthorize annotaion检查多个角色?
通过在 SpEL 表达式中使用 &&
或 ||
简单地组合角色
@PreAuthorize("hasRole('" + AuthoritiesConstants.USER + "')" +
" && hasRole('" + AuthoritiesConstants.ADMIN + "')" )
您可以创建自定义注释来验证许多角色和条件。 P.e.:
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_AGENT) " +
"|| hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_ADMIN)" +
"|| (hasRole(T(com.bs.dmsbox.api.constants.RoleConstants).ROLE_CUSTOMER) && #userId == principal.username)")
public @interface IsAuthenticatedAsAgentOrCustomerIsUserId {
}
然后,你可以使用这个注解如下:
@IsAuthenticatedAsAgentOrCustomerIsUserId
Folder findByUserIdAndType(@Param("userId") String userId, @Param("typeId") FolderType id);
此注释验证用户登录为角色 AGENT 或 ADMIN。如果用户具有 CUSTOMER 角色验证 userId
参数是否等于用户登录
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
hasAnyRole()
When you need to support multiple roles, you can use the hasAnyRole() expression.
@PreAuthorize("hasAnyRole('ADMIN','DB-ADMIN')")
https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html https://www.appsdeveloperblog.com/spring-security-preauthorize-annotation-example/