2015-09-22 56 views
0

我在使用springboot在angularjs中創建登錄表單時遇到了一些問題。如何使用@RestController登錄並將對象發送到angularjs

我可以註冊一個用戶並將數據發送到數據庫,但問題在我想登錄時開始。

在angularjs

我有這樣

function Login(username, password, callback) { 


     $http.post('/api/authenticate', { username: username, password: password }) 
      .success(function (response) { 
       callback(response); 
      }); 

    } 

功能是我能夠做到的,但可能豈不等於右:

@RequestMapping(value = "/authenticate/{id}",method = RequestMethod.GET) 
public User getUser(@PathVariable Integer id) { 

    return repo.findOne(id); 
} 

這給了我下面的JSON

{"id":2,"username":"jdoe","password":"$2a$10$5hgIyQr.K9wb8cXEyWGbROAU.rkYzd19vP7ajHpwp1KUYdShfcPn.","lastname":"doe","firstname":"john","customfield":"Hello there"} 

但現在我有以下問題和疑問:

如何通過轉到api/authenticate來檢查用戶名和密碼是否等於json的用戶名和密碼? (無{id})

我可以隱藏用戶的這個json嗎?

這是安全嗎?

現在所有的用戶怎麼會有房產? (我建議我可以從JSON檢索這個)

任何專業提示如何解決這個問題?

+0

也請考慮使用彈簧安全性,將是一個明智的選擇。否則發送用戶名/密碼到Controller驗證並返回UserId和authstatus作爲響應,並在會話中進行管理。在這裏,您必須自己處理所有事情,包括Logoit,授權等。 – M4ver1k

+0

我知道如何使用jsp創建自定義登錄春天的安全。但我如何用angularjs來管理這個。 ?這確實是一個完美的解決方案。但我該如何開始。 ? – Greg

+1

看看[this](https://spring.io/guides/tutorials/spring-security-and-angular-js/)。 – M4ver1k

回答

1

從AngularJS你調用HTTP POST方法和在春天你已宣佈爲HTTP GET,這是錯誤的。

正確的請求映射

@RequestMapping(value = "/api/authenticate",method = RequestMethod.POST, consumes = "application/json") 
@ResponseBody 
public User getUser(@RequestBody User user) { 
//here request body contains User POJO object's payload (JSON object) 

    //You are getting username from JSON, 
    //so you need to update your call to findOne method 
    return repo.findOne(user.getUserName()); 
} 

請參閱

+0

我會先看看@ M4ver1k說的,並首先研究Spring Security和Angular JS。但把你的問題標記爲解決問題,因爲這是解決這個問題的方法。感謝您的信息。 – Greg

+0

@ user3475722:不客氣。快樂編碼.... –

相關問題