在被maven,cas,tomcat各种贱人就是矫情的虐了好几天之后,终于跑通了demo,哈哈哈哈哈哈哈~

在这里详细记录一下,给和我一样连maven都不会的小白一点福利,同时欢迎大神指正。

首先上最好的参考资料:http://stackoverflow.com/questions/22625368/working-java-rest-client-example-to-access-cas-rest-api

这个大神讲的非常清楚了,只要跟着他的步骤一定是可以的,但是。。。大神一句话,通常我要花一天来理解。

另外还有官方的参考资料:

https://wiki.jasig.org/display/CASUM/Best+Practice+-+Setting+Up+CAS+Locally+using+the+Maven+WAR+Overlay+Method

https://wiki.jasig.org/display/casum/restful+api

这篇博客的基础是单点登录系统CAS服务器端搭建及实现用户名密码由MYSQL数据库验证这篇文章,在调用rest ful API之前,首先要保证:

1.tomcat配置好,支持https,检验的标准是通过https://localhost:8443/可以访问到tomcat;

2.知道怎么部署cas官网上的打好包的server,检验的标准是通过https://localhost:8443/cas/login可以访问到cas server;

3.知道怎么将server和MYSQL连接使用,检验的标准是可以通过MYSQL中的用户名,密码登录cas server。

以上三点上述博客里都有详细步骤。

以下是restful API使用的详细步骤:

要调用restful API,首先要在pom.xml里面加入restful API的依赖,然后重新编译出来一个cas server的war包。

1.安装Maven

具体步骤我就不详述了,灰常简单,参考:http://blog.csdn.net/kenhins/article/details/13298015,需要把本地仓库配置好。

2.把下面的pom.xml拷贝到一个文件夹里面(随便一个),命名为pom.xml,这个pom.xml参考了http://pastie.org/4064726#22,30,78,81https://wiki.jasig.org/display/CASUM/Best+Practice+-+Setting+Up+CAS+Locally+using+the+Maven+WAR+Overlay+Method

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  5.  
  6. <parent>
  7. <groupId>org.jasig.cas</groupId>
  8. <artifactId>cas-server</artifactId>
  9. <version>3.4.12</version>
  10. </parent>
  11.  
  12. <modelVersion>4.0.0</modelVersion>
  13. <groupId>h.usm.my</groupId>
  14. <artifactId>cas</artifactId>
  15. <packaging>war</packaging>
  16. <version>1.0</version>
  17. <name>HUSM CAS Web Application</name>
  18.  
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.jasig.cas</groupId>
  22. <artifactId>cas-server-webapp</artifactId>
  23. <version>${cas.version}</version>
  24. <type>war</type>
  25. <scope>runtime</scope>
  26. </dependency>
  27.  
  28. <dependency>
  29. <groupId>org.jasig.cas</groupId>
  30. <artifactId>cas-server-support-jdbc</artifactId>
  31. <version>${cas.version}</version>
  32. </dependency>
  33.  
  34. <dependency>
  35. <groupId>org.jasig.cas</groupId>
  36. <artifactId>cas-server-integration-restlet</artifactId>
  37. <version>${cas.version}</version>
  38. <type>jar</type>
  39. <exclusions>
  40. <exclusion>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-web</artifactId>
  43. </exclusion>
  44. </exclusions>
  45. </dependency>
  46.  
  47. <dependency>
  48. <groupId>org.hibernate</groupId>
  49. <artifactId>hibernate-core</artifactId>
  50. <version>${hibernate.core.version}</version>
  51. <type>jar</type>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.hibernate</groupId>
  55. <artifactId>hibernate-entitymanager</artifactId>
  56. <version>3.6.0.Final</version>
  57. </dependency>
  58. </dependencies>
  59.  
  60. <properties>
  61. <cas.version>3.4.12</cas.version>
  62. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  63. </properties>
  64.  
  65. <repositories>
  66. <repository>
  67. <id>ja-sig</id>
  68. <url>http://oss.sonatype.org/content/repositories/releases</url>
  69. </repository>
  70. </repositories>
  71.  
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <artifactId>maven-war-plugin</artifactId>
  76. <configuration>
  77. <warName>cas</warName>
  78. </configuration>
  79. </plugin>
  80. </plugins>
  81. </build>
  82. </project>

3.cd到pom.xml所在的路径下,然后执行mvn clean package,如下图所示:

然后就会在pom.xml所在的路径下得到一个target文件夹,里面的cas.war就是我们新打包好的cas server。

