【问题标题】:Use spring security expressions in custom annotation在自定义注解中使用 spring 安全表达式
【发布时间】:2017-01-11 01:36:30
【问题描述】:

您好,我有一个带有 Spring Boot + Spring Security 的项目。 我正在构建一个我这样定义的自定义注释:

@Retention(RUNTIME)
@Target(METHOD)
public @interface CustomAnnotation {
   String condition();
   String[] fields() default {};
}

此注释将应用于类的方法。我希望“条件”参数是一个“弹簧安全表达式”,我将在一个评估表达式的方面进行评估,如果它是真的,它会做一些逻辑。

Aspect 定义如下:

@Pointcut("@annotation(customAnnotation)"  )
public void pointcutForCustomAnnotation(CustomAnnotation  customAnnotation) {
// Do nothing.
}

@Around("pointcutForCustomAnnotation(customAnnotation)")
public Object customAspect(ProceedingJoinPoint pjp, CustomAnnotation customAnnotation) throws Throwable   {
     // Here should go the logic to evaluate spring security expression
    String condition = customAnnotation.condition();
    String[] fieldsToHide = customAnnotation.fields();
}

当我指的是 Spring 安全表达式时,我指的是在 @Preauthorize、@PostAuthorize、@PreFilter @PostFilter spring 注释中使用的那些。 例如:

hasRole('ROLE_USER')
isAuthenticated()

如何评估方面的弹簧安全表达式?我想我可以轻松地学习完成这项工作的 Spring 框架类

【问题讨论】:

    标签: spring-security aspectj


    【解决方案1】:

    以下要点可以作为起点:

    Programmatically check Spring Security expressions

    它在 Grails 中针对相同问题的一些广告对我有用。 (我不得不更改表达式处理程序查找)

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 2016-05-18
      • 1970-01-01
      • 2015-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多