2016-11-15 74 views
0

我寫的應用程序在啓動春季,春季安全與Thymeleaf,我試圖讓我的訪問靜態資源文件...春天引導安全性的靜態資源

這是我的項目結構...

​​3210

我在模板/ index.html的HTML文件,其中我嘗試使用標籤

 <img src="/praca.jpg" alt="sd"/> 

爲什麼我總是得到404錯誤?我在哪裏做錯了什麼?

我一般初始化類:

@SpringBootApplication 
    public class Application extends WebMvcConfigurerAdapter { 

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

我的安全類:

@Configuration 
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private UserAuthenticationDetails userAuthenticationDetails; 

     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.userDetailsService(userAuthenticationDetails); 
      auth.authenticationProvider(authenticationProvider()); 
     } 

     @Bean 
     public PasswordEncoder passwordEncoder() { 
      return new BCryptPasswordEncoder(); 
     } 

     @Bean 
     public DaoAuthenticationProvider authenticationProvider() { 
      DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); 
      authenticationProvider.setUserDetailsService(userAuthenticationDetails); 
      authenticationProvider.setPasswordEncoder(passwordEncoder()); 
      return authenticationProvider; 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.csrf().disable() 
        .authorizeRequests() 
        .antMatchers("/","/login").permitAll() 
        .anyRequest().authenticated() 
        .and() 
        .formLogin() 
        .loginPage("/login") 
        .usernameParameter("username") 
        .passwordParameter("password") 
        .defaultSuccessUrl("/",true) 
        .and() 
        .logout() 
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutUrl("/logout") 
        .logoutSuccessUrl("/login?logout") 
        .invalidateHttpSession(true); 
     } 

    } 
+0

只需確認,整個頁面返回一個404或只是圖像資產? –

+0

404只返回圖片資源。其他內容載入成功。 – technics

回答

1

在模板中,你需要使用thymeleaf格式自行自動添加上下文。使用此:

<img th:src="@{/praca.jpg}" alt="sd"/> 

/praca.jpg

應該從靜態或公用文件夾

+0

它不適合我。 – technics

0

我也有類似的問題,這個問題到圖像的完整路徑與春季安全,所以任何有此問題的人都可以嘗試將其靜態文件所在的文件夾添加到不需要身份驗證的網址列表中 例如antMatchers("/css/**", "/fonts/**").permitAll()