0

After using Spring Security my html pages from templates folder stop displaying. I'm trying to secure my REstful API with Spring Security and I have some static html pages which are located in templates folder under resource folder.

Before implementing Spring Security all the pages would display and could be navigated. But after implementing Spring Security all the pages stop displaying.

I have tried few threads specially this thread is closed to my problem: After spring security implementation it's blocking all resources in my static folder?

And tried the solution were provided there. but those solution did not solve my problem. After using those solution I could only display the home page, but can't navigate to other pages. Somewhere I got the suggestion that incase some static html pages located inside templates, then those sources should be declared in application.propert files and this declaration has been done properly for this project.

Below is the code from my Security configuration class.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.zevgma.api.auth.ManagementUserDetailsService;

@Configuration
@EnableWebSecurity
public class ApplicationsecutiryConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    ManagementUserDetailsService managementUserDetailsService;

    @Bean
    public DaoAuthenticationProvider authenticationProvider(){
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(managementUserDetailsService);
        provider.setPasswordEncoder(new BCryptPasswordEncoder());

        return provider;
    }

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

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
                .ignoring()
                .antMatchers("/assets/**", "/customs/**", "/templates/**");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/","/ru", "/resources/**", "/static/**", "/css/**", "/js/**", "/images/**","/assets/**","/fonts/**", "/**/favicon.ico" ,
                        "/ajax/**", "/bootstrap/**",  "/templates/**").permitAll()
                 .anyRequest().authenticated();

    }

}

I have tried to follow the below mentioned thread's suggestion but couldn't solve my problem. - Serving static web resources in Spring Boot & Spring Security application - https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot - Unable to load static content in spring security - Spring security does not allow CSS or JS resources to be loaded - Spring Boot not serving static content

File structure: file hierarchy in my resource folder

Mamun
  • 375
  • 2
  • 8
  • 17

0 Answers0