如何从另一个模块导入安全配置类

时间:2019-08-22 18:28:55

标签: spring-boot spring-security multi-module

我想在单独的模块中开发我的安全配置,并将其添加为主应用程序中的依赖项。因此我以父代的身份编写了一个非常简单的测试项目,其中包含2个子模块:安全模块 app

安全模块只有一个配置类,如下:

@Configuration
@EnableWebSecurity
public class Security extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().
                withUser("h").
                password("1").
                roles("admin");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().disable();
        http.csrf().disable();
        http.authorizeRequests()
                .antMatchers("/", "/login").permitAll()
                .antMatchers("/static/**").permitAll()
                .anyRequest().authenticated();
    }
}

app 模块中,我有一个 SecurityConfig 类,如下所示:

@Configuration
@Import({
        Security.class
})
public class SecurityConfig{
}

资源目录如下:

enter image description here

问题是在 Security 类中配置的 HttpSecurity 对象没有完全注册。说完全原因,如果我在 Security 类中添加anyRequest().pertmitAll(),那么一切都很好,但是现在对于任何请求,我都会收到403错误,即使当我显式调用资源时,例如 localhost:8080 /dash.html

我还尝试按照 SecurityConfig 中的说明扩展安全性

@Configuration
public class SecurityConfig extends Security{
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

但没有任何改变。

如何解决此问题?

0 个答案:

没有答案