gpt4 book ai didi

java - Spring Security无法访问/resources下的文件

转载 作者:行者123 更新时间:2023-12-01 09:44:30 25 4
gpt4 key购买 nike

您好,我正在尝试将前端包含在我的应用程序的/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.css时或http://localhost:8082/resources/style.css我正在得到

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

非常感谢您的帮助。

最佳答案

将目录结构更改为如下:

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)

关于java - Spring Security无法访问/resources下的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com