2017-07-28 83 views
-1

我有彈簧安全的彈簧web應用程序,我使用休息服務來訪問數據。我對此有兩個問題。如何使彈簧安全允許使用休息服務

首先,我不能讓Spring安全性允許我發送POST HTTP請求並向數據庫添加內容。我試圖允許這個請求,但春天的安全仍然拒絕訪問。

http.authorizeRequests().antMatchers("/services/rest/registerUser").permitAll(); 

第二個問題,保護休息服務的最佳做法是什麼?我絕對不想讓別人通過休息服務向數據庫發送數據。那麼允許從我的應用程序內部使用服務並限制其他每個休息請求的最佳方式是什麼?

+1

您需要啓用'permitAll'的匿名訪問才能工作。沒有它,你不會有一個認證對象,訪問仍然失敗。您可以忽略所有內容,但是這樣您也會丟失在請求中設置的安全標頭。 –

回答

0

您可以在WebSecurityConfigurerAdapter中使用configure(WebSecurity web)超載的方法,並執行以下操作;

public void configure(WebSecurity web) { 
    web.ignoring().antMatchers("/services/rest/registerUser"); 
} 

此外,如果你允許系統從您的REST API被放置在另一臺主機上訪問您的REST API,那麼你需要使用CORS標頭。

what is the best practice with securing rest services? I definitely don't want to let someone send data to database through rest services. So what is the best way to allow using services from inside of my app and restrict every other rest request? 

最廣泛使用和安全的保護REST Apis的方法是使用OAuth2。您可以使用Spring Security OAuth2項目來實現此目的。

另一種方法是使用JWT令牌,這也可以使用Spring Security和一些代碼來實現。

+0

謝謝你的信息。而我只是好奇,忽略Web請求確實有效,但是你有什麼想法,爲什麼我的代碼使用'permitAll()'不允許我發佈該請求? – wdc