【问题标题】:Cannot access content from static folder with Spring Security configurations无法使用 Spring Security 配置访问静态文件夹中的内容
【发布时间】:2018-06-29 08:16:20
【问题描述】:

我正在尝试访问“src\main\resources\static”文件夹中的内容,但在 chrome 的检查窗口中出现 401 错误。

在“静态”文件夹中,我有一个名为“assets”的子文件夹,结构有点像这样。

assets/css/** assets/js/** assets/fonts/** assets/image/** assets/css/boostrap/**

这是我的WebMvcConfigurerAdapter 课程

@Configuration
public class OAuthWebFormConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");

registry.addViewController("/oauth/confirm_access").setViewName("authorize");
}

@Configuration
@Order(-20)
protected static class LoginConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationManager customAuthenticationManager;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login").permitAll()
                .and()
                .requestMatchers()
                .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")//,"/resources/**", "/assets/**"
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(customAuthenticationManager);
    }

    }
    }

【问题讨论】:

标签: java spring-boot spring-security


【解决方案1】:

添加以下内容:

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

因此,通过这种方式,您允许所有请求匹配 /assets/** 的任何请求(因此您需要的所有可能性)

希望对你有帮助

【讨论】:

  • 我以前试过这个,但对我不起作用。仍然 chrome 检查显示 Refused to apply style from 'http://server:port/app/assets/bootstrap/css/bootstrap.min.css' because its MIME type ('application/json') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
  • 这是另一个问题,在引导文件的声明中将类型设置为 CSS ...阅读错误:因为它的 MIME 类型 ('application/json') 不是受支持的样式表 MIME 类型,并启用严格的 MIME 检查。像这样设置:type="text/css"
  • 我也尝试从直接 url 访问资源但没有工作。它正在显示Full authentication is required to access this resource
  • .. 如果您不更改当前的 LoginConfig.class,您将永远无法访问 /assets
猜你喜欢
  • 2019-03-04
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 2017-07-09
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多