Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何使用SpringSecurity允许SpringBootMaven的静态文件夹中的所有资源?_Spring Boot_Spring Security - Fatal编程技术网

Spring boot 如何使用SpringSecurity允许SpringBootMaven的静态文件夹中的所有资源?

Spring boot 如何使用SpringSecurity允许SpringBootMaven的静态文件夹中的所有资源?,spring-boot,spring-security,Spring Boot,Spring Security,我正在创建一个应用程序,前端使用angular,后端使用spring boot。 我使用spring boot安全性是出于安全目的 Spring boot提供来自src/main/resources/static文件夹的静态内容。 我将angular dist文件夹的所有内容放在这里,即ng build生成的所有js、css和html 但在运行应用程序时,我不断收到一个未经授权的错误。 以下是我的configure()方法: @Override protected void config

我正在创建一个应用程序,前端使用angular,后端使用spring boot。 我使用spring boot安全性是出于安全目的

Spring boot提供来自
src/main/resources/static
文件夹的静态内容。 我将angular dist文件夹的所有内容放在这里,即
ng build
生成的所有js、css和html

但在运行应用程序时,我不断收到一个未经授权的错误。 以下是我的
configure()
方法:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .cors()
                .and()
                .exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
                .and()
                .authorizeRequests()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                .antMatchers("/api/v1/login/").permitAll()
                    .anyRequest().authenticated()
                .and()
                .addFilter(getJWTAuthenticationFilter(authenticationManager()))
                .addFilter(new JWTAuthorizationFilter(authenticationManager()));
    }
如果我添加
.antMatchers(“/**”).permitAll()
,那么应用程序将正确加载所有角度资源,但由于所有api端点都将公开,因此安全性将变得无用

我还了解到默认情况下允许使用
\css、\images、\js
等,但这里我只引用静态文件夹。我还尝试通过实现
addResourceHandlers()
方法将所有内容移动到一个新的端点,比如说
/app
,但随后我的角度代码中断,因为它引用了所有css,jss直接在其href中,而不是在任何文件夹名称后面

我使用的是SpringBoot2.1.1版本,大多数答案都是关于SpringBootV1的,我找不到解决方案


我想将spring security配置为只允许访问
static
文件夹下的所有资源。这样做将允许我在不更改所有HREF的情况下使用角度代码。我如何才能做到这一点?

您的statoic源的URL是什么?显示一个示例。我已将所有资源放在
src/main/resources/static
文件夹中。我通过
http://localhost:8080/
本身。当启用spring安全性时,我开始将401发送到此URL。您的静态资源使用什么URL,例如,如何调用CSS文件?由于应用程序是基于角度的,因此直接调用它们就像
。src总是直接引用该文件。就像所有必需的文件都存在于同一个文件夹中一样,子文件夹中没有任何内容一样。Static源的URL是什么?显示一个示例。我已将所有资源放在
src/main/resources/static
文件夹中。我通过
http://localhost:8080/
本身。当启用spring安全性时,我开始将401发送到此URL。您的静态资源使用什么URL,例如,如何调用CSS文件?由于应用程序是基于角度的,因此直接调用它们就像
。src总是直接引用该文件。只需将其视为所有必需的文件都存在于同一文件夹中,子文件夹中没有任何内容