CAS4 之 集成RESTful API

       由于公司的需求,需要将现有的CAS4Server 集成Restful API,网上搜了一些资料、以及官方文档,但都是CAS3的,不过CAS3和CAS4是有一些区别的,以下看具体配置吧!

        CAS本身已经提供了 restlet 的集成包,如果你用的是 maven 的话直接加入,我这里的Cas-server的版本是 4.0.0: 


  1. <dependency>  
  2.     <groupId>org.jasig.cas</groupId>  
  3.     <artifactId>cas-server-integration-restlet</artifactId>  
  4.     <version>4.0.0</version>  
  5.     <type>jar</type>  
  6. </dependency>  
        查看POM文件:我们可以发现需要如下依赖包:

  •      org.restlet-2.1.0.jar
  •      org.restlet.ext.spring-2.1.0.jar
  •      org.restlet.ext.slf4j-2.1.0.jar
  •      org.restlet.ext.servlet-2.1.0.jar
         事实上我们还需要另外一个jar包

  •      cglib-nodep-2.1_3.jar

    如果自己编译的话工程的话,对应的JAR包也会下载到本地的maven仓库中的,或者直接去 maven.restlet.org 上找。

       然后再在 web.xml 中加入: 


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

       这里servlet-class要看好和CAS3的配置不一样!

        restlet的配置在 cas-server中已经存在在: /WEB-INF/restlet-servlet.xml文件


          客户端测试:

public class Client {

	public static String getTicket(final String server, final String username, final String password,
			final String service) {
		notNull(server, "server must not be null");
		notNull(username, "username must not be null");
		notNull(password, "password must not be null");
		notNull(service, "service must not be null");

		return getServiceTicket(server, getTicketGrantingTicket(server, username, password), service);
	}

	/**
	 * 取得ST
	 * @param server
	 * @param ticketGrantingTicket
	 * @param service
	 */
	private static String getServiceTicket(final String server, final String ticketGrantingTicket, final String service) {
		if (ticketGrantingTicket == null)
			return null;

		final HttpClient client = new HttpClient();

		final PostMethod post = new PostMethod(server + "/" + ticketGrantingTicket);

		post.setRequestBody(new NameValuePair[] { new NameValuePair("service", service) });

		try {
			client.executeMethod(post);

			final String response = post.getResponseBodyAsString();

			switch (post.getStatusCode()) {
			case 200:
				return response;

			default:
				warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");
				info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
				break;
			}
		}

		catch (final IOException e) {
			warning(e.getMessage());
		}

		finally {
			post.releaseConnection();
		}

		return null;
	}

	/**
	 * @param server
	 * @param username
	 * @param password
	 */
	private static String getTicketGrantingTicket(final String server, final String username, final String password) {
		final HttpClient client = new HttpClient();

		final PostMethod post = new PostMethod(server);

		post.setRequestBody(new NameValuePair[] { new NameValuePair("username", username),
				new NameValuePair("password", password) });

		try {
			client.executeMethod(post);

			final String response = post.getResponseBodyAsString();
			info("TGT="+response);
			switch (post.getStatusCode()) {
			case 201: {
				final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(response);

				if (matcher.matches())
					return matcher.group(1);

				warning("Successful ticket granting request, but no ticket found!");
				info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
				break;
			}

			default:
				warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");
				info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));
				break;
			}
		}

		catch (final IOException e) {
			warning(e.getMessage());
		}

		finally {
			post.releaseConnection();
		}

		return null;
	}

	private static void ticketValidate(String serverValidate, String serviceTicket, String service) {
		notNull(serviceTicket, "paramter 'serviceTicket' is not null");
		notNull(service, "paramter 'service' is not null");

		final HttpClient client = new HttpClient();
		GetMethod post = null;

		try {
			post = new GetMethod(serverValidate+"?"+"ticket="+serviceTicket+"&service="+URLEncoder.encode(service, "UTF-8"));
			client.executeMethod(post);

			final String response = post.getResponseBodyAsString();
			info(response);
			switch (post.getStatusCode()) {
			case 200: {
				info("成功取得用户数据");
			}
			default: {

			}
			}

		} catch (Exception e) {
			warning(e.getMessage());
		} finally {
			//释放资源
			post.releaseConnection();
		}

	}

	private static void notNull(final Object object, final String message) {
		if (object == null)
			throw new IllegalArgumentException(message);
	}

	public static void main(final String[] args) throws Exception {
		final String server = "http://localhost:8080/CASServer/v1/tickets";
		final String username = "username";
		final String password = "username";
		final String service = "http://localhost:8080/service";  //随意写
		final String proxyValidate = "http://localhost:8080/<span style="font-family: Arial, Helvetica, sans-serif;">CASServer</span>/proxyValidate";

		
		ticketValidate(proxyValidate, getTicket(server, username, password, service), service);
		
	}

	private static void warning(String msg) {
		System.out.println(msg);
	}

	private static void info(String msg) {
		System.out.println(msg);
	}

}
返回数据:
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
	<cas:authenticationSuccess>
		<cas:user>jy</cas:user>
        
        
	</cas:authenticationSuccess>
</cas:serviceResponse>



cglib-nodep-2.1_3.jar
server 地址: http://www.jasig.org/cas/download client 地址: http://www.ja-sig.org/downloads/cas-clients/ 当前最新版本 cas-server-3.4.2 , cas-client-3.1.10 2. 安装 server l 解压 cas-server-3.4.2 ,将 cas-server-3.4.2\modules\cas-server-webapp-3.4.2.war 拷贝到 tomcat 的 webapps 下。 3. 配置 server 3.1. 添加 cas server 依赖的 jar n cas-server-3.4.2\modules\cas-server-support-jdbc-3.4.2.jar 、 cas-server-integration-restlet-3.4.2.jar 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 目录下。 n 数据库驱动 jar 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 目录下。 n 到 apache 网站下载下面三个 cas server 依赖 jar 包拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 目录下 Ø http://apache.freelamp.com/commons/collections/binaries/commons-collections-3.2.1-bin.zip Ø http://apache.etoak.com/commons/dbcp/binaries/commons-dbcp-1.4-bin.zip Ø http://apache.etoak.com/commons/pool/binaries/commons-pool-1.5.4-bin.zip n 下载 restlet 相关 http://www.restlet.org/downloads/ , 解压后将下面 jar 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib: ( 它奶奶地这一步骤很折腾 ) com.noelios.restlet.ext.servlet_2.5.jar com.noelios.restlet.ext.spring_2.5.jar com.noelios.restlet.jar org.restlet.ext.spring_2.5.jar org.restlet.jar n 下载 CGlib http://sourceforge.net/projects/cglib/files/ 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 。 n 下载 ASM/OW2 http://forge.ow2.org/projects/asm/ 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 。 3.2. 添加数据源
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值