简体   繁体   English

AngularJS和Spring后端-在AngularJS中以user.password的身份从数据库获取密码

[英]AngularJS and Spring backend - Obtaining password from database as user.password in AngularJS

so I am calling Login function when the user logs in. This function calls UserService.GetByEmail, which does a GET HTTP request that fetches User from database and returns the User as a response if there's a User with email typed in login. 因此我在用户登录时调用Login函数。此函数调用UserService.GetByEmail,它执行GET HTTP请求,该请求从数据库中获取User,如果有用户在登录时输入了电子邮件,则将User作为响应返回。 After that, I do the authentication with if (user !== null && user.password === password) { part. 之后,我使用if(user!== null && user.password === password){部分进行身份验证。 However, when I look at console output, I do have an Object for user variable, but I have nothing for user.password to compare with password. 但是,当我查看控制台输出时,确实有一个用于用户变量的对象,但是没有供user.password与密码进行比较的对象。 How do I put the User password from response into user.password? 如何将响应中的用户密码放入user.password?

(function () {
'use strict';

angular
    .module('app')
    .factory('AuthenticationService', AuthenticationService);

AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService'];
function AuthenticationService($http, $cookieStore, $rootScope, $timeout, UserService) {
    var service = {};

    service.Login = Login;
    service.SetCredentials = SetCredentials;
    service.ClearCredentials = ClearCredentials;

    return service;


    function Login(email, password, callback) {

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

    }

Then here is part of my UserController in the backend. 然后这是后端我的UserController的一部分。

@RequestMapping(value = "/user/authenticate", method = RequestMethod.POST)
public ResponseEntity<Void> authenticateUser(@RequestBody User user,    UriComponentsBuilder ucBuilder) {

}

I'm not sure how I should authenticate in the backend. 我不确定如何在后端进行身份验证。 What are the steps needed to do this? 为此需要采取哪些步骤?

There's a few things: 有几件事:

  1. This cannot be all the code involved: it's not clear what the UserService object or AuthenticationService factory function are. 这不能是所有涉及的代码:还不清楚UserService对象或AuthenticationService工厂功能是什么。
  2. Moreover one expects that you would not have a password to compare with anyway (that be a bit of a security hole). 此外,人们希望您没有密码可以与之进行比较(这有点安全漏洞)。

Instead, authentication should be considered successful if the HTTP status code is 200 (or other 2xx codes depending on the backend). 相反,如果HTTP状态代码为200(或其他2xx代码,具体取决于后端),则应该认为身份验证成功。 Ordinarily, this means that if you enter the then() clause of the promise the login must have been successful, because 4xx codes would have been mapped to failures and reported through catch() instead. 通常,这意味着如果您输入promise的then()子句,则登录必须已成功,因为4xx代码将被映射到失败并通过catch()报告。

You shouldn't be sending password in any form to the client. 您不应该以任何形式向客户端发送密码。 If you're using Spring Security, you need to call the login handler on the server. 如果您使用的是Spring Security,则需要在服务器上调用登录处理程序。

You should look into using something like JWT instead (read more here https://www.toptal.com/java/rest-security-with-jwt-spring-security-and-java ) or if you really need to be using form based security for some reason you can login to the server by using this block of code. 您应该考虑使用类似JWT的东西(在此处了解更多信息https://www.toptal.com/java/rest-security-with-jwt-spring-security-and-java ),或者是否真的需要使用表单基于某种原因的安全性,您可以使用此代码块登录服务器。

this.login = function (username, password, rememberMe) {
if (rememberMe === undefined) rememberMe = false;
return $http.post(
  '/j_spring_security_check',
  $.param({
    j_username: username,
    j_password: password,
    j_remember: rememberMe
  }),
  {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'X-Requested-With': 'XMLHttpRequest'
    }
  }
).then(_.bind(function (response) {
  if (response.data.success === true) {
    //something to happen on login
  }
  return $q.reject(response);
}, this));
};

this.logout = function () { 
  $http.get('/j_spring_security_logout').then(_.bind(function () {
    //something to happen on logout
  }, this));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM