我已经实现了一个演示客户端,它将用户名和密码作为 POST 发送到一个 REST Api,该 API 读取并执行一些身份验证逻辑。问题是在控制器中总是捕获为空(没有用户没有通过)。我尝试在 Chrome 的 Advanced Rest Client 中发送相同的数据,一切正常。
这是我的代码段:
角客户端:
function LoginCtrl($scope, $rootScope, $location, $http, LoginService) {
$scope.submit = function() {
LoginService.authenticate($.param({username: $scope.inputUsername, password: $scope.inputPassword}), function(user) {
$rootScope.user = user;
$http.defaults.headers.common['Authentication'] = user.token;
$location.path("/");
});
};
};
控制器java(第一次尝试):
public @ResponseBody UserTransfer authenticate(HttpServletRequest request) throws IOException {
//tried this one first but NULL this two params.
String username = request.getParam("username");
String password = request.getParam("password");
//somer logic
return new UserTransfer(userDetails.getUsername(), roles, TokenUtils.createToken(userDetails));
控制器java(第二次尝试):
public @ResponseBody UserTransfer authenticate(@RequestParam("username") String username, @RequestParam("password") String password) throws IOException {
//tried secondly this one but in this case the server responded with ERROR 400 - Bad Request
//somer logic
return new UserTransfer(userDetails.getUsername(), roles, TokenUtils.createToken(userDetails));
将这些数据从 Angular 传递到我的控制器的正确方法是什么?,我看不到错误...
感谢您的帮助!!!