2015-12-30 38 views
0

我在查找如何在前端管理身份驗證角色時遇到了一些問題。我想訪問認證角色並將其傳遞給根作用域,但它返回一個數組。Spring引導和angularjs中的可管理身份驗證角色

我想知道用戶爲每個角色添加一些特權的角色。 這怎麼辦?

如果我叫用戶/用戶

{"details":{"remoteAddress":"0:0:0:0:0:0:0:1","sessionId":"1F64CC142A715A53FFF4A9329E6B933D"},"authorities":[{"authority":"USER_ROLE"}],"authenticated":true,"principal":{"password":null,"username":"[email protected]","authorities":[{"authority":"USER_ROLE"}],"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"enabled":true},"credentials":null,"name":"[email protected]"} 

這是我的登錄控制器:

(function() { 
    'use strict'; 

    angular 
     .module('app') 
     .controller('LoginController', LoginController); 


    LoginController.$inject = ['$rootScope', '$scope', '$http', '$location', '$route','$localStorage']; 
    function LoginController($rootScope, $scope, $http, $location, $route,$localStorage) { 


     $scope.tab = function(route) { 
      return $route.current && route === $route.current.controller; 
     }; 


     var authenticate = function(credentials, callback) { 

      var headers = credentials ? { 
       authorization : "Basic " 
       + btoa(credentials.username + ":" 
        + credentials.password) 
      } : {}; 


      $http.get('user/', { 
       headers : headers 
      }).success(function(data) { 
       if (data.name) { 

        //This returns an array... 
        console.log(data.authorities); 

        console.log("Good Credentials"); 
        $rootScope.authenticated = true; 
       } else { 

        $rootScope.authenticated = false; 
       } 
       callback && callback($rootScope.authenticated); 
      }).error(function() { 
       $rootScope.authenticated = false; 
       callback && callback(false); 
      }); 

     }; 

     authenticate(); 

     $scope.credentials = {}; 
     $scope.login = function() { 
      authenticate($scope.credentials, function(authenticated) { 
       if (authenticated) { 
        console.log("Login succeeded"); 
        console.log($scope.credentials.username); 
        $location.path("/app/dashboard"); 
        $scope.error = false; 
        $rootScope.authenticated = true; 

        //Here we add all the variables we need to the session that are safe to be seen 
        //In the future we can replace this and load this in the splash screen every time the application loads so we are sure the 
        // variables are stored 



       } else { 
        console.log("Login failed") 
        $location.path("/login"); 
        $scope.error = true; 
        $rootScope.authenticated = false; 
       } 
      }) 
     }; 



     //Logout 
     $scope.logout = function() { 
      $http.post('logout', {}).success(function() { 
       $rootScope.authenticated = false; 
       $location.path("/"); 
      }).error(function(data) { 
       $location.path("/"); 
       console.log("Logout failed") 
       $rootScope.authenticated = false; 
      }); 
     } 







    } 

})(); 

這是由於Spring文檔中描述我的控制器

@RequestMapping("/user") 
public Principal user(@AuthenticationPrincipal Principal user) { 
    return user; 
} 

回答

1

@AuthenticationPrincipal會返回值爲Authentication.getPrincipal()。但是,如果您查看Authentication,您將看到用戶的權限是與主體分開訪問的,使用Authentication.getAuthorities()

您可以做幾件事情,修改您的自定義Principal以包含用戶的角色,或者除了控制器中的主體之外,還可以獲取權限並序列化它們。

您可以通過以下方式獲得當前認證:SecurityContextHolder.getContext().getAuthentication();