在被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. SpringBoot对比传统开发以及自身的优缺点

    SpringBoot是伴随着Spring4.0诞生的,继承了Spring的优点,一经推出,引起了巨大的反向:目前Spring Boot的版本为2.1.0,需要Java7及Spring Framewor ...

  2. Linux 总结2

    cd                                             pwd                                            mkdir ...

  3. hdu 3062+1824(2-sat入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 思路:根据矛盾关系连边(如果a与b矛盾,则连边a'->b,b'->a),然后强连通缩 ...

  4. poj 2112(二分+多重匹配)

    题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...

  5. Laravel5.1 模型--ModelFactory

    今天要说的是模型工厂,它是可以快速生成一些测试数据的东西,之前我们介绍过Seeder,当我们使用模型访问数据时 可以用模型工厂搭配Seeder使用. 1 编写一个ModelFactory ModelF ...

  6. WPF开发简介教程

    1/ VS中文件-新建-项目-WPF应用程序 2/ 左上角工具箱中有很多组件可以直接拖拽使用 3/ 双击组件,进入脚本功能编辑界面,如按钮: private void Button_Click_1(o ...

  7. linux中使用vi 打开文件时,能显示行号

    方法一: 1.显示当前行行号,在VI的命令模式下输入 :nu 2.显示所有行号,在VI的命令模式下输入     :set nu方法二: 使用vi编辑~/.vimrc文件,在该文件中加入一行" ...

  8. 用训练好的caffemodel对单个/批量图片进行分类

    一.单个图片进行分类 这个比较简单,在*.bat文件中输入以下代码: @echo off set BIN_DIR=D:\caffe\caffe-windows\Build\x64\Release se ...

  9. CodeIgniter框架——CI中视图路径问题

    答: 视图中的所有路径全部和 index.php 同级,也就是和 index.php 属于一个目录下,也就是网站根目录. 因为 index.php 后面看似是路径的东西其实那只是一种 URL 参数而已 ...

  10. 【转】NPOI自定义单元格背景颜色

    经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...