在被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

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server</artifactId>
<version>3.4.12</version>
</parent> <modelVersion>4.0.0</modelVersion>
<groupId>h.usm.my</groupId>
<artifactId>cas</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>HUSM CAS Web Application</name> <dependencies>
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
</dependency> <dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-integration-restlet</artifactId>
<version>${cas.version}</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.core.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.0.Final</version>
</dependency>
</dependencies> <properties>
<cas.version>3.4.12</cas.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <repositories>
<repository>
<id>ja-sig</id>
<url>http://oss.sonatype.org/content/repositories/releases</url>
</repository>
</repositories> <build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>cas</warName>
</configuration>
</plugin>
</plugins>
</build>
</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:

<!--add a servlet-->
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>com.noelios.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>

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

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

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

 package cas;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import javax.net.ssl.HttpsURLConnection; public class Client { public static void main(String... args) throws Exception
{
String username ="test01";
String password ="psw01";
validateFromCAS(username,password);
} public static boolean validateFromCAS(String username, String password) throws Exception
{ String url = "https://localhost:8443/cas/v1/tickets";
try
{
HttpsURLConnection hsu = (HttpsURLConnection)openConn(url);
String s = URLEncoder.encode("username","UTF-8") + "=" + URLEncoder.encode("test01","UTF-8");
s+="&" +URLEncoder.encode("password","UTF-8") + "=" + URLEncoder.encode("psw01","UTF-8"); System.out.println(s);
OutputStreamWriter out = new OutputStreamWriter(hsu.getOutputStream());
BufferedWriter bwr = new BufferedWriter(out);
bwr.write(s);
bwr.flush();
bwr.close();
out.close(); String tgt = hsu.getHeaderField("location");
System.out.println( hsu.getResponseCode());
if(tgt != null && hsu.getResponseCode() == 201)
{
System.out.println(tgt); System.out.println("Tgt is : " + tgt.substring( tgt.lastIndexOf("/") +1));
tgt = tgt.substring( tgt.lastIndexOf("/") +1);
bwr.close();
closeConn(hsu); String serviceURL = "http://localhost:8080/CasClient";
String encodedServiceURL = URLEncoder.encode("service","utf-8") +"=" + URLEncoder.encode(serviceURL,"utf-8");
System.out.println("Service url is : " + encodedServiceURL); String myURL = url+ "/"+ tgt ;
System.out.println(myURL);
hsu = (HttpsURLConnection)openConn(myURL);
out = new OutputStreamWriter(hsu.getOutputStream());
bwr = new BufferedWriter(out);
bwr.write(encodedServiceURL);
bwr.flush();
bwr.close();
out.close(); System.out.println("Response code is: " + hsu.getResponseCode()); BufferedReader isr = new BufferedReader( new InputStreamReader(hsu.getInputStream()));
String line;
System.out.println( hsu.getResponseCode());
while ((line = isr.readLine()) != null) {
System.out.println( line);
}
isr.close();
hsu.disconnect();
return true; }
else
{
return false;
} }
catch(MalformedURLException mue)
{
mue.printStackTrace();
throw mue; }
catch(IOException ioe)
{
ioe.printStackTrace();
throw ioe;
} } static URLConnection openConn(String urlk) throws MalformedURLException, IOException
{ URL url = new URL(urlk);
HttpsURLConnection hsu = (HttpsURLConnection) url.openConnection();
hsu.setDoInput(true);
hsu.setDoOutput(true);
hsu.setRequestMethod("POST");
return hsu; } static void closeConn(HttpsURLConnection c)
{
c.disconnect();
} }

这段代码参考了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. PHPWord使用方法

    官方文档  github地址 一.安装 直接使用composer安装,链接地址 composer require phpoffice/phpword 二.简单使用 require_once 'PhpO ...

  2. linux无密登录

    ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub bigdata@cor2

  3. Oracle----oracle 事务总结

    用了这么长时间的oracle,该总结一下所得了 1,事务 事务用于保证数据的一致性, 它由一组相关的 dml语句组成, 该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败,比 ...

  4. POJ 1815 Friendship(最小割)

    http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissio ...

  5. http://localhost:8080请求用户名和密码。信息为:“XDB” 解决办法

    windows查看端口占用情况 cmd下 netstat -ano 查看端口和对应的服务 为2520 Oracle的服务 源博客: http://blog.163.com/jxguo_05/blog/ ...

  6. Android程序执行shell脚本

    在做Android应用时,经常需要执行shell脚本,以快速实现某些功能: 在Android应用程序中执行shell脚本可以省去一大堆繁琐的代码,还可以避免不必要的错误: 比如:拷贝文件夹时,可以执行 ...

  7. java数据结构之枚举

    Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素. import java.util.Vector; import java.util.Enumerat ...

  8. es字符串字段类型的私有属性 建立索引的分析器 用于查询的分析器 短语查询优化

    除了公共属性外,基于字符串的字段还有私有属性 term_vector  是否计算该字段的Lucene词向量term vector no  默认值 yes with_offsets with_posit ...

  9. dev 小问题列表

    1. MemoEdit > Lines Text lines are separated by line feed and carriage return characters ("\ ...

  10. 动态加载和卸载 DLL

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...