SpringBoot @PreAuthorize 使用 String.contains 或正则表达式在 hasAuthority 中

huangapple go评论105阅读模式
英文:

SpringBoot @PreAuthorize with String.contains or regex in hasAuthority

问题

我正在使用Spring Boot(2.7.6),并且正在查看@PreAuthorize注解。

我想知道如何在hasAuthority方法中使用String.Containsregex匹配来检查有效的权限。

例如,我有以下权限:

  1. MYAPI_ORDER_PROCESSING_DEV
  2. MYAPI_ORDER_PROCESSING_PROD
  3. MYAPI_ORDER_TRACKING_DEV
  4. MYAPI_ORDER_TRACKING_PROD

在某些情况下,我可能只需要MYAPI_ORDER_PROCESSING_DEV,在这种情况下,我可以使用:

  1. @PreAuthorize("hasAuthority('MYAPI_ORDER_PROCESSING_DEV')")

在其他情况下,我可以检查任何数量的权限,如:

  1. @PreAuthorize("hasAnyAuthority(
  2. 'MYAPI_ORDER_PROCESSING_DEV',
  3. 'MYAPI_ORDER_PROCESSING_PROD',
  4. 'MYAPI_ORDER_TRACKING_DEV',
  5. 'MYAPI_ORDER_TRACKING_PROD')")

这对我来说都很清楚和有效,但如果我有很长的权限列表,这可能会变得很长和复杂。

我想知道是否有一种方法可以使用String.contains(substring)regex来匹配多个权限。例如,类似于:

  1. @PreAuthorize("hasAuthority(authorityString.contains('MYAPI_ORDER_'))")

这将匹配上述的所有4个权限。

英文:

I am using Spring Boot (2.7.6) and I am looking at @PreAuthorize annotation.

I am wondering how can I use String.Contains or regex matching to check for valid authorities within hasAuthority method.

For example, I have following authorities:

  1. MYAPI_ORDER_PROCESSING_DEV
  2. MYAPI_ORDER_PROCESSING_PROD
  3. MYAPI_ORDER_TRACKING_DEV
  4. MYAPI_ORDER_TRACKING_PROD

In some cases, I might just need MYAPI_ORDER_PROCESSING_DEV in which case, I can use

  1. @PreAuthorize("hasAuthority('MYAPI_ORDER_PROCESSING_DEV')

In other cases, I can check for any of number of authorities like:

  1. @PreAuthorize("hasAnyAuthority(
  2. 'MYAPI_ORDER_PROCESSING_DEV',
  3. 'MYAPI_ORDER_PROCESSING_PROD',
  4. 'MYAPI_ORDER_TRACKING_DEV',
  5. 'MYAPI_ORDER_TRACKING_PROD')

This is all clear and working for me, but this might get to long and complicated if I have a long list of authorities.

I wonder if there is a way to use String.contains(substring) or regex to match multiple authorities. For example, something like:

  1. @PreAuthorize("hasAuthority(authorityString.Contains('MYAPI_ORDER_')

, which would match all 4 authorities above.

答案1

得分: 1

  1. 你可以创建一个处理检查权限逻辑的组件
  2. ```java
  3. @Component
  4. public class AuthorityHelper {
  5. public boolean hasAuthorityContaining(Authentication authentication, String substring) {
  6. return authentication.getAuthorities().stream()
  7. .map(GrantedAuthority::getAuthority)
  8. .anyMatch(i -> i.contains(substring));
  9. }
  10. }

由于@PreAuthorize支持SpEL表达式,可以定义如下:

  1. @PreAuthorize("@authorityHelper.hasAuthorityContaining(authentication, 'MYAPI_ORDER_')")

authentication是与方法调用关联的内置Authentication实例,其中包含权限等其他数据。

参考:Spring文档 - 使用SpEL表达式表示授权

  1. <details>
  2. <summary>英文:</summary>
  3. You can create a component that handles the logic for checking authorities:
  4. ```java
  5. @Component
  6. public class AuthorityHelper {
  7. public boolean hasAuthorityContaining(Authentication authentication, String substring) {
  8. return authentication.getAuthorities().stream()
  9. .map(GrantedAuthority::getAuthority)
  10. .anyMatch(i -&gt; i.contains(substring));
  11. }
  12. }

Since @PreAuthorize supports SpEL expressions, it can be defined as follows:

  1. @PreAuthorize(&quot;@authorityHelper.hasAuthorityContaining(authentication, &#39;MYAPI_ORDER_&#39;)&quot;)

authentication is a built-in Authentication instance associated with the method invocation and it contains authorities among other data.

Reference: Spring Documentation - Expressing Authorization with SpEL

huangapple
  • 本文由 发表于 2023年6月8日 06:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定