[英]If I refresh the page when I'm logged in, the Guard redirects me to the login page
[英]angularjs When I refresh my page it redirects me to login page
嗨,在设置Spring Security之后,登录对我来说很有效,但是当我刷新任何页面时,它将重定向到登录页面。 我该如何摆脱它。 这是我的代码
config.js
'use strict';
var app = angular.module('MyApp', [ 'ngResource', 'ngRoute',
'myAppControllers', 'ngAnimate', 'ui.bootstrap', 'ngCookies' ]);
app.config(function($routeProvider) {
$routeProvider.when('/products/:idCat', {
templateUrl : '../views/partials/products.html',
controller : 'ProductsController'
}).when('/products', {
templateUrl : '../views/partials/allProducts.html',
controller : 'ProductsController'
}).when('/supprimer', {
templateUrl : '../views/partials/delete.html',
}).when('/update', {
templateUrl : '../views/partials/update.html',
controller : 'UserController'
}).when('/client', {
templateUrl : '../views/partials/client.html',
controller : 'ClientController'
}).when('/agences', {
templateUrl : '../views/partials/agences.html',
controller : 'MapController'
}).when('/login', {
templateUrl : '../views/partials/login.html',
controller : 'mainController'
}).when('/', {
templateUrl : '../views/partials/Home.html',
})
$routeProvider.otherwise('/');
});
app.run(function($route, $rootScope, $http, $location, $cookieStore,
$anchorScroll) {
$rootScope.authenticated = false;
$rootScope.$on('$routeChangeStart', function() {
var originalPath = $location.url();
if ($rootScope.authenticated == false) {
$location.path('/login');
} else {
$rootScope.redirectUrl = angular.copy($location.url());
var user = $cookieStore.get('user');
if (user !== undefined) {
$rootScope.user = user;
$http.defaults.headers.common['X-Auth-Token'] = user.token;
$location.url(originalPath);}})
});
我的app.js中我有我的控制器ang调用以验证服务
myAppControllers
.controller(
'mainController',
function($scope, $rootScope, loginService, ProductService,
$location, $cookies, $cookieStore, $http) {
$rootScope.salesAgent = true;
$scope.loggedIn = false;
$rootScope.hasRole = function(role) {
if ($rootScope.user === undefined) {
return false;
}
if ($rootScope.user.authorities === undefined) {
return false;
}
for (var i = 0; i < $rootScope.user.authorities.length; i++) {
if ($rootScope.user.authorities[i].authority == role)
return true;
}
return false;
};
$scope.logout = function() {
console.log("Testtttttttttt");
delete $rootScope.user;
delete $http.defaults.headers.common['X-Auth-Token'];
$cookieStore.remove('user');
$location.url("/login");
};
$scope.authenticate = function() {
loginService
.authenticate($scope.username,
$scope.password)
.then(
function(user) {
$rootScope.user = user;
$http.defaults.headers.common['X-Auth-Token'] = user.token;
$cookieStore.put('user',
user);
if ($rootScope.redirectUrl != null
&& $rootScope.redirectUrl
.indexOf('/') == -1)
$location
.url($rootScope.redirectUrl);
else {
$rootScope.authenticated = true;
$location.path("/");
}
$rootScope.redirectUrl = null;
$rootScope.redirectStatus = null;
}),
function err(data) {
if ($rootScope.redirectStatus == 401)
}
};});
我很高兴您的帮助!
检查cookie是否存在,作为run
第一步
app.run(function($route, $rootScope, $http, $location, $cookieStore, $anchorScroll) {
var user = $cookieStore.get('user');
if (user !== undefined) {
// set up X- header and other variables here
刷新后,您将在$ rootScope中存储经过身份验证的值,该值将变为空,而不是将$ cookieStore用于angular 1.2或更低版本,或将$ cookies用于angular 1.2或更高版本。 您正在为用户使用$ cookieStore,对经过身份验证的用户也是如此
$cookieStore.put('authenticated',$rootScope.authenticated);
另一个选择是将身份验证值存储在localStorage中:
localStorage.setItem ('session.authenticated', true);
然后,您可以获取身份验证值,如下所示:
localStorage.getItem("session.authenticated") || false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.