【问题标题】:why you need to explicitly import spring-security-web dependency?为什么需要显式导入 spring-security-web 依赖项?
【发布时间】: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


【解决方案1】:

Spring Security 是 Spring 的一个不同模块。 在我们使用 Spring CoreSpring Web/ 的地方并不总是使用此模块Spring MVC 或任何其他模块。 这是一个独立的模块,我们可以随时添加和使用。

这就是 Spring 开发人员使 Spring Security 模块独立的原因。

我们不应该让我们的应用程序充满未使用的东西。 我们应该在应用程序需要时使用它。

【讨论】:

    【解决方案2】:

    这个框架本身就非常庞大,它甚至有自己的框架,叫做Spring-Boot。将功能部分分成称为Spring Projects 的东西是非常有意义的。和社区项目一样的官方有很多:很少的例子:

    • 弹簧数据
    • Spring 安全性
    • 春季批次

    注意Spring网页笔记:

    Spring 在设计上是模块化的

    学习所有 Spring 项目可能会变得繁琐且不堪重负。就像您将应用程序划分为包、项目一样——同样的方法使用 Spring 独立于其他部分来控制其所有部分。一些 Spring 项目仍然太大而无法划分为子项目 - Spring Security 就是一个很好的例子。

    【讨论】:

    • 所以您认为我们可以强制声明运行时依赖项吗?这可能是一个小缺陷吗?
    • 一个运行时依赖被复制到您部署的应用程序中,在单元测试执行和运行时执行时可用,但在编译期间不可用。我认为runtime 没有问题。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2011-01-16
    • 2017-04-09
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多