【问题标题】:check inside method which role @preauthorize has passed for spring controller method检查内部方法 @preauthorize 已为 spring 控制器方法传递了哪个角色
【发布时间】:2018-07-27 13:17:34
【问题描述】:

我正在使用@preauthorize 来确保方法级别的安全性。对于我的方法1,两个角色之一只能满足。但是在那个方法中,我需要知道它这次满足了哪个角色,并基于此我需要为功能需求设置后续旅程。我的问题如下 -

@PreAuthorize("hasAnyRole('Role_1') or hasAnyRole('Role_2')")
    @RequestMapping(value = URL_MAPPING, method = {RequestMethod.GET, RequestMethod.POST })
    public ModelAndView methodName(HttpSession session,
            ModelAndView modelAndView) {


        //here i need to know which role it has passed to execute the method.
        if (role1 executed) {
            sessionInformation.updateJourneyInfo(updateforrole1);
        }else{
            sessionInformation.updateJourneyInfo(updateforrole2);
        }

任何人都可以帮助解决这个问题。在此先感谢。

【问题讨论】:

    标签: java spring-mvc spring-security roles


    【解决方案1】:

    HttpServletRequest 请求添加到方法的参数列表中,然后使用

    request.isInRole("ROLE_Role_1")
    

    【讨论】:

    • 同意这是最简单的解决方案,如果这仍然没有意义,为什么不简单地将身份验证对象注入方法参数中
    猜你喜欢
    • 2014-08-03
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2011-03-06
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多