2015-06-16 60 views
0

我有獨立的正面和背面的Web應用程序:春天開機+ MVC(休息)+ angularJS:添加安全

/project 
+--- /back 
+--- /front 

背面是使用Spring啓動+ Spring MVC的developped,而前輪使用AngularJS 。

我試圖設置後面/前面之間的通信安全。我做了什麼:

- create a ConfigSecurity class which extends from WebSecurityConfigurerAdapter 
- create a SpringWebMvcInitializer which extends from AbstractAnnotationConfigDispatcherServletInitializer and call ConfigSecurity 
- create a SecurityWebInitializer class which extends AbstractSecurityWebApplicationInitializer 

我ConfigSecurity看起來是這樣的:

@Configuration 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class ConfigSecurity extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("/demo/**","/home").permitAll() 
       .anyRequest().fullyAuthenticated() 
       .and() 
       .formLogin().loginPage("...") 
       .and() 
       .httpBasic(); 
    } 
} 

我的問題:

  1. configureGlobal(),我可以設置用戶名和密碼來訪問我保護的URL,但如果不只有一個用戶,我該怎麼辦?我的意思是,我想授予在我的數據庫中註冊的所有用戶的訪問權限。

  2. 由於後臺和前臺只與REST(通過JSON文件)進行通信,因此formLogin()ConfigSecurity應該做什麼?默認情況下,Spring Security會生成一個默認的登錄表單。我不需要它在應用程序的後面,因爲它是負責顯示loginPage的前端。我怎樣才能跳過後面的登錄頁面?也許通過將用戶名和密碼放在JSON文件中,前端發送給後端?有人知道如何做到這一點嗎?

我正在使用Spring的Java配置(不是XML配置)。

+1

請檢查本教程https://spring.io/blog/2015/01/12/the-login-page-angular-js-and-spring-security-part-ii它應該解釋如何從AngularJS登錄應用。 – kTT

回答

2

你有很多選擇,他們都需要相當多的代碼,但其中大部分都已經很好地在互聯網上實現,所以你只需要使它適合你的需求。您可以採取最簡單的方式,即http會話,JSON令牌或JWT(JSON Web令牌)或其他任何方式。

HTTP會話肯定是最容易設置,並且已經被Spring Security很好地支持。