0

我在弄清楚如何使用 springboot 在 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 中检索它)

关于如何解决这个问题的任何专业提示?

4

1 回答 1

1

从 AngularJS 你正在调用HTTP POST方法并且在 Spring 端你已经声明为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());
}

请参考

于 2015-09-22T09:33:39.767 回答