我正在研究Spring Security并将它介绍给我的Spring MVC项目。 但是,我的资源现在被阻止了404(CSS / JS / IMG..etc) 有谁知道他们被封锁的原因?我怀疑WebInit.java中的Dispatcher Servlet存在问题..?
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");
}
}

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

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/*");
}
}

答案 0 :(得分:0)
将配置更改为
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.antMatchers("/Dashboard/**").hasRole("ADMIN")
.and()
.httpBasic();
}