How concatenate two string in Spring Expression Language (SpEL)

孤者浪人 提交于 2019-12-08 21:03:54

问题


In my spring application, the methods from my controller and service classes have this annotation to security purposes:

@PreAuthorize("hasPermission(#user, 'cadastra')")

the second argument, the permission, should have this format:

<<action_name>>_<<class_name>>

What expression I should use to accomplish that, taking in consideration the class name is held by this.getClass().getName()?


回答1:


To concatenate two strings in Spring EL you use concat function . See here for more details : Spring EL docs

for example, I used the following :

    @PreAuthorize("hasRole('ROLE_'.concat(this.class.simpleName))")



回答2:


I finally solve this. I add a new method in my controller:

public String getName() {
    String nome_classe = entityClass.getSimpleName();
    System.out.println("getName nome_class = "+nome_classe);
    return nome_classe;
}

and now I use the annotation in that way:

@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")


来源:https://stackoverflow.com/questions/24121277/how-concatenate-two-string-in-spring-expression-language-spel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!