在被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. Struts1标签

    Struts1 标签库  说明 Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML 标签 : 用来创建能够和Struts 框架和其他相应的HT ...

  2. 【转】Monkey测试4——Monkey命令行可用的全部选项

    Monkey命令行可用的全部选项 常规 --help 列出简单的用法. -v 命令行的每一个-v将增加反馈信息的级别. Level 0(缺省值)除启动提示.测试完成和最终结果之外,提供较少信息. Le ...

  3. sourcenav安装

    $ ./configure之后会出现 configure: error: ./configure failed for unixconfigure: error: ./configure failed ...

  4. 第二章----python函数

    第一节:调用函数 1.函数是什么? 函数是组织好的,可以重复利用的. 2.为什么要用到函数? 提高应用的模块性,提高重复利用率.指的是:多个文件中可能都要用到该函数,直接拿来调用就行,不用在重复写一个 ...

  5. Mac下通过shell脚本修改properties文件

    通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...

  6. ASP.NET中RegisterStartupScript和RegisterClientScriptBlock有区别吗

    今天用RegisterClientScriptBlock()方法调用了alertify.js(绚丽的实现alert()同样的提示功能): Page.ClientScript.RegisterClien ...

  7. iOS学习笔记(九)—— xml数据解析

    在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML.TouchXML.KissXML.Tiny ...

  8. 小程序 less wxss 混合 Mixins picker样式优化 箭头样式的实现原理

    lessc src/style/picker-arrow_.less src/style/picker-arrow_.wxss 快速入门 | Less.js 中文文档 https://less.boo ...

  9. <2013 07 05> 804.15. 4--> TI MSP430+CC2520 调试

    这一周,实际参与eCar项目的工作正式展开. 来TUM的第一个月,主要熟悉了eCar的机电结构,特别是熟悉了eCar的IT(Information Technology),包括硬件和代码. 来的时候, ...

  10. python错误笔记

    1.print "hello world!";SyntaxError:Missing parentheses in call to ‘paint’ . Did you mean p ...