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

前端 未结 18 1623
梦毁少年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:00

    you can use the isUserInRole method of the HttpServletRequest object.

    something like:

    public String createForm(HttpSession session, HttpServletRequest request,  ModelMap   modelMap) {
    
    
        if (request.isUserInRole("ROLE_ADMIN")) {
            // code here
        }
    }
    

提交回复
热议问题