本文介绍了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.

推荐答案

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

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

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

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 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:02
查看更多