4.把生成的cas.war拷贝到\apache-tomcat-7.0.57\webapps下,启动tomcat,如果没有报错(一个小tip,可从\apache-tomcat-7.0.57\logs\localhost.yy-mm-dd.log中查看具体错误),说明我们的cas.war是可以用的。这个时候访问https://localhost:8443/cas/login即可跳转到cas的登录页面了。

5.接下来配置cas server和MYSQL的连接,使得我们可以用MYSQL数据库里面的用户名和密码登录。详细步骤我就省略了,参见上面提到的单点登录系统CAS服务器端搭建及实现用户名密码由MYSQL数据库验证这篇文章。

上述5步以后,我们可以从https://localhost:8443/cas/login访问到cas server的登录页面,并且可以用MYSQL中的用户名和密码登录。

6.在G:\Lab\CAS\apache-tomcat-7.0.57\webapps\cas\WEB-INF\web.xml中添加一个servlet:

  1. <!--add a servlet-->
  2. <servlet>
  3. <servlet-name>restlet</servlet-name>
  4. <servlet-class>com.noelios.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
  5. <load-on-startup>1</load-on-startup>
  6. </servlet>
  7.  
  8. <servlet-mapping>
  9. <servlet-name>restlet</servlet-name>
  10. <url-pattern>/v1/*</url-pattern>
  11. </servlet-mapping>

保存后打开:https://localhost:8443/cas/v1/tickets会出现如下界面:

说明server端的restful api配置好了。

7. 接下来新建一个eclipse工程,我这里叫做CasTest,然后新建一个类Client.java,内容如下(完整的工程见附录):

  1. package cas;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.net.URLEncoder;
  12.  
  13. import javax.net.ssl.HttpsURLConnection;
  14.  
  15. public class Client {
  16.  
  17. public static void main(String... args) throws Exception
  18. {
  19. String username ="test01";
  20. String password ="psw01";
  21. validateFromCAS(username,password);
  22. }
  23.  
  24. public static boolean validateFromCAS(String username, String password) throws Exception
  25. {
  26.  
  27. String url = "https://localhost:8443/cas/v1/tickets";
  28. try
  29. {
  30. HttpsURLConnection hsu = (HttpsURLConnection)openConn(url);
  31. String s = URLEncoder.encode("username","UTF-8") + "=" + URLEncoder.encode("test01","UTF-8");
  32. s+="&" +URLEncoder.encode("password","UTF-8") + "=" + URLEncoder.encode("psw01","UTF-8");
  33.  
  34. System.out.println(s);
  35. OutputStreamWriter out = new OutputStreamWriter(hsu.getOutputStream());
  36. BufferedWriter bwr = new BufferedWriter(out);
  37. bwr.write(s);
  38. bwr.flush();
  39. bwr.close();
  40. out.close();
  41.  
  42. String tgt = hsu.getHeaderField("location");
  43. System.out.println( hsu.getResponseCode());
  44. if(tgt != null && hsu.getResponseCode() == 201)
  45. {
  46. System.out.println(tgt);
  47.  
  48. System.out.println("Tgt is : " + tgt.substring( tgt.lastIndexOf("/") +1));
  49. tgt = tgt.substring( tgt.lastIndexOf("/") +1);
  50. bwr.close();
  51. closeConn(hsu);
  52.  
  53. String serviceURL = "http://localhost:8080/CasClient";
  54. String encodedServiceURL = URLEncoder.encode("service","utf-8") +"=" + URLEncoder.encode(serviceURL,"utf-8");
  55. System.out.println("Service url is : " + encodedServiceURL);
  56.  
  57. String myURL = url+ "/"+ tgt ;
  58. System.out.println(myURL);
  59. hsu = (HttpsURLConnection)openConn(myURL);
  60. out = new OutputStreamWriter(hsu.getOutputStream());
  61. bwr = new BufferedWriter(out);
  62. bwr.write(encodedServiceURL);
  63. bwr.flush();
  64. bwr.close();
  65. out.close();
  66.  
  67. System.out.println("Response code is: " + hsu.getResponseCode());
  68.  
  69. BufferedReader isr = new BufferedReader( new InputStreamReader(hsu.getInputStream()));
  70. String line;
  71. System.out.println( hsu.getResponseCode());
  72. while ((line = isr.readLine()) != null) {
  73. System.out.println( line);
  74. }
  75. isr.close();
  76. hsu.disconnect();
  77. return true;
  78.  
  79. }
  80. else
  81. {
  82. return false;
  83. }
  84.  
  85. }
  86. catch(MalformedURLException mue)
  87. {
  88. mue.printStackTrace();
  89. throw mue;
  90.  
  91. }
  92. catch(IOException ioe)
  93. {
  94. ioe.printStackTrace();
  95. throw ioe;
  96. }
  97.  
  98. }
  99.  
  100. static URLConnection openConn(String urlk) throws MalformedURLException, IOException
  101. {
  102.  
  103. URL url = new URL(urlk);
  104. HttpsURLConnection hsu = (HttpsURLConnection) url.openConnection();
  105. hsu.setDoInput(true);
  106. hsu.setDoOutput(true);
  107. hsu.setRequestMethod("POST");
  108. return hsu;
  109.  
  110. }
  111.  
  112. static void closeConn(HttpsURLConnection c)
  113. {
  114. c.disconnect();
  115. }
  116.  
  117. }

这段代码参考了http://www.dzone.com/snippets/cas-restful-java-client

要配置的地方有4个:

  1)21,22行的username和password,这里改成你数据库里面存放的username和password

  2)33,34行的username和password,这里改不改都行,因为它只是一行输出,改了比较好看吧。

  3)29行server存放ticket的地址,这里的localhost需要换成你的server所在的ip地址。

  4)56行的service,即client端的服务网址,理论上可以随便填,我这里的网址是我这篇单点登录系统CAS客户端demo里面建的client的工程,你可以换成百度什么的,我试过了。

8.根据自己的情况配置完上面4个地方后,直接运行工程,就有如下结果了:

可以看到我们成功的获取到了TGT和ST。

附件:casTest.zip

【Tech】CAS RESTful API使用笔记的更多相关文章

  1. Flask RESTful API搭建笔记

    之前半年时间,来到项目的时候,已经有一些东西,大致就是IIS+MYSQL+PHP. 所以接着做,修修补补,Android/iOS与服务器数据库交换用PHP, Web那边则是JS+PHP,也没有前后端之 ...

  2. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  3. RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  4. Restful API学习笔记

    之前关于这个概念在网上看了一些,看完似懂非懂,模模糊糊,发现专业术语或者说书面表达的形式对于理解这种十分抽象的概念还是低效了点. 书面文档方面看了以下几个: 理解本真的REST架构风格 1. 要深入理 ...

  5. Spring Boot 2.x 编写 RESTful API (六) 事务

    用Spring Boot编写RESTful API 学习笔记 Transactional 判定顺序 propagation isolation 脏读 不可重复读 幻读 不可重复读是指记录不同 (upd ...

  6. Spring Boot 2.x 编写 RESTful API (五) 单元测试

    用Spring Boot编写RESTful API 学习笔记 概念 驱动模块 被测模块 桩模块 替代尚未开发完毕的子模块 替代对环境依赖较大的子模块 (例如数据访问层) 示例 测试 Service @ ...

  7. Spring Boot 2.x 编写 RESTful API (四) 使用 Mybatis

    用Spring Boot编写RESTful API 学习笔记 添加依赖 <dependency> <groupId>org.mybatis.spring.boot</gr ...

  8. Spring Boot 2.x 编写 RESTful API (三) 程序层次 & 数据传输

    用Spring Boot编写RESTful API 学习笔记 程序的层次结构 相邻层级的数据传输 JavaBean 有一个 public 的无参构造方法 属性 private,且可以通过 get.se ...

  9. Spring Boot 2.x 编写 RESTful API (二) 校验

    用Spring Boot编写RESTful API 学习笔记 约束规则对子类依旧有效 groups 参数 每个约束用注解都有一个 groups 参数 可接收多个 class 类型 (必须是接口) 不声 ...

随机推荐

  1. python字符串连接的N种方式总结

    python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2python 新字符串连接语法:str1, str2奇怪的字符串方式:str1 str2 ...

  2. Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理

    /** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...

  3. Power Network - poj 1459 (最大流 Edmonds-Karp算法)

      Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24788   Accepted: 12922 Description A ...

  4. 没有Promise的时候自己处理复合异步请求

    function getList(options){ $.ajax(success:funciton(){ if(options.callback) options.callback.call(); ...

  5. OpenGL ES andoid学习————1

    package com.xhm.getaccount; import javax.microedition.khronos.egl.EGLConfig; import javax.microediti ...

  6. struts2之constant 讲解 (转)

    struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. stru ...

  7. Android开发:使用DialogFragment实现dialog自定义布局

    使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期. TestFragment的代码: public class TestFragment ex ...

  8. c++包含头文件好还是重新定义好

    A.h struct A { int a; int b; }; B.cpp 在B.cpp里面用到这个结构体 有两种方法 .自己定义一个一模一样的结构体 struct A { }; .包含A.h头文件 ...

  9. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  10. @tap的传参和对全局变量的修改 onTap方法的k-v参数同时传入;

    小程序wepy <view class="weui-panel__bd" @tap="onTap(e)" data-tapkey="itemNa ...