How concatenate two string in Spring Expression La

2019-06-18 07:30发布

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()?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-06-18 07:41

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))")
查看更多
看我几分像从前
3楼-- · 2019-06-18 07:51

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

public String getName() {
    String expressao = entityClass.getName();
    String nome_classe = new String();
    StringTokenizer st = new StringTokenizer(expressao, ".");
    while (st.hasMoreTokens()) {
        nome_classe = st.nextToken();
    }
    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)")
查看更多
登录 后发表回答