我正在尋找一種方法來保護Spring Boot的@Secured
註釋的方法。對於大約10-15個用戶,我不想連接到數據庫,並從那裏獲取用戶及其權限/角色,而是將它們本地存儲在特定於配置文件的application.yml
文件中。 Spring Boot中有沒有一個概念支持這個想法?所有我能找到到目前爲止與基本安全驅動器('org.springframework.boot:spring-boot-starter-security'
)的作品,看起來像這樣:在application.yml中定義基本安全用戶,密碼和角色
security:
basic:
enabled: true
user:
name: admin
password: admin
role: EXAMPLE
不過,我仍然能夠訪問與@RolesAllowed("READ")
註釋的方法,即使我會假設用戶管理不應該有權訪問所述方法。我的SecurityConfiguration看起來像這樣:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
@Profile("secure")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.fullyAuthenticated()
.and()
.httpBasic();
http.sessionManagement()
.sessionFixation()
.newSession();
http.csrf().disable();
http.headers().frameOptions().disable();
}
}
最終這可能是一個不同的問題,但也許這對我自己的理解很重要。
我想知道如何在我的application.yml
中指定具有不同密碼和不同角色的多個用戶並註釋方法以確保只有授權用戶才能訪問這些方法。
您無法在您的公司中指定多個用戶nfiguration。你需要爲此自行構建一些東西。 –
@ M.Deinum謝謝,我想出了一個非常簡單的方法來實現它。 – user3105453