【问题标题】:antMatchers Spring Security pattern with changeable URL user ID具有可变 URL 用户 ID 的 antMatchers Spring Security 模式
【发布时间】:2015-02-23 07:46:58
【问题描述】:

我一直在寻找答案,但找不到任何有效的方法

在我的休息服务中,我在 /account/{id}/download 下保留了一些功能,我想在 SecurityConfig java 文件中设置访问角色,只有 ROLE_TOKENSAVED 用户可以访问此 url

当 {id} 可变时,模式应该是什么样子?

我尝试了一些正则表达式模式,但没有达到我想要的效果,以下是我的一些尝试:

1. antMatchers("account/**/download").access(somerolehere)
2. antMatchers("account/\\d/download").access(somerolehere)
3. antMatchers("account/[\\d]/download").access(somerolehere)

提前感谢您的回答:)

编辑:

    @Override
    protected void configure(HttpSecurity http) throws Exception {            
        http.authorizeRequests()
                .antMatchers("/admin**").access("hasRole('ROLE_ADMIN')")
                .antMatchers("/account*//**").access("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
                .antMatchers("/account/\\d+/download").access("hasRole('ROLE_TOKENSAVED')")
                .antMatchers("/user**").permitAll()
                //othercode...
    }

【问题讨论】:

    标签: java regex spring security spring-security


    【解决方案1】:

    这对我有用:

    antMatchers("/account/{\\d+}/download").access("hasAnyAuthority('ROLE_TOKENSAVED')")
    

    注意表示 ID 的路径变量周围的花括号。

    【讨论】:

    • 是的,它有效,但只有当我评论 .antMatchers("/account*//**").access("hasRole('ROLE_USER') 或 hasRole('ROLE_ADMIN')") 模式时,如何调和这两种模式?
    • 因为那个匹配器更通用。匹配器必须从最具体到最不具体排序。所以把 /account/{\\d+}/download 匹配器放在它前面。
    • 但我为什么要在模式中添加 {} 括号?因为控制器使用:{id} 模式从中获取 PathVariable?
    • @azalut 当您查看AntPathMatcher JavaDoc 时,它说:“URI 模板变量通过大括号('{' 和'}')表示”。所以这只是 Ant 匹配器的特定语法。
    • 请注意,\d 是一个数值,我在路径中有字符串参数。所以,我使用了\w
    【解决方案2】:

    尽管 Bohuslav 的建议有效,但它并不完整。根据 AntPathMarcher 的文档: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

    您需要使用正则表达式指定路径变量:

    {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"

    如果你不这样做,你可能会暴露其他路线。例如:

        .authorizeRequests()
            .antMatchers(HttpMethod.GET, "/users/{^[\\d]$}").authenticated()
            .antMatchers("/users/**").hasAuthority("admin")
    

    以及 UserController 上的这些方法:

    @ResponseBody
    @RequestMapping(value = "/users/{userId}", method = RequestMethod.GET)
    public User getUser(@PathVariable("userId") Object id) {
        return userService.getUserById(userId);
    }
    
    @ResponseBody
    @RequestMapping(value = "/users/roles", method = RequestMethod.GET)
    public List<String> getAllRoles() {
        return userService.getAllRoles();
    }
    

    由于您没有指定路径变量userId,用户将能够在“/users/roles”上执行 GET 请求而无需管理员权限。即使需要管理员授权,也将公开其他期货路线,如“/users/test”。为了防止这种情况:

    antMatchers("/account/{accountId:\\d+}/download")
           .access("hasAnyAuthority('ROLE_TOKENSAVED')")
    

    如果您的路径变量的名称是“accountId”

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 2013-12-30
      • 2018-07-17
      • 1970-01-01
      • 2018-01-31
      • 2019-09-27
      • 1970-01-01
      • 2015-05-11
      • 2013-05-01
      相关资源
      最近更新 更多