복붙노트

[SPRING] 스프링 보안은 CSS 또는 JS 리소스를로드 할 수 없습니다

SPRING

스프링 보안은 CSS 또는 JS 리소스를로드 할 수 없습니다

리소스는 src / main / resources / static / css 또는 src / main / resources / static / js에 있으며 스프링 부트를 사용하고 있습니다. 보안 클래스는 다음과 같습니다.

@Configuration
@EnableWebMvcSecurity
@EnableGlobalAuthentication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//      http.authorizeRequests().antMatchers("/", "/index", "/quizStart")
//              .permitAll().anyRequest().authenticated();
//      http.formLogin().loginPage("/login").permitAll().and().logout()
//              .permitAll();
    }

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

브라우저에서 "/ index"에 액세스 할 때 제대로 작동합니다 (리소스를로드 할 수 있음). 그러나 클래스의 네 줄을 주석 처리하지 않으면 리소스를로드 할 수 없습니다. 네 줄은 다음을 의미합니다.

    http.authorizeRequests().antMatchers("/", "/index", "/quizStart")
            .permitAll().anyRequest().authenticated();
    http.formLogin().loginPage("/login").permitAll().and().logout()
            .permitAll();

누구든지 이걸 도와 줄 수 있니? 미리 감사드립니다.

해결법

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

    1.permitAll로 설정된 항목이 들어있는 디렉토리가 있는지 확인해야합니다.

    permitAll로 설정된 항목이 들어있는 디렉토리가 있는지 확인해야합니다.

    여기에 봄 보안 컨텍스트 파일에서 발췌 한 내용이 있습니다. resources 디렉토리 아래에 js, css 및 images 행이 있으며이 행에 의해 사용 권한이 부여됩니다.

    <security:intercept-url pattern="/resources/**" access="permitAll" />
    
  2. ==============================

    2.웬일인지, 이것은 나를 위해 작동하지 않았다 :

    웬일인지, 이것은 나를 위해 작동하지 않았다 :

    http.authorizeRequests().antMatchers("/resources/**").permitAll();
    

    나는 이것을 덧붙여 야했다 :

    http.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
    

    또한이 줄은 액세스를 제한하는 코드 뒤에 있어야합니다.

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

    3.다음 추가

    다음 추가

    @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/resources/**").anyRequest();
        }
    
  4. ==============================

    4.특정 파일의 경우 "/*.js"또는 디렉토리의 경우 "/ resources / **"와 같이 직접 사용할 수도 있습니다

    특정 파일의 경우 "/*.js"또는 디렉토리의 경우 "/ resources / **"와 같이 직접 사용할 수도 있습니다

     http.authorizeRequests()
                    .antMatchers("/", "/login", "/logout", "/error").permitAll()
                    .antMatchers("/resources/**").permitAll()
                    .antMatchers("/*.js").permitAll()
                    .antMatchers("/api/**").authenticated()
    
  5. ==============================

    5.나도 같은 문제가 있었고 "permitAll"에 대한 액세스가 변경 되어도 도움이되지 않았습니다. 보안을 "none"으로 설정 한 다음 인증없이 css 및 js 파일을 다운로드 할 수있는 새 http 패턴을 만들었습니다.

    나도 같은 문제가 있었고 "permitAll"에 대한 액세스가 변경 되어도 도움이되지 않았습니다. 보안을 "none"으로 설정 한 다음 인증없이 css 및 js 파일을 다운로드 할 수있는 새 http 패턴을 만들었습니다.

    <http pattern="/resources/**" security="none" />
    
  6. ==============================

    6.이것은 결국 나를 위해 일했다. / home (로그인 페이지가 표시됨) 및 오류 메시지에는 인증이 필요하지 않습니다. 모든 리소스는 permitAll이며 / main url은 인증됩니다. 다른 URL (예 : / 사용자 / 고객 등)은 isAuthenticated ()로 추가해야합니다.

    이것은 결국 나를 위해 일했다. / home (로그인 페이지가 표시됨) 및 오류 메시지에는 인증이 필요하지 않습니다. 모든 리소스는 permitAll이며 / main url은 인증됩니다. 다른 URL (예 : / 사용자 / 고객 등)은 isAuthenticated ()로 추가해야합니다.

      <security:intercept-url pattern="/home" access="isAnonymous()"/>
      <security:intercept-url pattern="/error*" access="isAnonymous()"/>      
      <security:intercept-url pattern="/main" access="isAuthenticated()"/>
      <security:intercept-url pattern="/css/**" access="permitAll" />     
      <security:intercept-url pattern="/js/**" access="permitAll" />
      <security:intercept-url pattern="/fonts/**" access="permitAll" />
      <security:intercept-url pattern="/images/**" access="permitAll" />
    
  7. from https://stackoverflow.com/questions/25368535/spring-security-does-not-allow-css-or-js-resources-to-be-loaded by cc-by-sa and MIT license