首页 > 解决方案 > 有没有办法混合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。有没有办法做到这一点?

标签: javaspringscalaspring-securityspring-annotations

解决方案


您可以使用@PreAuthorize。它会解决你的问题。

@PreAuthorize("hasRole('" + <Your Role> + "')")

推荐阅读