我使用正确的凭据登录为“CHILD”,因此它与“USER”角色不同。当我将带有 token 的 get 请求发送到 http://localhost:8081/iam/accounts/users 时,它不允许我,那没关系。
但是当我向 http://localhost:8081/iam/accounts/users/ 发送请求时(请注意,斜杠已添加到网址末尾),我可以访问此链接,并向我提供返回正文。但我不应该被授权接收它。
我的配置方法。它看起来有效,我只是不明白为什么“/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/