Spring Security 404 Static Resources

I am learning Spring Security and introduced it to my Spring MVC project. However my resources are now locked w / 404 (CSS / JS / IMG..etc) Does anyone know why they are being locked? I suspect there is a problem with the dispatch servlet in WebInit.java ..?

Project structure

SpringSecurity.java

package com.catalyst.Config;

/*
    Not Done!
    Still working on Spring Security!
    Problem: Blocking my Resources Folder
*/
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter
{    
    @Override
    public void configure(WebSecurity webSecurity) throws Exception
    {
        webSecurity
            .ignoring()
                .antMatchers("/Resources/**");
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .authorizeRequests()
                .antMatchers("/Resources/**").permitAll()
                .antMatchers("/Dashboard/**").hasRole("ADMIN")
                .and()
                .httpBasic();
    }
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER")
                .and()
                .withUser("admin").password("password").roles("USER", "ADMIN");
    }
}
      

Run code


WebInit.java

package com.catalyst.Config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInit implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException
    {
        Dynamic hServlet;
        AnnotationConfigWebApplicationContext hAnnoCTX;

        hAnnoCTX = new AnnotationConfigWebApplicationContext();
        hAnnoCTX.register(WebMVCConfig.class);
        hAnnoCTX.setServletContext(servletContext);
        hServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(hAnnoCTX));
        hServlet.addMapping("/");
        hServlet.setLoadOnStartup(1);
    }
}
      

Run code


WebMVCConfig.java

package com.catalyst.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.catalyst")
@EnableWebMvc
public class WebMVCConfig extends WebMvcConfigurerAdapter
{
    @Bean
    public UrlBasedViewResolver setupViewResolver()
    {
        UrlBasedViewResolver hResolver;
        hResolver = new UrlBasedViewResolver();
        hResolver.setPrefix("/WEB-INF/JSP/");
        hResolver.setSuffix(".jsp");
        hResolver.setViewClass(JstlView.class);
        return(hResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry hRegistry)
    {
        hRegistry.addResourceHandler("/Resources/**").addResourceLocations("/WEB-INF/Resources/*");
    }
}
      

Run code


+3


source to share


1 answer


Change your config to



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

            .antMatchers("/Dashboard/**").hasRole("ADMIN")
            .and()
            .httpBasic();
}

      

0


source







All Articles