Pbb
Pbb

Reputation: 428

Serving Static Html Files From Spring, cant get with Spring Security

I have a restful application made up of html, js, and css files. The application is divided into two folders, regular and admin. I have put the two folders under the Spring static directory.

src/main/resources/static/regular

src/main/resources/static/admin

src/main/resources/static/regular/index.html

package coffee.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web
                        .configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web
                    .configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("peter").password("peter")
            .authorities("ROLE_USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin/**").hasRole("USER")
        .antMatchers("/", "/**").permitAll()
        .and()
        .formLogin().loginPage("/login.html");
    }
}

package coffee.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/admin/**").addResourceLocations("/static/admin/");
        registry.addResourceHandler("/**").addResourceLocations("/static/regular/");

    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }

}

Upvotes: 1

Views: 895

Answers (1)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

Just change your configuration file from :

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/admin/**").addResourceLocations("/static/admin/");
        registry.addResourceHandler("/**").addResourceLocations("/static/regular/");

    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }

}

To:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/regular/index.html");
    }

}

Upvotes: 2

Related Questions