【问题标题】:Spring security can't access files under /resourcesSpring Security 无法访问 /resources 下的文件
【发布时间】:2016-11-06 01:34:56
【问题描述】:

您好,我正在尝试在我的应用程序的 /resources 文件夹中包含一个前端。 我的目录结构如下:

src
  main
    java // backend located here
  webapp
     resource //my html/css/js

我正在使用 oauth 来保证后端的安全性:

MethodSecurityConfig.java

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        return new OAuth2MethodSecurityExpressionHandler();
    }

}

ResourceServerConfigurerAdapter.java

@Configuration

    @EnableResourceServer
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Override
        public void configure(final HttpSecurity http) throws Exception {

            http.authorizeRequests()
                    .antMatchers("/resources/**").permitAll().and()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                    .and().authorizeRequests().anyRequest().authenticated();

        }
    }

ResourceWebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan({"eu.emif.resource.web"})
public class ResourceWebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("(/resources/");
    }
}

每当我尝试去http://localhost:8082/style.csshttp://localhost:8082/resources/style.css 我得到了

 <oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>

我们将不胜感激。

【问题讨论】:

  • 你在用maven吗??
  • 是的,我的后端使用 maven

标签: java spring spring-security spring-security-oauth2


【解决方案1】:

改变你的目录结构如下:

src
  main
    java // backend located here
    resources(By default these files are not added in deployment assembly and mostly used for loading java resources like xml file,properies file etc.)  
  webapp
    resource(create new folder here and put your frontend(html/js/css) files here)

【讨论】:

猜你喜欢
  • 2014-10-02
  • 2020-05-05
  • 2023-03-23
  • 2012-11-07
  • 2021-06-06
  • 2018-07-22
  • 1970-01-01
  • 2014-10-15
  • 2014-09-15
相关资源
最近更新 更多