3
我在學習Spring Security,並將它引入到我的Spring MVC項目中。 但是,我的資源現在被阻止瓦特/ 404(CSS/JS/IMG..etc) 任何人都知道他們爲什麼被封鎖?我在WebInit.java中懷疑Dispatcher Servlet的問題..?Spring Security 404靜態資源
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/*");
}
}
仍然沒有運氣。在與一些同事交談之後,我決定拋棄Spring Security。似乎只是問題。 – Sessions1024
您可以在更改後提供最新的代碼...爲什麼不用基於XML的配置進行Spring Security配置? – Abhinay
上面的代碼是我所做的唯一修改。註釋似乎是未來,所以我決定這將是繼續前進和學習的最佳途徑。 – Sessions1024