【问题标题】:antMatchers for a list of pathsantMatchers 用于路径列表
【发布时间】:2019-05-11 20:20:07
【问题描述】:

我有一个路径列表,我想提供给antMatchers()。但由于这个列表是动态的,我不能提供逗号分隔的路径。我目前正在遍历列表并将每个路径添加到antMatcher()。有没有更好的方法可以将 antMatcher 应用于路径列表?

当前代码sn-p:

   http.authorizeRequests()
    .antMatchers("/signup","/about")
    .hasRole("ADMIN")
    .anyRequest().authenticated();

我有一个路径列表。例如:

List<String> pathList = Arrays.asList(new String[]{"/signup","/about"});

我想应用 antMatchers 类似的东西:

http.authorizeRequests()
        .antMatchers(pathList)
        .hasRole("ADMIN")
        .anyRequest().authenticated();

参考:https://spring.io/blog/2013/07/11/spring-security-java-config-preview-readability/ spring security http antMatcher with multiple paths

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    antMatchers 接受字符串数组。

    public C antMatchers(String... antPatterns) {
     ....
    }
    

    可以将列表转成字符串数组,然后传给antMatchers

    String[] pathArray = new String[]{"/signup","/about"};
    http.authorizeRequests()
        .antMatchers(pathArray)
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      相关资源
      最近更新 更多