|
21 | 21 | var app = angular.module('myApp', ["ngResource","ngRoute","ngCookies"]); |
22 | 22 |
|
23 | 23 | app.controller('mainCtrl', function($scope,$resource,$http,$httpParamSerializer,$cookies) { |
24 | | - $scope.foo = {id:0 , name:"sample foo"}; |
25 | | - $scope.foos = $resource("http://localhost:8081/spring-security-oauth-resource/foos/:fooId",{fooId:'@id'}); |
| 24 | + $scope.foo = {id:0 , name:"sample foo"}; |
| 25 | + $scope.foos = $resource("foos/:fooId",{fooId:'@id'}); |
26 | 26 |
|
27 | | - $scope.getFoo = function(){ |
28 | | - $scope.foo = $scope.foos.get({fooId:$scope.foo.id}); |
29 | | - } |
30 | | - |
31 | | - $scope.data = {grant_type:"password", username: "", password: "", client_id: "fooClientIdPassword"}; |
32 | | - $scope.encoded = btoa("fooClientIdPassword:secret"); |
| 27 | + $scope.getFoo = function(){ |
| 28 | + $scope.foo = $scope.foos.get({fooId:$scope.foo.id}); |
| 29 | + } |
33 | 30 |
|
| 31 | + $scope.loginData = {grant_type:"password", username: "", password: "", client_id: "fooClientIdPassword"}; |
| 32 | + $scope.refreshData = {grant_type:"refresh_token", refresh_token:""}; |
| 33 | + |
34 | 34 | var isLoginPage = window.location.href.indexOf("login") != -1; |
35 | 35 | if(isLoginPage){ |
36 | | - if($cookies.get("access_token")){ |
| 36 | + if($cookies.get("access_token")){ |
37 | 37 | window.location.href = "index"; |
38 | | - }else{ |
39 | | - $http.defaults.headers.common.Authorization= 'Basic ' + $scope.encoded; |
40 | 38 | } |
41 | 39 | }else{ |
42 | | - if($cookies.get("access_token")){ |
43 | | - $http.defaults.headers.common.Authorization= 'Bearer ' + $cookies.get("access_token"); |
44 | | - }else{ |
45 | | - window.location.href = "login"; |
46 | | - } |
47 | | - |
| 40 | + if($cookies.get("access_token")){ |
| 41 | + $http.defaults.headers.common.Authorization= 'Bearer ' + $cookies.get("access_token"); |
| 42 | + }else{ |
| 43 | + refreshAccessToken(); |
| 44 | + } |
48 | 45 | } |
49 | 46 |
|
50 | 47 | $scope.login = function() { |
51 | | - var req = { |
| 48 | + $scope.obtainAccessToken($scope.loginData); |
| 49 | + } |
| 50 | + |
| 51 | + function refreshAccessToken(){ |
| 52 | + $http.get("refreshToken"). |
| 53 | + success(function(data, status, headers, config) { |
| 54 | + if(headers("refreshToken") && headers("refreshToken").length>0){ |
| 55 | + $scope.refreshData.refresh_token = headers("refreshToken"); |
| 56 | + $scope.obtainAccessToken($scope.refreshData); |
| 57 | + }else{ |
| 58 | + window.location.href = "login"; |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + $scope.obtainAccessToken = function(params){ |
| 64 | + var req = { |
52 | 65 | method: 'POST', |
53 | | - url: "http://localhost:8081/spring-security-oauth-server/oauth/token", |
54 | | - headers: { |
55 | | - "Authorization": "Basic " + $scope.encoded, |
56 | | - "Content-type": "application/x-www-form-urlencoded; charset=utf-8" |
57 | | - }, |
58 | | - data: $httpParamSerializer($scope.data) |
| 66 | + url: "oauth/token", |
| 67 | + headers: {"Content-type": "application/x-www-form-urlencoded; charset=utf-8"}, |
| 68 | + data: $httpParamSerializer(params) |
59 | 69 | } |
60 | 70 | $http(req).then( |
61 | | - function(data){ |
62 | | - $http.defaults.headers.common.Authorization= 'Bearer ' + data.data.access_token; |
63 | | - $cookies.put("access_token", data.data.access_token); |
64 | | - window.location.href="index"; |
65 | | - },function(){ |
66 | | - console.log("error"); |
67 | | - }); |
68 | | - } |
69 | | - |
| 71 | + function(data){ |
| 72 | + $http.defaults.headers.common.Authorization= 'Bearer ' + data.data.access_token; |
| 73 | + var expireDate = new Date (new Date().getTime() + (1000 * data.data.expires_in)); |
| 74 | + $cookies.put("access_token", data.data.access_token, {'expires': expireDate}); |
| 75 | + window.location.href="index"; |
| 76 | + },function(){ |
| 77 | + console.log("error"); |
| 78 | + window.location.href = "login"; |
| 79 | + }); |
| 80 | + } |
70 | 81 | }); |
71 | 82 | /*]]>*/ |
72 | 83 | </script> |
|
0 commit comments