java - Keycloak - antMatcher 与请求的路径 Java 不匹配

标签 java spring rest spring-mvc keycloak

我使用正确的凭据登录为“CHILD”,因此它与“USER”角色不同。当我将带有 token 的 get 请求发送到 http://localhost:8081/iam/accounts/users 时,它不允许我,那没关系。

enter image description here

但是当我向 http://localhost:8081/iam/accounts/users/ 发送请求时(请注意,斜杠已添加到网址末尾),我可以访问此链接,并向我提供返回正文。但我不应该被授权接收它。

enter image description here

我的配置方法。它看起来有效,我只是不明白为什么“/iam/accounts/users*”也不匹配末尾添加斜杠的网址?!

@Override
protected void configure(HttpSecurity http) throws Exception {

    super.configure(http);
    ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = http.cors() //
            .and() //
            .csrf().disable() //
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) //
            .and() //
            .authorizeRequests();

    expressionInterceptUrlRegistry = expressionInterceptUrlRegistry.antMatchers("/iam/accounts/users*").hasRole("USER");
    expressionInterceptUrlRegistry = expressionInterceptUrlRegistry.antMatchers("/iam/accounts/childs*").hasRole("CHILD");

    expressionInterceptUrlRegistry.anyRequest().permitAll();
}

休息 Controller

@RestController
@RequestMapping(path = "/iam/accounts", produces = MediaType.APPLICATION_JSON_VALUE)
@Validated
public class AccountController {

@GetMapping(path = "/users")
public String promoters() {
    return "Logged as user";
}

@GetMapping(path = "/childs")
public String supervisors() {
    return "Logged as child";
}
}

如果我添加 2 行而不是 1 行,我就可以解决这个问题,但是如果星号 * 意味着后面可以有任意数量的字符,那么为什么 antMatcher 不这样做呢?

expressionInterceptUrlRegistry = expressionInterceptUrlRegistry.antMatchers("/iam/accounts/users").hasRole("USER");
expressionInterceptUrlRegistry = expressionInterceptUrlRegistry.antMatchers("/iam/accounts/users/*").hasRole("USER");

最佳答案

按预期工作。 * 匹配同一目录。/是路径分隔符,因此不算同一个目录。 user* 将匹配 user, users,但不匹配 user/。

关于java - Keycloak - antMatcher 与请求的路径 Java 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795336/

相关文章:

java - 如何在java和node中编写精确的加密代码?

java - 2 个非嵌套不同循环方法的大 O 表示法的时间复杂度

spring - 无法使用 Spring Cloud 连接到 Hystrix Dashboard 的 Command Metric Stream

rest - 如何通过 REST api 删除带有二进制 rowkey 的 HBase 单元?

java - 在 JBoss 上启动 Spring webapp 的问题

java - org.springframework.boot.SpringApplication无法解析

java - 如何防止 TestNG 跳过测试?

pagination - 在一个安静的 url 上编码分页信息的标准方法得到了吗?

arrays - WordPress JSON API : Retrieve multiple posts by ids

java - 网络在主线程异常