繁体   English   中英

Spring Boot - Spring 安全 @ComponentScan 或 @Import

[英]Spring Boot - Spring security @ComponentScan or @Import

我使用 Spring Initializr 生成了一个 Spring Boot Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并将其打包为可执行 JAR 文件。

使用的技术:

Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat Embed 8.5.6、Maven 3、Java 8

我有这个安全课

com.tdk.config

/**
 * @author  nunito
 * @version 1.0
 * @since  4 mar. 2017
 */
@Configuration
@EnableWebSecurity
@PropertySource("classpath:/com/tdk/config/app-${APP-KEY}.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    protected String loginPage = "/tdk/login";

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .formLogin()
                .loginPage(getLoginPage() )
                .permitAll()
                .and()
            .authorizeRequests()
                .antMatchers("/mockup/**").permitAll()
                .antMatchers("/welcome/**").authenticated()
                .and()
            .logout()
                .permitAll()
                .logoutSuccessUrl("/index.html");


    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .passwordEncoder(new StandardPasswordEncoder())
                .withUser("nunito").password("08c461ad70fce6c74e12745931085508ccb2090f2eae3707f6b62089c634ddd2636f380f40109dfb").roles("ADMIN").and()
                .withUser("nunito").password("4cfbf05e4493d17125c547fdba494033d7aceee9310f253f3e96c4f928333d2436d669d63a84fe4f").roles("ADMIN");
    }

    public String getLoginPage() {
        return loginPage;
    }

    public void setLoginPage(String loginPage) {
        this.loginPage = loginPage;
    }

使用这个配置文件

@SpringBootApplication
@ComponentScan(basePackages = "com.tdk.config")
@EnableAutoConfiguration
public class TdkCloudApplication {

    public static void main(String[] args) {
        SpringApplication.run(TdkCloudApplication.class, args);
    }
}

我在 URL 中访问的任何地方都有一个 404

但是这个配置一切正常

@SpringBootApplication
@EnableAutoConfiguration
@Import({SecurityConfig.class})
public class TdkCloudApplication {

    public static void main(String[] args) {
        SpringApplication.run(TdkCloudApplication.class, args);
    }
}

我想知道区别,因为对我来说是两种完全相同的想法

这是对@Import@ComponentScan的解释,这不是您问题的答案(因为我不知道为什么它不适用于 ComponentScan),更像是对它的提示。

@Import用于导入其他配置,所以如果一个类用@Configuration注解并且在那里定义了一些bean,它们将被导入到应用程序上下文中,例如

@Configuration
public class config{
    @Bean
    public ClassA a(){
        return new ClassA();
    }
}

@Import({config.Class}) // import Bean for ClassA

@ComponentScan扫描所有用@Component@Service@Repository注释的@Component ,并将一对一的 bean 映射到每个类。 例如

@Component
public class ClassB {}

@ComponentScan // import Bean ClassB

Spring 4.2 版本之后, @ComponentScan ComponentScan 也可以将@Configuration扫描为一个组件。 所以在你的情况下, SecurityConfig 也应该作为一个组件而不是作为一个配置导入到上下文中。

唯一不太明白的是@Import是如何触发SecurityConfig 中代码的执行的,如果有人知道,请发表评论。

这么晚才回复很抱歉。 但它可能会帮助某人。 问题中不存在“TdkCloudApplication”包。 我假设它与“com.tdk.config”位于不同的包中。 如果是这种情况,那么在@ComponentScan() 注释中,您必须给出TdkCloudApplication 的包和“SecurityConfig”的包

像这样。 @ComponentScan(basePackages = {"com.tdk.config", "TdkCloudApplication 的包名"})。

这是因为@SpringBootApplication。 默认情况下,它将扫描该包及其子包中的 bean。 但是如果我们用@ComponentScan 注释类,那么我们必须提供我们需要的所有包,以便 spring 将检查所有这些区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM