测试cas,转发获取token
原创
2017-12-02
java

1.需要引入jquery
2.通过后台转发cas access-token
3.restful 风格,cas登录方法的解决方式。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">

  <head>
    <meta charset="UTF-8">
    <title>cas client</title>
    <script type="text/javascript" src="./jquery.min.js">
    </script>

  </head>

  <body>
    <table>
      <tr>
        <td>
          <a href="http://www.test.com:8443/cas/login?service=http://www.test.com:8020/test.html">
            <h4>第一部(获取code)</h4>
          </a>
        </td>
        <td>
          code: <input id="code" name="code" value="" style="width: 400px;" />
        </td>
      </tr>
      <tr>
        <td>
          <a onclick="getToken()" href="javascript:;">
            <h4>第二部access_token</h4>
          </a>
        </td>
        <td>
          token: <input id="token" name="token" value="" style="width: 900px;" />
        </td>
      </tr>
    </table>

    <!--<script type="text/javascript" src="./test.js" ></script>-->
    <script type="text/javascript">
      (function($) {
        var code = GetQueryString('ticket');
        console.log(code);
        $('#code').val(code);
      })(jQuery);

      //var url = "https://test.newtranx.com/admin/oauth2.0/access-token";
      var url = "http://www.test.com:8080/api/oauth2.0/access-token";
      //      var url = "https://plugin.newtranx.com:8003/adminsys/oauth2.0/access-token";
      //    var url = "https://test.newtranx.com:8000/usersys/oauth2.0/access-token";
      //    function GetQueryString(name) {
      //      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      //      var r = window.location.search.substr(1).match(reg);
      //      if(r != null) return unescape(r[2]);
      //      return null;
      //    }
      function GetQueryString(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
          results = regex
          .exec(location.search);
        return results == null ? null : decodeURIComponent(results[1]
          .replace(/\+/g, " "));
      }

      function getToken() {
        var code = GetQueryString('ticket');
        var params = {
          "code": code,
          "redirectUri": "http://www.test.com/test.html",
          "clientSecret": "test",
          "clientId": "test"
        }


        $.ajax({
          type: "post",
          url: url,
          data: params,
          success: function(e) {
            console.log(e.data[0].accessToken)
            $("#token").val(e.data[0].accessToken);
          },
          error: function(e) {
            console.log(e)
          }
        });
      }
    </script>

  </body>

</html>