【问题标题】:Spring Security not serving static contentSpring Security 不提供静态内容
【发布时间】:2019-10-20 22:32:04
【问题描述】:

我正在尝试获取 spring 安全性以允许提供静态文件,如 .css .js 等,而无需先登录。

我尝试使用资源处理程序创建 MVC 配置并更改 spring 安全配置中的规则,但似乎没有任何效果。

MvcConfig.java:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

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

}

SecurityConfig.java:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/", "/assets/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
}

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

}

当我转到 http://localhost:8080/assets/js/particles.min.js 时,我希望它返回文件内容,但每次我尝试像 localhost:8080/assets/* 这样的链接时,它都会返回 login.html 的内容

My assets files My project files

【问题讨论】:

  • 我使用的是 2.1.5,我在代码中添加了@Configuration,但问题仍然存在
  • link 这个项目是今天早上新创建的,我一直在努力解决这个问题,我只有两个文件,我包含了指向它们内容的链接,但我不认为他们会有所帮助。这是我第一个使用 Spring Security 的项目,所以可能在某个地方我犯了一些我看不到的简单错误。我还用我的文件截图更新了帖子。
  • 它在子包配置中。
  • 哦,对不起,我刚刚意识到发生了什么,我碰巧刷新页面,同时它在断点处停止,我认为这是每个请求。只需再次检查,你就对了,它是在开始时。
  • 没有任何变化

标签: java spring spring-boot spring-mvc spring-security


【解决方案1】:
web.ignoring().antMatchers("/assets/**");

上面的语句将告诉 Spring Security 忽略任何以 “/assets/” 开头的请求。所以如果我是你,我将删除以下所有配置:

.antMatchers("/", "/assets/**")
        .permitAll()

来自configure(HttpSecurity http) 方法。

【讨论】:

  • 我发现添加 web.ignoring().antMatchers("/assets/") 为某人解决了问题,但即使删除它也不能解决问题。问题仍然存在
  • 我认为你误会了。看看这个链接spring.io/blog/2013/07/03/…更好的理解
【解决方案2】:

假设你的静态文件在src/main/resources下:

有两个主要部分需要配置:

实现WebMvcConfigurer 接口以发现您的静态资源:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/assets/**")) {
            registry.addResourceHandler("/assets/**")
                    .addResourceLocations("/assets/");
        }
    }
}

设置您的安全配置以允许公开访问静态资源(例如 CSS、JavaScript 和图像):

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  // Your settings

    @Override  
    protected void configure(HttpSecurity http) throws Exception {

        // Your AuthN handlers and filter chain...

        http        
            .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .anyRequest().authenticated();

        // Logout handler...
    }
}

假设你有一个 CSS 文件如下:

src/main/resources/assets/css/layout.css

网络服务器将使其可在以下位置访问:

http://<root_url>:<port>/css/layout.css

【讨论】:

  • 效果和显示login.html的内容一样
  • 因此,问题出在其他地方,因为静态资源配置非常简单。您是否检查过您的 MVC 控制器正在呈现正确的内容页面?是否有任何由自定义处理程序管理的重定向?
  • 只有一个控制器有一个映射来处理 /login 我想不出除了安全配置之外的任何其他地方
  • 您基本上被 authN 过滤器链重定向。请注意,如果您将静态文件设置为src/main/resources/assets,则无需在您的 url 中包含 "assets"。例如,src/main/resources/assets/css/layout.css 可以通过网络服务器在http://<root_url>/css/layout.css 访问。
  • 我在使用这种方法时遇到的问题是,我无法在我的日志记录设置中分离对静态的访问和对控制器的访问。我的日志文件挤满了对静态文件的一堆获取请求。
【解决方案3】:

尝试更改为:

http.authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers("/assets/").permitAll()
        .and()
        .authorizeRequests()
        .anyRequest()
        .authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll();

【讨论】:

  • 仍然,而不是文件显示 login.html 的内容
猜你喜欢
  • 2021-06-19
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2019-09-20
  • 1970-01-01
  • 2015-05-24
  • 2011-03-13
相关资源
最近更新 更多