【问题标题】:Angular & Spring security - Secure folders and filesAngular 和 Spring 安全性 - 保护文件夹和文件
【发布时间】:2016-02-17 13:03:49
【问题描述】:

你好

我按照本教程成功设置了一个带有 spring security 的 angular webapp: https://spring.io/guides/tutorials/spring-security-and-angular-js/

一切正常,我还配置了 Spring Security 以使用 MySql 数据库。

我使用的是spring boot,所以我的angularJS文件位于静态文件夹中:

  • “src/main/resources/static/js”
  • “src/main/resources/static/css”
  • ...

我的第一个问题是关于 spring 安全配置以及我必须保护 angular webapp 中的哪些文件/文件夹? 我是否必须允许访问 spring 安全配置中的所有静态内容?例如:

http.httpBasic().and().authorizeRequests()
              .antMatchers("/js/**", "/css/**", "/").permitAll()

或者这不是一个好主意?这里的最佳做法是什么? 我首先尝试只授予对“/”和“/login”页面的访问权限,但这对我不起作用!

教程中的示例 spring 安全配置:

  @Configuration
  @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http
        .httpBasic()
      .and()
        .authorizeRequests()
          .antMatchers("/index.html", "/home.html", "/login.html", "/").permitAll()
          .anyRequest().authenticated();
    }
  }

我的第二个问题是关于我的 Spring Boot 应用程序中 /static 下的角度文件和文件夹结构。如何在 spring boot 中为我的 angular webapp 配置一个单独的文件夹,并通过 spring security 授予应用程序访问权限,例如使用这种结构(不在 /static 下):

MyAngularApp
  -- login
     -- Controller
        -- loginController
     -- Services
     -- Templates
        -- login.html
  -- Feature 2
     -- Controller
     -- Services
     -- Templates
  -- Feature 3
     -- Controller
     -- Services
     -- Templates

对于这个结构,spring 安全配置应该是什么样的?

【问题讨论】:

  • 仅当您在不同页面(不同的 HTML 页面)上使用它们时,单独保护控制器等才会起作用。如果你有一个真正的 SPA,那是行不通的。
  • 所以没关系,如果我允许访问所有角度文件: /js/** 和 /css/** ?

标签: angularjs spring-security spring-boot


【解决方案1】:

请指定spring MVC的静态资源如下:

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("(/resources/");    
    }
}

Please refer this for more details.this

【讨论】:

  • 好的,所以我必须像这样定义一个新的资源“MyAngularApp”? registry .addResourceHandler("/MyAngularApp/**") .addResourceLocations("(/MyAngularApp/"); 然后我的应用程序可以在 src/main/MyAngularApp/ 访问吗?
猜你喜欢
  • 2013-04-22
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多