Spring security nie pozwala na ładowanie zasobów CSS lub JS

Zasób znajduje się pod src/main/resources/static/css lub src / main / resources/static / js, używam Spring boot, A Klasa bezpieczeństwa to:

@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");
    }
}

To działa dobrze (zasoby mogą być załadowane) kiedy uzyskuję dostęp do" / index " z przeglądarki, jednak jeśli odkomentuję cztery linie w klasie, zasoby nie mogą być załadowane, cztery linie oznaczają:

    http.authorizeRequests().antMatchers("/", "/index", "/quizStart")
            .permitAll().anyRequest().authenticated();
    http.formLogin().loginPage("/login").permitAll().and().logout()
            .permitAll();
Czy ktoś mógłby w tym pomóc ? Z góry dzięki.
Author: jww, 2014-08-18

7 answers

Prawdopodobnie chcesz się upewnić, że twój katalog zawierający te elementy jest ustawiony jako permitAll.

Oto fragment mojego pliku kontekstowego spring security. W katalogu resources mam foldery js, css i images, które mają uprawnienia tej linii.
<security:intercept-url pattern="/resources/**" access="permitAll" />
 33
Author: John Humphreys - w00te,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-18 17:15:12

Z jakiegoś powodu, to nie działa dla mnie:

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

Musiałem dodać to:

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

Również ta linia musi być po kodzie, który ogranicza dostęp.

 18
Author: Sande,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-14 13:21:38

Dodaj następujący

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**").anyRequest();
    }
 11
Author: Yogesh Bombe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-28 12:10:07

Możesz również użyć bezpośrednio jak "/*.js "dla określonego pliku lub"/ resources / ** " dla katalogu

 http.authorizeRequests()
                .antMatchers("/", "/login", "/logout", "/error").permitAll()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/*.js").permitAll()
                .antMatchers("/api/**").authenticated()
 7
Author: Om Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-20 09:50:19

Miałem ten sam problem i permitAll() rozwiązanie nie działało dla mnie. Dodałem następującą metodę @Overridedo mojej klasy WebSecurityConfig.

@Override
public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**");
}
Powodzenia!
 5
Author: Athena,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-03-10 19:01:31

Miałem ten sam problem i zmiana dostępu do "permitAll" nie pomogła. Stworzyłem nowy wzorzec http, w którym ustawiłem bezpieczeństwo na "brak", a następnie mogłem pobrać pliki css i js bez uwierzytelniania.

<http pattern="/resources/**" security="none" />
 0
Author: JohnP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-01 16:21:19

To w końcu zadziałało. /Home (który wyświetli stronę logowania) i komunikaty o błędach nie wymagają uwierzytelniania. Wszystkie zasoby są dozwolone, a adres URL / main jest uwierzytelniony. Dowolny inny adres url (np. / użytkownicy / klienci itp..) trzeba by dodać jako 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" />
 0
Author: Shahriar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-02 21:47:36