В примере проекта Spring со следующей конфигурацией безопасности:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
MySimpleEncoder passwordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("admin").password(passwordEncoder.encode("admino")).roles("USER","ADMIN")
.and()
.withUser("user").password(passwordEncoder.encode("123456")).roles("USER");
}
@Bean
public MySimpleEncoder passwordEncoder() {
return new MySimpleEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").hasAnyRole("ADMIN", "USER")
.and().httpBasic()
.and().csrf().disable();
}
}
Я заметил, что вам нужны эти две зависимости:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
но также это, которое необходимо во время выполнения:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<scope>runtime</scope>
</dependency>
Почему последняя зависимость не извлекается автоматически с областью выполнения одной из двух других зависимостей?
Почему мы должны явно объявить это?
Я использую Spring 5