加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通过@Profile启用WebSecurityConfigurer不起作用

发布时间:2020-12-15 05:21:25 所属栏目:Java 来源:网络整理
导读:我认为,我有一个非常简单和基本的设置,用于本地运行带有一些身份验证的 Spring Boot webapp. 我希望当我通过Spring Boot运行此应用程序时,我指定本地配置文件时,我的自定义安全设置将覆盖默认行为. mvn -Dspring.profiles.active =“local”spring-boot:run
我认为,我有一个非常简单和基本的设置,用于本地运行带有一些身份验证的 Spring Boot webapp.

我希望当我通过Spring Boot运行此应用程序时,我指定本地配置文件时,我的自定义安全设置将覆盖默认行为.

mvn -Dspring.profiles.active =“local”spring-boot:run

也许我正在指定profiles.active错误,但是当应用程序运行时,它仍会吐出生成的密码以供使用,并且似乎不允许在没有所述身份验证的情况下访问/ login路径.

我也没有看到/ env下的活动配置文件,这可能有点说.

我有一个WebSecurityConfigurer被覆盖,如下所示:

@Configuration
@EnableWebSecurity
@Profile("local")
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin().permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN","USER")
        .and().withUser("user").password("user").roles("USER");

    }
}

我的主要@Configuration类是您标准的Spring Java风格的基本配置:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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

解决方法

第二次尝试提供更好的安全设置控制.什么是控制安全自动配置的高级选项:

>完全永久地关闭安全性:

>从类路径中删除Spring Security
>或排除安全性自动配置 – @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)

>通过设置security.basic.enabled = false来关闭默认的基本身份验证安全性

如果您完全控制安全设置,安全自动配置和弹簧配置文件的使用方式,则可以非常轻松地控制不同的安全设置.

@Configuration
@ComponentScan
public class Application {
    public static void main(String[] args) throws Throwable {
        SpringApplication.run(Application.class,args);
    }
}

@Configuration
public class WebSecurityConfig {

    @Configuration
    @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
    @ConditionalOnExpression("!${my.security.enabled:false}")
    protected static class DefaultWebSecurityConfig {
    }

    @Configuration
    @EnableAutoConfiguration
    @EnableWebMvcSecurity
    @Profile("local")
    @ConditionalOnExpression("${my.security.enabled:false}")
    protected static class LocalWebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/","/home").permitAll()
                    .anyRequest().authenticated();
            http
                .formLogin().loginPage("/login").permitAll().and()
                .logout().permitAll();
        }

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

}

在上面的类中,我基本上从Application类顺序中删除了@EnableAutoConfiguration以有条件地使用它.创建了两个配置类,DefaultWebSecurityConfig和LocalWebSecurityConfig,它们由my.security.enabled标志使用Boot @ConditionalOnExpression选择.

如果我的安全性未启用,则第一个配置只会排除SecurityAutoConfiguration.第二个启用安全性并使用本地配置文件.通过使用不同的配置文件创建另一个配置,您可以控制不同配置文件发生的情况.然后,您可以选择是否启用安全性以及使用哪个配置文件:

#java -jar build/libs/gs-securing-web-0.1.0.jar
#java -jar build/libs/gs-securing-web-0.1.0.jar --spring.profiles.active=local --my.security.enabled=true

如果您可以选择使用application.yml,则每个配置文件可以自动应用不同的设置,但仍定义默认值.如果您只想禁用默认安全自动配置启用的默认基本身份验证,这将是一件好事.

security:
    basic:
        enabled: false
---
spring:
    profiles: local
security:
    basic:
        enabled: true
---

可能有一百万种不同的方法来执行这些操作,并且始终根据具体情况最适合当前用例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读