How to check “hasRole” in Java Code with Spring Security?

前端 未结 18 1642
梦毁少年i
梦毁少年i 2020-11-28 20:54

How to check user authority or permission in Java Code ? For example - I want to show or hide button for user depending on role. There are annotations like:

         


        
18条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 21:23

    Instead of using a loop to find the authority from UserDetails you can do:

    Collection authorities = authentication.getAuthorities();
    boolean authorized = authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN"));
    

提交回复
热议问题