Spring Security 404 静态资源
Posted
技术标签:
【中文标题】Spring Security 404 静态资源【英文标题】:Spring Security 404 Static Resources 【发布时间】:2015-08-21 00:52:07 【问题描述】:我正在研究 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/*");
【问题讨论】:
【参考方案1】:将您的配置更改为
@Override
protected void configure(HttpSecurity http) throws Exception
http
.authorizeRequests()
.antMatchers("/Dashboard/**").hasRole("ADMIN")
.and()
.httpBasic();
【讨论】:
还是没有运气。在与一些同事交谈后,我决定放弃 Spring Security。似乎只是有问题。 您能否在更改后提供最新代码...为什么不为 Spring 安全配置选择基于 XML 的配置? 上面的代码是我做的唯一改变。注释似乎是未来,所以我认为这是继续学习的最佳途径。以上是关于Spring Security 404 静态资源的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 整合security 4 怎么设置忽略的静态资源
Spring security 如何设置才能避免拦截到静态资源