java - 有没有办法混合spring-security java配置和@secured注解?
问题描述
我为网络应用程序设置了弹簧安全性。我想制定全局安全规则(在 GET 上允许所有经过身份验证的用户,并且只允许在其他 http 方法上使用角色 ADMIN )并将自定义规则添加到带有@Secured
注释的端点。
当我@Secured
像只有角色 SUPER_USER 的用户那样配置和注释时,无法访问该端点
override def configure(http: HttpSecurity): Unit = {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, ALL)
.permitAll()
.and()
.authorizeRequests()
.antMatchers(ALL).hasRole("ADMIN")
}
@DeleteMapping(value = Array("/{id}"))
@Secured(Array("SUPER_USER"))
def delete(@PathVariable("id") id: String): Unit = {...}
我希望有全局安全规则和自定义规则@Secured
。有没有办法做到这一点?
解决方案
您可以使用@PreAuthorize。它会解决你的问题。
@PreAuthorize("hasRole('" + <Your Role> + "')")
推荐阅读
- linux - 单行 bash 命令作为 root 可以正常工作,但不能作为 sudo [语法问题]
- selenium - selenium chromedriver python - 默认下载文件夹
- spring-boot - Expo & SpringBoot 分段上传问题
- javascript - startTypeError:daemon.getblockcount 不是函数
- python - TikTok 喜欢用 BeautifulSoup 和请求进行抓取。Python
- javascript - React JSX 动态改变组件的状态。最佳实践和原因
- apache-spark - 在服务监视器中重命名指标
- c# - 使用具有单次和多次返回的工厂模式
- python - 使用来自另一个函数 python3 的值
- javascript - 不预加载视频,但仍显示“缩略图”