복붙노트

[SPRING] 보안 Spring 부트 애플리케이션의 정적 컨텐츠 액세스

SPRING

보안 Spring 부트 애플리케이션의 정적 컨텐츠 액세스

나는 템플릿 / src / main / resources / templates와 / src / main / resources / static의 정적 컨텐츠를 가진 독립형 Spring Boot 애플리케이션을 가지고있다. 인증 전에 정적 컨텐츠에 액세스 할 수 있기를 바랍니다. 그러면 CSS가 로그인 페이지에도로드됩니다. 이제 인증 후에 만로드됩니다. 내 보안 구성은 다음과 같습니다.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = Logger.getLogger(SecurityConfig.class);

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        try {
            auth.inMemoryAuthentication()
            ...
        } catch (Exception e) {
            logger.error(e);
        }
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .formLogin()
                .defaultSuccessUrl("/projects", true)
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
                .permitAll()
                .and()
            .authorizeRequests()
                .antMatchers("/static/**").permitAll()
                .anyRequest().authenticated();
    }

}

해결법

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

    1.classpath : / static의 정적 내용은 응용 프로그램의 보안 여부와 관계없이 응용 프로그램 루트 (/ *)에서 제공되므로 루트 아래 특정 경로와 일치해야합니다. Spring Boot는 기본적으로 모든 액세스가 / js / **, / css / **, / images / ** (자세한 내용은 SpringBootWebSecurityConfiguration 참조)를 허용하지만, 나머지 코드는 볼 수 없습니다. .

    classpath : / static의 정적 내용은 응용 프로그램의 보안 여부와 관계없이 응용 프로그램 루트 (/ *)에서 제공되므로 루트 아래 특정 경로와 일치해야합니다. Spring Boot는 기본적으로 모든 액세스가 / js / **, / css / **, / images / ** (자세한 내용은 SpringBootWebSecurityConfiguration 참조)를 허용하지만, 나머지 코드는 볼 수 없습니다. .

  2. from https://stackoverflow.com/questions/22823863/access-static-content-in-secured-spring-boot-application by cc-by-sa and MIT license