Spring security @PreAuthorize hasRole() properties injection

后端 未结 3 1722
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 15:49

Assuming that my Spring Security and properties are configured properly, I would like to use role name from property like

@PreAuthorize(\"hasRole(\'${role.ro         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 16:32

    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() {}
    

提交回复
热议问题