在被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. PHP 几种 序列化/反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  2. java 窗体

    import javax.swing.*; /** * 一个简单的java窗体例子 */ public class Test { public static void main(String[] ar ...

  3. I.MX6 Ethernet MAC (ENET) MAC Address hacking

    /********************************************************************* * I.MX6 Ethernet MAC (ENET) M ...

  4. ASP.NET管道技术

    Asp.net mvc是基于dnf(.net framework )实现了代码分离的开源框架.ASP.NET MVC框架拥有极强的定制以及扩展的特性.本文梳理了ASP.NET MVC的管道(pipel ...

  5. chm文件无法阅读

    当我们费劲千辛万苦从网上下载好chm文件资料后,打开后发现竟然是这个样子的: 其中主要原因是CHM文件被阻止显示了,CHM文件在NTFS格式的硬盘里的时候就会被阻止显示.我们返回我的电脑,点中我们存放 ...

  6. 【BZOJ1190】[HNOI2007]梦幻岛宝珠 分层背包DP

    [BZOJ1190][HNOI2007]梦幻岛宝珠 Description 给你N颗宝石,每颗宝石都有重量和价值.要你从这些宝石中选取一些宝石,保证总重量不超过W,且总价值最大为,并输出最大的总价值. ...

  7. 打日志--以python为例

    日志报错要去修,要不然是隐患,总有一天会爆炸 增加日志是排错的好方法,不要不舍得加日志,比如怕代码变难看,怕日志输出太多. python logging exc_info sys.exc_info() ...

  8. 【python】-- Django ORM(进阶)

    Django ORM(进阶) 上一篇博文简述了Django ORM的单表操作,在本篇博文中主要简述Django ORM的连表操作. 一.一对多:models.ForeignKey() 应用场景:当一张 ...

  9. setlocale同mbstowcs函数的关系(VS2008下setlocale(LC_ALL, "chs")可以执行成功,BCB使用setlocale(LC_ALL, "Chinese (Simplified)_People's Republic of China"),linux上locale别名表大概在 /usr/lib/X11/locale/locale.alias)

    序中,如果要将ASCII码字符串转换为宽字符(Unicode),可以利用标准C的mbstowcs函数. 微软在MSDN中有示例,如下: 然而,这段代码在处理含有汉字的字符串时就会出现问题.比如将: w ...

  10. vuejs组件通信

    <body> <div id="example"> <father></father> </div> </body ...