spring - 如何区分具有路径变量的 antMatchers 与上下文路径的其余部分
问题描述
我有两条路:
/api/posts/{postId}
/api/posts/myPosts
我想允许第一条路径的所有内容并使用角色 USER 保护第二条路径。
我尝试了以下模式,但是当我添加第一个模式时,第二个模式停止工作(即使他没有 USER 角色,用户也可以 GET myPosts )。我做错了什么?
.antMatchers(HttpMethod.GET, "/api/posts/{postId}").permitAll()
.antMatchers(HttpMethod.GET, "/api/posts/myPosts").hasRole("USER")
解决方案
问题在于您的规则顺序。颠倒顺序将起作用。
.antMatchers(HttpMethod.GET, "/api/posts/myPosts").hasRole("USER")
.antMatchers(HttpMethod.GET, "/api/posts/{postId}").permitAll()
推荐阅读
- android - 在将图像从 url 加载到 ImageView 之前会出现一个白屏
- html - 如何根据响应行为添加三个 div?
- java - kotlin 1.3 协程使用导致 ClassNotFoundException: kotlinx.coroutines.BuildersKt
- php - 通过作曲家下载后,我应该如何使用位于供应商文件夹的类?
- pyspark - 通过仅从两列中获取唯一值来对 pyspark 数据框的列进行分组
- ansible - 使用 csv 文件的输入从 jinja 模板创建文本文件
- python-3.x - 如何将网页 html 中提取的文本添加到计算机上的文本文件中?
- css - 动画 feComposite SVG 过滤器元素
- c# - 通过具有 V1 运行时的 Azure Function 将 MP3 从 blob 存储流式传输到客户端
- c# - 不完整的 Microsoft.Graph.User 配置文件数据