2016-04-23 168 views
0

我正在使用Thymleaf和Spring-Security的SpringMVC。 我想使用Thymleaf模板加載一個頁面,我可以加載我的靜態資源。Spring Security Thymleaf靜態資源不加載

我想加載例如位於畫面:靜態/ IMG /主題/ logo.png從template.html

以下是我有:result


template.html:

 


     body> 
      div layout:fragment="content"> 

       a href="">img src="../static/img/theme/logo.png" alt="Logo"> 

       h1>Hello 

      /div> 

     /body> 


MvcConfig.java

 

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/home").setViewName("home"); 
     registry.addViewController("/index").setViewName("index"); 
     registry.addViewController("/template").setViewName("template"); 
     registry.addViewController("/layout").setViewName("layout"); 
     registry.addViewController("/login").setViewName("login"); 

    } 



    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 


} 


WebSecurityConfig:

 

    @Configuration 
    @EnableWebSecurity 
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 


     //List of all free pages 

     private static final String[] pagesFree = { 
       "/home", 
       "/template", 
       "/layout", 

       //Thymleaf directory 
       "/css/**", 
       "/js/**", 
       "/img/**", 
       "/fonts/**", 
       "/ico/**", 
       "/twitter/**", 
       "/" 
       }; 



     @Override 
     protected void configure(HttpSecurity http) throws Exception { 



      http 
       .authorizeRequests() 
        .antMatchers(pagesFree).permitAll() 
        .anyRequest().authenticated() 
        .and() 
       .formLogin() 
        .loginPage("/login") 
        .permitAll() 
        .and() 
       .logout() 
        .permitAll(); 
     } 

     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication() 
        .withUser("u").password("u").roles("USER"); 
     } 


    } 


Source Code tree

+0

你能分享你的Spring Security配置嗎? –

回答

1

在您的安全配置,你會聲明是這樣的:

/** Public URLs. */ 
private static final String[] PUBLIC_MATCHERS = { 
     "/webjars/**", 
     "/css/**", 
     "/js/**", 
     "/images/**", 
     "/" 
}; 

然後是這樣的:

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles()); 
    if (activeProfiles.contains("dev")) { 
     http.csrf().disable(); 
     http.headers().frameOptions().disable(); 
    } 

    http 
      .authorizeRequests() 
      .antMatchers(PUBLIC_MATCHERS).permitAll() 
      .anyRequest().authenticated() 
      .and() 
      .formLogin().loginPage("/login").defaultSuccessUrl("/payload") 
      .failureUrl("/login?error").permitAll() 
      .and() 
      .logout().permitAll(); 
} 

而在你Thymeleaf的模板你會聲明是這樣的:

<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" /> 

項目的工作副本可以發現here

+0

我有相同的安全配置,除了我目前不管理我的配置文件。當我把這個線獲得的圖像:
''Logo
我還沒有達到圖像。 – ValentinD

+0

你能分享你的代碼嗎? –

+0

是的,當然,添加在主要問題 – ValentinD