如何使用Spring Security允​​许Spring Boot Maven的静态文件夹中的所有资源?

时间:2019-03-30 12:59:32

标签: spring-boot spring-security

我正在创建一个使用angular进行前端的应用程序并将spring boot用作后端的应用程序。 我出于安全目的使用spring-boot-security。

Spring Boot从src/main/resources/static文件夹提供静态内容。 我将把Angular dist文件夹的所有内容放在这里,即ng build产生的所有js,css,html。

但是在运行该应用程序时,我继续收到401未经授权的错误。 这是我的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等,但是这里我只指静态文件夹。我还尝试通过实现{将所有内容移动到新端点,例如/app {1}}方法,但是我的角度代码坏了,因为它直接在href中引用了所有css,jss,而不是在任何文件夹名后面。

我正在使用Spring Boot 2.1.1发行版,大多数答案都涉及Spring Boot v1,但找不到解决方案。

我想将spring安全性配置为仅允许访问addResourceHandlers()文件夹下的所有资源。这样做将使我能够使用我的角度代码而无需更改所有href。我该如何实现?

0 个答案:

没有答案