Angular JS和Spring Boot,认证后获取User

Angular JS and SpringBoot, get User after authentication

我想在登录我的应用程序后让用户创建自己的帐户。例如,我希望他的 Id 将他重定向到页面:'/user-cars/:userId'。我正在使用 Angular JS 和 SpringBoot。我不知道该怎么做。下面是我的代码:

Config.js

angular.module('app')
.config(function ($routeProvider, $httpProvider) {
    $routeProvider
        .when('/users', {
            templateUrl: 'app/components/users/list/userList.html',
            controller: 'UserListController',
            controllerAs: 'ctrl'
        })
        .when('/user-add', {
            templateUrl: 'app/components/users/edit/userEdit.html',
            controller: 'UserEditController',
            controllerAs: 'ctrl'
        })

        .when('/user-cars/:userId', {
            templateUrl: 'app/components/users/userCars/carList.html',
            controller: 'CarListController',
            controllerAs: 'ctrl'
        })
        .when('/user-login',{
            templateUrl: 'app/components/users/login/login.html',
            controller: 'AuthenticationController',
            controllerAs: 'ctrl'
        })

        .otherwise({
            redirectTo: '/',
            controller: 'HomeController',
            controllerAs: 'ctrl'
        });
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
});

authService.js

angular.module('app')
.constant('LOGIN_ENDPOINT', '/user-login')
.constant('LOGOUT_ENDPOINT', '/user-logout')
.service('AuthenticationService', function($http, LOGIN_ENDPOINT, LOGOUT_ENDPOINT) {
    this.authenticate = function(credentials, successCallback) {
        var authHeader = {Authorization: 'Basic ' + btoa(credentials.email+':'+credentials.password)};
        var config = {headers: authHeader};
        $http
            .post(LOGIN_ENDPOINT, {
            }, config)
            .then(function success(res) {
                $http.defaults.headers.post.Authorization = authHeader.Authorization;
                successCallback();
            }, function error(reason) {
                console.log('Login error');
                console.log(reason);
            });
    }
    this.logout = function(successCallback) {
        delete $http.defaults.headers.post.Authorization;
            successCallback();
    }

});

authController.js

angular.module('app')
.controller('AuthenticationController', function($rootScope, $location, AuthenticationService) {
    var vm = this;
    vm.credentials = {};
    var loginSuccess = function() {
        $rootScope.authenticated = true;
        $location.path('/');
    }
    vm.login = function() {
        AuthenticationService.authenticate(vm.credentials, loginSuccess);
    }
    var logoutSuccess = function() {
        $rootScope.authenticated = false;
        $location.path('/');
    }
    vm.logout = function() {
        AuthenticationService.logout(logoutSuccess);
    }
})

一切正常,我从后端获得了主体,但现在我不知道如何使用它并获得用户

RestController.java`

@RestController
public class AuthenticationController { 
    @RequestMapping("/user-login")
    @ResponseBody
    public Principal login(Principal user)
    {
        return user;
    }`

授权有效,我拿到了校长,但我不知道我和他有什么关系。

根据您的 UserDetails 实现,您可以获得填充的用户详细信息。默认 implementation 只有用户名和密码,可以将其他详细信息添加到自定义实现中。

示例来自 Spring reference documentation

Authentication auth = httpServletRequest.getUserPrincipal();
// assume integrated custom UserDetails called MyCustomUserDetails
// by default, typically instance of UserDetails
MyCustomUserDetails userDetails = (MyCustomUserDetails) auth.getPrincipal();
String firstName = userDetails.getFirstName();
String lastName = userDetails.getLastName();

浏览评论,默认实现是 UserDetails

针对您的目的自定义实施的示例是

public class MyCustomUserDetails implements UserDetails{
 // Have the id field declared
 private String userId;
 // ...getters and setters

}

可以在 this SO 问答中找到完整的示例。