Spring security @PreAuthorize hasRole() properties injection
I've found that you can just grab the propertyResolver and pull values directly from that, instead of writing your own class as was suggested by @Maksym.
Exammple:
@PreAuthorize("hasRole(@environment.getProperty('role.rolename')")
public void method() {}
Try to remove ''
signs:
@PreAuthorize("hasRole(${role.rolename})")
public void method() {}
EDIT. I am sure that there is a better way, but as a workaround you can call some method on some bean:
@Component("appVariablesHolder")
public class AppVariablesHolder {
@Value("${role.rolename}")
private String someRole;
public String getSomeRole() {
return this.someRole;
}
}
@PreAuthorize("hasRole(@appVariablesHolder.getSomeRole())")
public void method() {}