1

I'm using SpringMVC with Thymleaf and Spring-Security. I want to load a page using Thymleaf template and I can load my static resources.

I want to load for example a picture located in : static/img/theme/logo.png from template.html

Here is what I have : result


template.html :

<body>
    <div layout:fragment="content">
                
        <a href="">img src="../static/img/theme/logo.png" alt="Logo"></a>
                                                        
        <h1>Hello</h1>
                  
    </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

eglease
  • 2,445
  • 11
  • 18
  • 28
ValentinD
  • 143
  • 2
  • 3
  • 11

1 Answers1

2

In your security configuration you would declare something like this:

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

Then something like this:

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

And in your Thymeleaf template you'd declare something like this:

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

A working copy of your project can be found here.

Marco Tedone
  • 592
  • 4
  • 13
  • I have the same security configuration, excepted that I don't manage my profiles for the moment. And when I put this line to get an image :
    `Logo`
    I still don't reach the image.
    – ValentinD Apr 24 '16 at 14:28
  • Here is the project : [link] (http://s000.tinyupload.com/?file_id=09542223325203610613) – ValentinD Apr 24 '16 at 14:53
  • Hi, I've made few changes. You can download from GitHub here: https://github.com/mtedone/nickeed The template page comes back with the logo now – Marco Tedone Apr 24 '16 at 15:41
  • Hi I tried and everything work great ! Thank you very much :) – ValentinD Apr 24 '16 at 20:14
  • My pleasure. BTW, I'm completing the last few lectures for an online course on how to create a Spring Boot website from scratch with Thymeleaf, Bootstrap, Spring Boot, Email, Data JPA, Security and Amazon Web Services. You can register your interest here if you want: http://academy.devopsfolks.com/courses/startup-ready-web-skeleton-with-spring-boot-aws-and-stripe/ – Marco Tedone Apr 24 '16 at 20:16