首页 > 解决方案 > 如何让spring security允​​许访问静态资源?

问题描述

我决定在我的应用程序中添加一些 CSS,但由于 Spring 安全性,它无法正常工作。这是我试图解决的问题。1)添加到mvcConfig

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
            .addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
}

2)将此添加到 webSecurityConfig

 @Override
public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/resources/**");
}

3)我也有这个方法

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/","/registration","/resources/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
}
  1. 我也将此添加到 application.properties 文件中: spring.mvc.static-path-pattern=/resources/**

这些方法都不适合我。如果有人处理过这个问题,请帮忙

如果对我的项目有帮助,您可以查看整个项目

ps 我只是春天的初学者,所以不要评判我

标签: cssspringspring-securitystaticresources

解决方案


您也必须忽略 css 目录。

@Component
public class SecurityConfig implements WebSecurityConfigurer {

    @Override
    public void configure(WebSecurity web) {
        web
            .ignoring() 
                .antMatchers("/css/**")
                .antMatchers("/js/**")
                .antMatchers("/images/**")
                .antMatchers("/resources/**");
    }
}

推荐阅读