Spring Security不允许静态内容
在过去的几天里,我一直在与Spring Security进行斗争,所以我希望有人能在这里帮助我Spring Security不允许静态内容,spring,spring-mvc,spring-security,spring-boot,Spring,Spring Mvc,Spring Security,Spring Boot,在过去的几天里,我一直在与Spring Security进行斗争,所以我希望有人能在这里帮助我 我使用的是Spring Boot 1.2.5 我使用的是Spring执行器和Spring Remote Shell,它们已经从类路径中删除,认为它们可能会导致问题 我排除了SecurityAutoConfiguration,因为它可能导致我的问题 这是我的主课 @SpringBootApplication(exclude = {org.springframework.boot.autoconfig
- 我使用的是Spring Boot 1.2.5
- 我使用的是Spring执行器和Spring Remote Shell,它们已经从类路径中删除,认为它们可能会导致问题
- 我排除了SecurityAutoConfiguration,因为它可能导致我的问题
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
这是我的安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AuthFailureHandler authFailureHandler;
@Autowired
private AuthSuccessHandler authSuccessHandler;
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/css/**")
.antMatchers("/js/**")
.antMatchers("/images/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint())
.accessDeniedPage("/403")
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/about").permitAll()
.antMatchers("/login").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.usernameParameter("sec-user")
.passwordParameter("sec-password")
.loginPage("/login")
.failureHandler(authFailureHandler)
.successHandler(authSuccessHandler)
.permitAll()
.and()
.logout()
.deleteCookies("JESSIONID")
.invalidateHttpSession(true)
.permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
}
我的问题是
<link rel="stylesheet" media="screen" th:href="@{/css/main.css}" />
在添加安全性之前,我的静态资源工作正常
我的静态文件位于resources/public/中
根据Spring Boot文档,这很好
默认情况下,Spring Boot将从类路径中名为/static(或/public或/resources或/META-INF/resources)的文件夹或ServletContext的根目录提供静态内容
尝试添加资源处理程序
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**")
.addResourceLocations("classpath:/css/**");
registry.addResourceHandler("/img/**")
.addResourceLocations("classpath:/img/**");
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
为什么要将css/js/图像映射为授权请求下的URL?你不应该在那里列出它们,你应该将它们添加到你的资源处理程序中,让spring来完成这项工作。我使用的是spring Boot,我的理解是它已经通过它的自动配置完成了这项工作,因为在我添加安全性之前,它运行良好,除非我不理解你。它们位于/resources/public中,spring boot会自动映射“.authorizeRequests().antMatchers(“/css/**”、“js/**”、“images/**”)。permitAll()是多余的代码,不应在其中列出。您基本上是告诉系统希望/css是一个有效的url来访问。您仍然可以在启动时使用资源处理程序。但是,摆脱这一行,给它一个旋转我更新了我的配置,但它仍然不工作。当请求/css/main.css时,它会返回作为登录页面的HTML。您究竟是如何加载资源的?你能显示视图代码吗