Spring Security不允许加载CSS或JS资源 [英] Spring security does not allow CSS or JS resources to be loaded

查看:285
本文介绍了Spring Security不允许加载CSS或JS资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该资源位于src/main/resources/static/css或src/main/resources/static/js下,我使用的是Spring Boot,安全级别为:

The resource is under src/main/resources/static/css or src/main/resources/static/js, I'm using spring boot, and the class of security is:

@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"时,它运行良好(可以加载资源),但是,如果取消注释该类中的四行,则无法加载资源,这四行表示:

It works well (resources can be loaded) when I access "/index" from browser, however, if I uncomment the four lines in the class, resources can not be loaded, the four lines means:

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

有人可以帮忙吗?预先感谢.

Could anyone help with this ? Thanks in advance.

推荐答案

您可能要确保将包含那些项的目录设置为allowAll.

You probably want to make sure to have your directory containing those items set as permitAll.

这是我的spring安全上下文文件的摘录.在resources目录下,我有js,css和images文件夹,并通过此行为其授予了权限.

Here's an excerpt from my spring security context file. Under the resources directory, I have js, css, and images folders which are given permissions by this line.

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

这篇关于Spring Security不允许加载CSS或JS资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