java - 如何使用@RestController登录并将对象发送到angularjs

标签 java json angularjs spring request-mapping

我在弄清楚如何使用 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 吗?

这安全吗?

现在 Angular 将如何处理所有用户属性? (我建议我可以从 json 中检索它)

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

最佳答案

从 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());
}

请引用

关于java - 如何使用@RestController登录并将对象发送到angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32713157/

相关文章:

javascript - Protractor - 通过 Protractor 上传文件/运行exe

java - com.sun.jdi.InvocationException 发生调用方法

java - Android 应用程序进行 JSON 调用从服务器获取意外数据,但相同的调用在浏览器中有效

java - 在 Java 中使用链式键获取嵌套 JSON 值

java - 使用 Spring MVC 自定义 JSON 响应

angularjs - 如何 $state.go()

javascript - $http.get 之后 View 未更新

java - 平滑渐变背景动画java

java - 富人脸树问题

java - 为什么这个 Maven 配置文件没有运行?