2015-11-16 114 views
0

你好角及春季安全 - 安全文件夾和文件

我成功地建立了與春季安全角度的webapp下面這個教程: https://spring.io/guides/tutorials/spring-security-and-angular-js/

一切工作正常,我還配置春季安全使用MySql數據庫。

我使用的春天開機等我angularJS文件位於靜態文件夾:

  • 「的src /主/資源/靜態/ JS」
  • 「的src /主/資源/靜態/ CSS」
  • ...

我的第一個問題是關於從Web應用的角度我必須確保春季安全配置和文件/文件夾? 我是否必須允許訪問Spring安全配置中的所有靜態內容?例如:

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

或者這不是一個好主意嗎?這裏最好的做法是什麼? 我第一次嘗試只訪問「/」和「/ login」頁面,但這不適合我!從教程

實施例彈簧的安全配置:

@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中爲我的角度web應用程序配置單個文件夾,並通過彈簧安全性爲應用程序提供訪問權限,例如這種結構(不低於/靜態):

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

並執行春季安全配置應該是什麼樣子的這種結構?

+0

保護控制器等分別只會工作,如果你在不同的網頁(不同的HTML頁)中使用它們。如果你有一個真正的SPA,那是行不通的。 – dunni

+0

所以沒關係,如果我允許訪問所有角度文件:/ js/**和/ css/**? – Jensebluemchen

回答

0

請指定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

+0

好的,所以我必須定義一個像這樣的新資源「MyAngularApp」? 註冊表 .addResourceHandler( 「/ MyAngularApp/**」) .addResourceLocations( 「(/ MyAngularApp /」); 是我的應用程序,然後在的src/main/MyAngularApp易觸及/? – Jensebluemchen