我启动了 Spring Boot + Angular 应用程序,现在我想将整个应用程序部署为 jar。所以我创建了 Maven 配置,其中构建了 Angular 应用程序,然后将其复制到 /target/classes/resources
但是每个对 root (localhost:8080) 的请求都会被安全阻止。当我禁用它时,我可以看到该页面,这意味着整个事情都已正确部署,但不知何故 spring 不允许我看到它。这是我的简单安全配置,我希望静态资源不受保护,而任何其他请求都需要身份验证:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated()
.and().httpBasic();
}
}
编辑: 我的问题的一个最小例子在这里: https://gitlab.com/jnoacki/security-issue-demo
编辑2: 我尝试了这篇文章中的所有内容: 在 Spring Boot 和 Spring Security 应用程序中提供静态 Web 资源 我在概念层面上做错了什么吗?将静态内容与 Spring Boot 应用程序一起提供是错误的吗?
添加此附加覆盖:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(AUTH_WHITELIST);
}
其中
AUTH_WHITELIST
将包含要忽略的路径。例如:
private static final String[] SWAGGER_AUTH_WHITELIST = {
// -- swagger ui
"/v2/api-docs",
"/swagger-resources",
"/swagger-resources/**",
"/swagger-ui.html",
"/resources/**"
};
尝试下面。
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
使用此方法可以让静态资源被spring security忽略
//this method allows static resources to be neglected by spring security
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**", "/static/**");
}
根据
StaticResourceLocation
文档:
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
将允许访问CSS、JS、ICO、图像,不允许访问html。
要允许
index.html
,您可以使用以下配置:
http.authorizeRequests()
.antMatchers("/index.html", "/").permitAll()
.anyRequest().authenticated()
.and().httpBasic();
// 扩展 WebSecrityConfiguratesAdapeter
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Overide
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(HttpMethod.OPTIONS, ALL_ESCAPE)
.antMatchers("*.js")
.antMatchers(")
.antMatchers(BOWER_COMPONENTS)
.antMatchers(I18N)
.antMatchers(CONTENT);
}
}
您必须匹配真实路径,或示例:
.pathMatchers("/assets/**", "/static/**")
.permitAll()ere
试试这个:
.requestMatchers( “/v1/api/get-token”, “/swagger-ui.html”, “/swagger-ui/*”, “/v3/api-docs/”, “/swagger-资源/”, "/webjars/**").permitAll()