我有一個基於Spring構建的Web服務。我目前正在驗證使用Spring Security的如下:Spring Security的授權和身份驗證
@Configuration
@EnableGlobalMethodSecurity(securedEnabled=true)
@EnableWebSecurity
public class ServerSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private Properties properties;
private static final String ALL_URI = "/v1/**";
private static final String HEALTH_URI = "/v1/healthCheck";
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(getFilter(), BasicAuthenticationFilter.class);
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers(HEALTH_URI).permitAll()
.anyRequest().authenticated();
http.csrf().disable();
}
private AuthenticationFilter getFilter() {
return new AuthenticationFilter(properties.getKey());
}
}
我AuthenticationFilter
類擴展AbstractAuthenticationProcessingFilter
和執行實際的驗證。如果我想將授權添加到我的安全性中,我是否只需在AuthenticationFilter的attemptAuthentication
方法中進行這些檢查?還是有更好的方法來做到這一點?我理解的方式是授權和認證應該獨立完成。您首先進行身份驗證,然後驗證權限。所以,我會假設在Spring Security中會有更好的方法來進行授權,而不是將其添加到attemptAuthentication
方法中。