oauth2.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
  6. <title>Knife4j-OAuth2</title>
  7. <script src="axios.min.js"></script>
  8. </head>
  9. <body>
  10. <script type="text/javascript">
  11. (function(){
  12. function OAuth2(url){
  13. this.url=url;
  14. this.code=null;
  15. this.accessToken=null;
  16. this.tokenType=null;
  17. this.state=null;
  18. //缓存在localStorage中的对象
  19. this.cacheValue=null;
  20. }
  21. OAuth2.prototype.init=function(){
  22. var local=this.url;
  23. this.code=this.getKey("code",local,"");
  24. this.accessToken=this.getKey("access_token",local,"");
  25. this.tokenType=this.getKey("token_type",local,"Bearer");
  26. this.state=this.getKey("state",local);
  27. if(window.localStorage){
  28. var value=window.localStorage.getItem(this.state);
  29. if(this.strNotBlank(value)){
  30. this.cacheValue=JSON.parse(value);
  31. }
  32. }
  33. }
  34. OAuth2.prototype.auth=function(){
  35. if(this.strNotBlank(this.code)){
  36. this.authorizationCode();
  37. }else{
  38. this.implicit();
  39. }
  40. }
  41. OAuth2.prototype.getKey=function(key,str,defaultValue){
  42. var reg=new RegExp(".*?"+key+"=(.*?)(&.*)?$","ig");
  43. var val=defaultValue;
  44. if(reg.test(str)){
  45. val=RegExp.$1;
  46. }
  47. return val;
  48. }
  49. OAuth2.prototype.strNotBlank=function(str){
  50. var flag = false;
  51. if ( str != undefined &&str != null &&str != "") {
  52. flag = true;
  53. }
  54. return flag;
  55. }
  56. OAuth2.prototype.implicit=function(){
  57. this.cacheValue.accessToken=this.tokenType+" "+this.accessToken;
  58. this.cacheValue.tokenType=this.tokenType;
  59. this.cacheValue.granted=true;
  60. window.localStorage.setItem(this.state,JSON.stringify(this.cacheValue))
  61. window.close();
  62. }
  63. OAuth2.prototype.authorizationCode=function(){
  64. var that=this;
  65. console.log(this.cacheValue);
  66. var url=this.cacheValue.tokenUrl;
  67. var params={
  68. "grant_type":"authorization_code",
  69. "code":this.code,
  70. "redirect_uri":decodeURIComponent(this.cacheValue.redirectUri),
  71. "client_id":this.cacheValue.clientId,
  72. "client_secret":this.cacheValue.clientSecret
  73. }
  74. let instance=axios.create();
  75. let requestConfig={
  76. url: url,
  77. method: 'post',
  78. timeout: 0,
  79. //此data必传,不然默认是data:undefined,https://github.com/axios/axios/issues/86
  80. //否则axios会忽略请求头Content-Type
  81. data: null,
  82. params:params
  83. }
  84. instance.request(requestConfig).then(res=>{
  85. let data=res.data;
  86. if(data!=null&&data!=undefined) {
  87. that.cacheValue.accessToken=data.token_type+" "+data.access_token;
  88. that.cacheValue.tokenType=data.token_type;
  89. that.cacheValue.granted=true;
  90. window.localStorage.setItem(that.state,JSON.stringify(that.cacheValue))
  91. window.close();
  92. }
  93. })
  94. }
  95. var oauth=new OAuth2(window.location.href);
  96. oauth.init();
  97. oauth.auth();
  98. })()
  99. </script>
  100. </body>
  101. </html>