0

Java - Spring Annotation param from property value

Following is my @Secured annotation i want to define string "USER_ABC" in .properties file and use it over here like @Secured({myProp}) but it gives me error

@Value('${my.property}') private string myProp;

   @Secured({myProp,"ADMIN_123"})
    public void mySecureMethod(){
    }

instead of

@Secured({"USER_ABC","ADMIN_123"})
public void mySecureMethod(){
}

any solution appreciated in advance.

d-man
  • 57,473
  • 85
  • 212
  • 296
  • It is my understanding that annotation parameters must be [constant expressions](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28). What you're trying to do is not one of those unfortunately. – Edwin Dalorzo Jan 06 '15 at 16:45

1 Answers1

1

While you can't use @Secured because as Edwin points out, it can't handle constant expressions, you can use the @PreAuthorize annotation which will evaluate an SPEL expression.

Just be aware that @PreAuthorize works slightly differently in terms of the Spring Security Filter chain and when it gets invoked.

There is a very good question that should probably be your first port of call here. If there is some reason why you can't use @PreAuthorize, let me know.

Community
  • 1
  • 1
JamesENL
  • 6,400
  • 6
  • 39
  • 64