[英]Unable to get username and password from angularjs to spring security
[英]AngularJS and Spring backend - Obtaining password from database as user.password in AngularJS
因此我在用戶登錄時調用Login函數。此函數調用UserService.GetByEmail,它執行GET HTTP請求,該請求從數據庫中獲取User,如果有用戶在登錄時輸入了電子郵件,則將User作為響應返回。 之后,我使用if(user!== null && user.password === password){部分進行身份驗證。 但是,當我查看控制台輸出時,確實有一個用於用戶變量的對象,但是沒有供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);
});
}
然后這是后端我的UserController的一部分。
@RequestMapping(value = "/user/authenticate", method = RequestMethod.POST)
public ResponseEntity<Void> authenticateUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
}
我不確定如何在后端進行身份驗證。 為此需要采取哪些步驟?
有幾件事:
相反,如果HTTP狀態代碼為200(或其他2xx代碼,具體取決於后端),則應該認為身份驗證成功。 通常,這意味着如果您輸入promise的then()
子句,則登錄必須已成功,因為4xx代碼將被映射到失敗並通過catch()
報告。
您不應該以任何形式向客戶端發送密碼。 如果您使用的是Spring Security,則需要在服務器上調用登錄處理程序。
您應該考慮使用類似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.