簡體   English   中英

有什么辦法可以混合使用spring-security java配置和@secured注釋?

[英]Is there any way to mix spring-security java configuration and @secured annotation?

我設置了Web應用程序的Spring Security。 我想制定全局安全規則(允許所有經過GET身份驗證的用戶,並且僅允許其他HTTP方法上具有ADMIN角色),並使用@Secured注釋將自定義規則添加到端點。

當我像只具有角色SUPER_USER的用戶那樣進行配置和@Secured注釋時,無法訪問該端點

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> + "')")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM