<i id='b10iX'><tr id='b10iX'><dt id='b10iX'><q id='b10iX'><span id='b10iX'><b id='b10iX'><form id='b10iX'><ins id='b10iX'></ins><ul id='b10iX'></ul><sub id='b10iX'></sub></form><legend id='b10iX'></legend><bdo id='b10iX'><pre id='b10iX'><center id='b10iX'></center></pre></bdo></b><th id='b10iX'></th></span></q></dt></tr></i><div id='b10iX'><tfoot id='b10iX'></tfoot><dl id='b10iX'><fieldset id='b10iX'></fieldset></dl></div>
  • <small id='b10iX'></small><noframes id='b10iX'>

      <bdo id='b10iX'></bdo><ul id='b10iX'></ul>

        <legend id='b10iX'><style id='b10iX'><dir id='b10iX'><q id='b10iX'></q></dir></style></legend>

        <tfoot id='b10iX'></tfoot>

        AngularJS向路由添加授权

        时间:2023-10-21

            • <tfoot id='sSlVt'></tfoot>

              <small id='sSlVt'></small><noframes id='sSlVt'>

                <tbody id='sSlVt'></tbody>
              <i id='sSlVt'><tr id='sSlVt'><dt id='sSlVt'><q id='sSlVt'><span id='sSlVt'><b id='sSlVt'><form id='sSlVt'><ins id='sSlVt'></ins><ul id='sSlVt'></ul><sub id='sSlVt'></sub></form><legend id='sSlVt'></legend><bdo id='sSlVt'><pre id='sSlVt'><center id='sSlVt'></center></pre></bdo></b><th id='sSlVt'></th></span></q></dt></tr></i><div id='sSlVt'><tfoot id='sSlVt'></tfoot><dl id='sSlVt'><fieldset id='sSlVt'></fieldset></dl></div>
              <legend id='sSlVt'><style id='sSlVt'><dir id='sSlVt'><q id='sSlVt'></q></dir></style></legend>
                <bdo id='sSlVt'></bdo><ul id='sSlVt'></ul>

                  本文介绍了AngularJS向路由添加授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何向 AngularJS 和 ui.router 添加授权?我正在使用 modulg ng-oauth https://github.com/andreareginato/oauth-ng

                  How can I add authorization to AngularJS and ui.router? I'm using the modulg ng-oauth https://github.com/andreareginato/oauth-ng

                  我可以使用页面中的以下示例 http://andreareginato.github.io/oauth-ng/?

                  Can I use the following examples from the page http://andreareginato.github.io/oauth-ng/?

                  $scope.$on('oauth:login', function(event, token) {
                    console.log('Authorized third party app with token', token.access_token);
                  });
                  
                  $scope.$on('oauth:logout', function(event) {
                    console.log('The user has signed out');
                  });
                  
                  $scope.$on('oauth:loggedOut', function(event) {
                    console.log('The user is not signed in');
                  });
                  
                  $scope.$on('oauth:denied', function(event) {
                    console.log('The user did not authorize the third party app');
                  });
                  
                  $scope.$on('oauth:expired', function(event) {
                    console.log('The access token is expired. Please refresh.');
                  });
                  
                  $scope.$on('oauth:profile', function(profile) {
                    console.log('User profile data retrieved: ', profile);
                  });
                  

                  谢谢,西蒙

                  推荐答案

                  你可以像这样创建一些常量角色:

                  You could create some constant roles like this:

                  .constant('USER_ROLES', {
                      ALL: '*', //@unused
                      ADMIN: 'ROLE_ADMIN';
                      USER: 'ROLE_USER',
                      ANONYMOUS: 'ROLE_ANONYMOUS' 
                  })
                  

                  将此自定义数据/常量添加到您的状态:

                  Add this custom data/constants to your states:

                  $stateProvider.state('myapp.admin', {
                      url: '/admin',
                      .....
                      data : {
                          authorizedRoles : [USER_ROLES.ADMIN] //Thes
                      }
                  }
                  

                  因此,当您对这些角色进行身份验证并从数据库中检索这些角色时,您可以将其存储在您的用户对象和会话中,以便最终在路由更改时进行验证...

                  So when you authenticate and retrieve these roles from your database you can store this in your user object and session so you can eventually verify this when a route changes...

                  在您的身份验证服务中(除了登录、注销等),您可以添加以下方法.

                  In your auth service (apart from logging in, logging out etc...) you add the following methods.

                  isAuthenticated: function () {
                      return session.hasSession();
                  },
                  
                  isAuthorized: function (authorizedRoles) {
                      if (!angular.isArray(authorizedRoles)) {
                          authorizedRoles = [authorizedRoles];
                      }
                  
                      var roles = session.roles();
                  
                      var roleIncluded = roles.some(function (role) {
                          return (authorizedRoles.indexOf(role) != -1);
                      });
                  
                      return (session.hasSession() && roleIncluded);
                  },
                  

                  因此,当您更改应用程序中的路由时,.run 会发生块验证,并且可以进行预防.

                  So when you change the route in the applications .run block validation occurs and prevention is possible.

                  $rootScope.$on('$stateChangeStart', function (event, next) {
                      if (authService.isAuthenticated()) {
                          if (next.data.authorizedRoles === null) {
                              handle();
                          }
                          if (!authService.isAuthorized(next.data.authorizedRoles)) {
                              handle();
                          }
                      } else {
                          handle();
                      }
                  }
                  

                  当然这只是一个例子,请记住还有其他解决方案.

                  Ofcourse this is just an example and bear in mind there are other solutions.

                  这篇关于AngularJS向路由添加授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:简单的 ui-routes,请解释一下 下一篇:错误:Angular Ui 路由器状态更改时超出了最大调用堆栈大小

                  相关文章

                1. <legend id='CiuD8'><style id='CiuD8'><dir id='CiuD8'><q id='CiuD8'></q></dir></style></legend>

                  <small id='CiuD8'></small><noframes id='CiuD8'>

                  <i id='CiuD8'><tr id='CiuD8'><dt id='CiuD8'><q id='CiuD8'><span id='CiuD8'><b id='CiuD8'><form id='CiuD8'><ins id='CiuD8'></ins><ul id='CiuD8'></ul><sub id='CiuD8'></sub></form><legend id='CiuD8'></legend><bdo id='CiuD8'><pre id='CiuD8'><center id='CiuD8'></center></pre></bdo></b><th id='CiuD8'></th></span></q></dt></tr></i><div id='CiuD8'><tfoot id='CiuD8'></tfoot><dl id='CiuD8'><fieldset id='CiuD8'></fieldset></dl></div>
                    <bdo id='CiuD8'></bdo><ul id='CiuD8'></ul>
                  1. <tfoot id='CiuD8'></tfoot>