【问题标题】:Spring security does not allow CSS or JS resources to be loadedSpring Security 不允许加载 CSS 或 JS 资源
【发布时间】:2014-10-11 16:12:16
【问题描述】:

资源在src/main/resources/static/css或src/main/resources/static/js下,我用的是spring boot,安全的class是:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalAuthentication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.inMemoryAuthentication().withUser("test").password("test")
                .roles("USER");
    }
}

从浏览器访问“/index”时效果很好(可以加载资源),但是如果我取消类中的四行注释,则无法加载资源,这四行表示:

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

有人可以帮忙吗?提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可能希望确保将包含这些项目的目录设置为 permitAll。

    这是我的 spring 安全上下文文件的摘录。在资源目录下,我有 js、css 和 images 文件夹,这些文件夹由这一行授予权限。

    <security:intercept-url pattern="/resources/**" access="permitAll" />
    

    【讨论】:

    • 感谢您的通知,我将行 http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**").permitAll(); 添加到 protected void configure(HttpSecurity http) 中,然后它可以工作了,非常感谢。
    • 没问题。我使用我在网上找到的一个非常好的 maven 原型来生成这个项目,它从一个工作的、spring MVC、spring security、JPA 和 thymeleaf 项目开始。默认情况下,它有一个非常好的 spring java 配置设置,你可能想看看:github.com/kolorobot/spring-mvc-quickstart-archetype.
    • 对我来说真的很好,我已经加入了观看列表,稍后会尝试,再次感谢~
    • 这个文件应该放在哪里?有什么例子吗?
    • Spring Boot 默认允许访问 /css/**/js/**/images/**/**/favicon.ico
    【解决方案2】:

    由于某种原因,这对我不起作用:

    http.authorizeRequests().antMatchers("/resources/**").permitAll();
    

    我必须添加这个:

    http.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
    

    另外,这一行必须在限制访问的代码之后。

    【讨论】:

    • 这样你删除了所有的安全,'.anyRequest().permitAll()' 将允许所有请求,你必须找到你的资源的正确路径并使用它。如果你使用spring security,那么通常anyRequest() 必须是authenticated()
    【解决方案3】:

    添加关注

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

    【讨论】:

    • .anyRequest();将阻止您的网站
    • @Yogesh:工作..Kudos,这正是我遇到的问题。再次感谢。
    【解决方案4】:

    你也可以直接使用“/*.js”来表示特定文件或“/resources/**”来表示目录

     http.authorizeRequests()
                    .antMatchers("/", "/login", "/logout", "/error").permitAll()
                    .antMatchers("/resources/**").permitAll()
                    .antMatchers("/*.js").permitAll()
                    .antMatchers("/api/**").authenticated()
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,permitAll() 解决方案对我不起作用。我将以下@Overridemethod 添加到我的WebSecurityConfigclass。

      @Override
      public void configure(WebSecurity web) throws Exception {
          web
                  .ignoring()
                  .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**");
      }
      

      祝你好运!

      【讨论】:

        【解决方案6】:

        我遇到了同样的问题,更改对“permitAll”的访问没有帮助。 我创建了一个新的 http 模式,将安全设置为“无”,然后无需身份验证即可下载 css 和 js 文件。

        <http pattern="/resources/**" security="none" />
        

        【讨论】:

          【解决方案7】:

          这终于对我有用了。 /home(将打开登录页面)和错误消息不需要身份验证。所有资源都是 permitAll,并且 /main url 是经过身份验证的。任何其他网址(例如 /users /customers 等)都需要添加为 isAuthenticated()

            <security:intercept-url pattern="/home" access="isAnonymous()"/>
            <security:intercept-url pattern="/error*" access="isAnonymous()"/>      
            <security:intercept-url pattern="/main" access="isAuthenticated()"/>
            <security:intercept-url pattern="/css/**" access="permitAll" />     
            <security:intercept-url pattern="/js/**" access="permitAll" />
            <security:intercept-url pattern="/fonts/**" access="permitAll" />
            <security:intercept-url pattern="/images/**" access="permitAll" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-10-08
            • 2016-11-04
            • 1970-01-01
            • 2016-08-17
            • 2019-05-24
            相关资源
            最近更新 更多