簡體   English   中英

Spring Boot + MVC(Rest)+ angularJS:增加安全性

[英]Spring boot + mvc (rest) + angularJS: add security

我有一個Web應用程序,其正面和背面分別存在:

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

背面使用Spring boot + Spring MVC開發,而正面使用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文件),因此ConfigSecurity formLogin()應該怎么做? 默認情況下,Spring Security會生成一個默認的登錄表單。 我不需要在應用程序的背面,因為它是負責顯示loginPage的正面。 如何跳過后面的登錄頁面? 也許通過將用戶名和密碼放在JSON文件中,前端就發送到后端了? 有人知道該怎么做嗎?

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

您有很多選擇,所有這些都需要很多代碼,但是大多數選項已經在Internet上很好地實現了,因此您只需要使其適合您的需求即可。 您可以采用最簡單的方法,例如http會話,JSON令牌或JWT(JSON Web令牌)或其他方法。

HTTP會話肯定是最容易設置的,並且Spring Security已經開箱即用地對其提供了很好的支持。

在這個baeldung網站 (即使它使用XML配置)和這個網站 (使用Java配置)上找到了很大的幫助。

要全面了解Spring安全性是如何工作的, 此鏈接對我有很大幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM