Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring security login page - images

i'm a newbie. I'm trying to developp a webapp using Spring. I have a custom login page where i want to insert an image. How can i do? I've red all of the questions on SO but no one worked for me...

I don't know why i can't see the image.

This is my project structure :

- src/main/resources
  - images
    -logo.png
  + static
  - template
    -login.html

that's the image in login.html:

<img src="/resources/images/logo.png" align="left" />

could be better using thymeleaf taglib th:?

that's securityConfig :

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

    http
    .authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .formLogin()
    .loginPage("/login")
    .permitAll()
    .and()
    .logout()
    .permitAll();
}
like image 957
alex Avatar asked Sep 12 '25 18:09

alex


2 Answers

Your request to get the image will be blocked because the user is not authenticated. You have to whitelist the URLs to your static resources (Javascript files, images, CSS files etc).

This will work:

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

    String[] staticResources  =  {
        "/css/**",
        "/images/**",
        "/fonts/**",
        "/scripts/**",
    };

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

Also, my folder structure is:

/src/main/resources/static/images/...
/src/main/resources/static/scripts/...

My code works for this setup.

like image 67
Andrei Epure is hiring Avatar answered Sep 15 '25 11:09

Andrei Epure is hiring


You should hold your images under static folder it's default directory for resources. For example move images folder there and modify HTML:

<img src="images/logo.png" align="left" />

or this if you want append some variables in image's path:

<img th:src="images/logo.png" align="left" />
like image 32
Michael Dz Avatar answered Sep 15 '25 10:09

Michael Dz