【发布时间】:2019-02-15 15:35:22
【问题描述】:
在具有以下安全配置的示例 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
【问题讨论】:
-
因为依赖不应该和其他的紧密耦合
-
也许非 Web 应用程序也有安全性,如果你拉所有这些依赖项,你可以在不同的问题中运行
-
在包含安全模块时使用 Spring Initializr,您会得到 mvnrepository.com/artifact/org.springframework.boot/…。也许试试这个。
-
@J.Kennsy 我实际上想明确声明我直接使用的依赖项,以便 dependency:analyze 很高兴。
标签: java spring maven spring-security