복붙노트

[SPRING] Spring 부트에서 "기본 보안 암호 사용"제거

SPRING

Spring 부트에서 "기본 보안 암호 사용"제거

Spring Boot에서 응용 프로그램에 하나의 사용자 지정 보안 구성을 추가했지만 "기본 보안 암호 사용"에 대한 메시지는 여전히 LOG 파일에 있습니다.

그것을 제거하는 것이 있습니까? 이 기본 암호가 필요하지 않습니다. 스프링 부트가 내 보안 정책을 인식하지 못하는 것 같습니다.

@Configuration
@EnableWebSecurity
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String uri = "/custom/*";

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().httpStrictTransportSecurity().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Authorize sub-folders permissions
        http.antMatcher(uri).authorizeRequests().anyRequest().permitAll();
    }
}

해결법

  1. ==============================

    1.SecurityAutoConfiguration 클래스를 제외하는 방법에 대한 해결책을 찾았습니다.

    SecurityAutoConfiguration 클래스를 제외하는 방법에 대한 해결책을 찾았습니다.

    예:

    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
    public class ReportApplication {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    
  2. ==============================

    2.application.properties에서 다음을 추가하면 나를 위해 일했습니다.

    application.properties에서 다음을 추가하면 나를 위해 일했습니다.

    security.basic.enabled=false
    

    응용 프로그램을 다시 시작하고 콘솔을 체크인하십시오.

  3. ==============================

    3.작동하지만 현재 솔루션은 약간의 과장된 의견이 있습니다. 그래서 여기에 최신 스프링 부트 (1.4.3)를 사용하여 저에게 도움이되는 대안이 있습니다.

    작동하지만 현재 솔루션은 약간의 과장된 의견이 있습니다. 그래서 여기에 최신 스프링 부트 (1.4.3)를 사용하여 저에게 도움이되는 대안이 있습니다.

    기본 보안 암호는 Spring Boot의 AuthenticationManagerConfiguration 클래스에서 구성됩니다. 이 클래스에는 AuthenticationManager Bean이 이미 정의되어있는 경우로드되지 못하도록하는 조건부 주석이 있습니다.

    다음 코드는 현재 AuthenticationManager를 bean으로 정의하기 때문에 AuthenticationManagerConfiguration 내부에서 코드가 실행되지 않도록합니다.

    @Configuration
    @EnableWebSecurity
    public class MyCustomSecurityConfig extends WebSecurityConfigurerAdapter{
    
    [...]
    
    @Override
    protected void configure(AuthenticationManagerBuilder authManager) throws Exception {
        // This is the code you usually have to configure your authentication manager.
        // This configuration will be used by authenticationManagerBean() below.
    }
    
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        // ALTHOUGH THIS SEEMS LIKE USELESS CODE,
        // IT'S REQUIRED TO PREVENT SPRING BOOT AUTO-CONFIGURATION
        return super.authenticationManagerBean();
    }
    
    }
    
  4. ==============================

    4.Spring Boot 2.0.4를 사용하여 동일한 문제를 발견했습니다.

    Spring Boot 2.0.4를 사용하여 동일한 문제를 발견했습니다.

    SecurityAutoConfiguration.class를 제외하면 응용 프로그램이 파괴되었습니다.

    이제 @SpringBootApplication (exclude = {UserDetailsServiceAutoConfiguration.class})을 사용하고 있습니다.

    @EnableResourceServer 및 JWT와 잘 작동합니다. :)

  5. ==============================

    5.검색 : http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

    검색 : http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

    코드를 보는 AuthenticationManagerConfiguration.java에서, 나는 아래를 본다. 또한 Javadoc에 따라 인증 관리자가 제공되지 않으면 인 메모리 구성이 대체됩니다. 이전에 인증 관리자를 삽입하려고 했더라도 더 이상 메모리 내 인증을 사용하지 않아이 클래스가 화면에 보이지 않기 때문에 작동합니다.

    @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            if (auth.isConfigured()) {
                return;
            }
            User user = this.securityProperties.getUser();
            if (user.isDefaultPassword()) {
                logger.info("\n\nUsing default security password: " + user.getPassword()
                        + "\n");
            }
            Set<String> roles = new LinkedHashSet<String>(user.getRole());
            withUser(user.getName()).password(user.getPassword()).roles(
                    roles.toArray(new String[roles.size()]));
            setField(auth, "defaultUserDetailsService", getUserDetailsService());
            super.configure(auth);
        }
    

    기본 인 메모리 인증을 사용하는 경우 org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration에 대한 로거 구성을 사용자 정의하고이 메시지를 제거하십시오.

  6. ==============================

    6.스프링 부트를 사용할 때 응용 프로그램 클래스에서 SecurityAutoConfiguration.class를 제외하고 아래 에서처럼 보안을 정확히 구성하는 위치를 제외해야합니다.

    스프링 부트를 사용할 때 응용 프로그램 클래스에서 SecurityAutoConfiguration.class를 제외하고 아래 에서처럼 보안을 정확히 구성하는 위치를 제외해야합니다.

    그런 다음에는 기본 보안 암호를 피할 수 있습니다.

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
    
    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
    @EnableJpaRepositories
    @EnableResourceServer
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.Configuration;
    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;
    
        @Configuration
        @EnableWebSecurity
        @EnableAutoConfiguration(exclude = { 
                org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class 
            })
        public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity httpSecurity) throws Exception {
                httpSecurity.authorizeRequests().anyRequest().authenticated();
                httpSecurity.headers().cacheControl();
            }
        }
    
  7. ==============================

    7.org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration에 대한 문서를 확인하십시오. 자동 구성이 중단 될 때 조건이 있습니다.

    org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration에 대한 문서를 확인하십시오. 자동 구성이 중단 될 때 조건이 있습니다.

    내 경우에는 사용자 정의 AuthenticationProvider를 bean으로 정의하는 것을 잊었다.

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(getAuthenticationProvider());
        }
    
        @Bean
        AuthenticationProvider getAuthenticationProvider() {
            return new CustomAuthenticationProvider(adminService, onlyCorporateEmail);
        }
    }
    
  8. ==============================

    8.@SpringBootApplication 주석을 사용하여 SecurityAutoConfiguration을 제외 할 때 작동하지 않지만 @EnableAutoConfiguration에서 제외했을 때 작동했습니다.

    @SpringBootApplication 주석을 사용하여 SecurityAutoConfiguration을 제외 할 때 작동하지 않지만 @EnableAutoConfiguration에서 제외했을 때 작동했습니다.

    @EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
    
  9. ==============================

    9.Spring Boot version> = 2.0을 사용한다면 설정에서이 bean을 설정해보십시오 :

    Spring Boot version> = 2.0을 사용한다면 설정에서이 bean을 설정해보십시오 :

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange().anyExchange().permitAll();
        return http.build();
    }
    

    참조 : https://stackoverflow.com/a/47292134/1195507

  10. ==============================

    10.configs를 별도의 패키지로 선언하는 경우, 다음과 같이 구성 요소 검사를 추가해야합니다.

    configs를 별도의 패키지로 선언하는 경우, 다음과 같이 구성 요소 검사를 추가해야합니다.

    @SpringBootApplication
    @ComponentScan("com.mycompany.MY_OTHER_PACKAGE.account.config")
    
        public class MyApplication {
    
            public static void main(String[] args) {
                SpringApplication.run(MyApplication.class, args);
            }
    
    
    
        }
    

    다음과 같이 config 클래스에 @component annotation을 추가해야 할 수도 있습니다.

      @Component
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
    
    .....
    
  11. ==============================

    11.webflux로 봄 부팅 2에서 ReactiveAuthenticationManager를 정의해야합니다.

    webflux로 봄 부팅 2에서 ReactiveAuthenticationManager를 정의해야합니다.

  12. from https://stackoverflow.com/questions/30761253/remove-using-default-security-password-on-spring-boot by cc-by-sa and MIT license