2016-11-08 42 views
0

使用AngularJS和Spring Security,當身份驗證失敗時,重定向到登錄頁面error=true。我需要在登錄頁面(AngularJS)上顯示此錯誤。我不知道該怎麼做。無法檢索關聯的錯誤並在登錄頁面中顯示

下面是authentication-failure-urlssecurity-context.xml聲明:

<security:form-login login-page="/login" 
       login-processing-url="/authenticate" 
       authentication-failure-url="/login?error=true" 
       username-parameter="username" password-parameter="password"/> 

一些能請告訴我如何檢索錯誤,並顯示登錄頁面上?

JS

angular.module('app.services') 
.service('AuthenticateService'‌​,['$http','$location‌​',function($http,$lo‌​cation) 
{ 
    return 
     {  
     login : function(uname, pword) 
       { 
       var data = "username="+uname+"&password="+pword; 
       return $http.post("authenticate",data, 
         { 
          headers: { "Content-Type": "application/x-www-form-urlencoded" } }) 
          .then(function (response) 
          { 
          console.log(response.status); 
          },function(error) 
          { 
          console.log(error.status); 
          }); 
          } 
        } 
}]); 

回答

0

當您的登錄嘗試失敗,你重定向到/login/error/true

然後,你應該有你的angularJS與你想要的部分HTML到URL路由以顯示一個角度控制器,該角色控制器將處理或對該URL執行適當的操作。

例如,讓我們說,你的路由文件看起來這樣:

var myApp = angular.module("myApp", [ "ngRoute" ]); 

myApp.config(function ($routeProvider) { 

    $routeProvider 
     .when("/login/error/:errValue", 
     {controller: "LoginController", templateUrl: "/partials/login.html"}) 
    }); 

現在,所有你需要做的就是對的LoginController壽檢查:errValue是真還是假。並根據該值採取行動。

with wrong credentials, I tried checking the value of errValue in the controller, but its says undefined 

    Below are the routes declared 

    $stateProvider 
      .state("/login",{ //this for login page 
      url:'/login', 
      templateUrl:'./resources/js/baseapp/ContactLogin/views/contactLogin.html', 
      controller:'contactLoginCtrl' 
     }) 
     .state("/home",{ 
      url:'/home',  // this is for home page 
      templateUrl:'./resources/js/baseapp/ContactHome/views/ContactHome.html', 
      controller:'contactHomeCtrl' 
     }) 
     .state("/login:error/:errValue",{ // this is for login error page 
      url:'/home', 
      templateUrl:'./resources/js/baseapp/ContactLogin/views/contactLogin.html', 
      controller:'contactLoginCtrl' 
     }) 
      $urlRouterProvider.otherwise("/login");