[英]Unable to understand the behavior of Spring security
我正在使用帶有啟動器依賴項的spring boot 2.1.4。 我想為執行器和我的應用程序配置單獨的身份驗證和授權機制。 我閱讀了Multiple HttpSecurity並配置了WebSecurityAdapter,如下所示:
@Configuration
public class ProvisioningServiceSecurityConfiguration {
@Value("${actuator.user.name}")
private String actuatorUserName;
@Value("${actuator.password}")
private String actuatorPassword;
@Value("${actuator.role}")
private String actuatorRole;
@Bean
public UserDetailsService userDetailsService() throws Exception {
// ensure the passwords are encoded properly
UserBuilder users = User.withDefaultPasswordEncoder();
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(users.username("user").password("password").roles("ADMIN").build());
manager.createUser(
users.username(actuatorUserName).password(actuatorPassword).roles(actuatorRole).build());
return manager;
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/locations/**")
.antMatcher("/organizations/**")
.antMatcher("/productTypes/**")
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}
@Configuration
@Order(2)
public static class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/manage/**")
.authorizeRequests()
.anyRequest().hasRole("ACTUATOR_ADMIN")
.and()
.httpBasic();
}
}
/*@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}*/
}
注意:我已暫時禁用表單登錄
當我執行curl請求時
curl -XGET http://localhost:9797/provisioningService/organizations/all
我能夠看到輸出。 好像彈簧安全性從未存在過。 啟用表單登錄后,將顯示spring登錄屏幕。 我觀察到的另一個行為是,如果將/ locations的用戶名和密碼與執行器的用戶名和密碼互換,我仍然會收到有效的響應。
我知道表單登錄更多是一種后備方式,但是我想禁用表單登錄(可能我們可能會改用cas),並且僅基於Spring Security httpBasic使用身份驗證和授權。 我無法理解我正在犯的錯誤。
我的要求終於是:
1)僅當用戶名密碼為“ user”和“ password”時,才可以訪問對/ organizations或/ locations等的請求
2)僅當用戶名和密碼以及角色與執行器用戶名和密碼相匹配時,才可以訪問作為執行器api的對/ manage的請求。
3)任何其他API都可以被允許所有/表單登錄
我該如何實現這一目標?
1)Spring Security具有通過權限(在身份驗證之后)進行過濾來控制訪問的功能,但是沒有功能可以根據登錄所需的信息進行過濾。 您需要業務邏輯來驗證您是否嘗試在登錄期間使用相應的ID和密碼登錄。
2)如上所述,未提供具有ID和密碼的訪問控制。 我建議僅為您請求的兩個帳戶創建“單位”。
3) .antMatcher("/form").permitAll()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.