CAS配置(一)-集成RESTFul

CAS单点登录服务器很多时候都是被B/S的应用使用,那么对已有些系统是CS的那么怎么去调用呢,这个时候就需要使用webservice来给CS的系统调用了,我们先来说说先决条件吧:

1)集成需要的jar包,这个是必不可少的

com.noelios.restlet.ext.servlet-1.1.1.jar

com.noelios.restlet.ext.spring-1.1.1.jar

com.noelios.restlet-1.1.1.jar

org.restlet.ext.spring-1.1.1.jar

org.restlet-1.1.1.jar

cglib-2.2.jar

cas-server-integration-restlet-3.4.7.jar

2)配置,在web.xml中增加一个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>

那么我们的CS客户端怎么去处理呢,以及怎么去拿到用户数据呢?需要有三次交互才能取得用户数据

1)CS客户端提供用户名和密码,请求http://localhost:8080/TFP-S/v1/tickets,如果用户合法则得到TGT数据。

2)根据TGT和service取得ST票据,请求的路径是:http://localhost:8080/TFP-S/v1/tickets/TGT_编号

3)验证ST票据,得到用户信息的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/TFP-S/v1/tickets";
		final String username = "username";
		final String password = "username";
		final String service = "http://localhost:8080/service";
		final String proxyValidate = "http://localhost:8080/TFP-S/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);
	}

}

如果对返回来的用户信息是什么格式不清楚,那么下面是一个xml格式。

<cas:serviceResponse >
	<cas:authenticationSuccess>
		<cas:user>xuf</cas:user>
		<cas:attributes>
			<cas:securityLevel>2</cas:securityLevel>
			<cas:userType>个人用户</cas:userType>
			<cas:age>32</cas:age>
		</cas:attributes>  
	</cas:authenticationSuccess>
</cas:serviceResponse>
这个格式怎么修改?在透露一点吧,就是在CAS服务器那边是不是有casServiceValidationFailure.jsp文件,对了,就是它决定返回的xml格式的。如果使用Filter,其实也是传递回来这个xml,只是验证票据的过滤器,将这个xml转换成Assertion对象了。明白了吧。


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org ----------------------------------------- Native REST support * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The innovation is that that it uses the same API, reducing the learning curve and the software footprint. * Restlet-GWT module available, letting you leverage the Restlet API from within any Web browser, without plugins. * Concept of "URIs as UI" supported based on the URI Templates standard. This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc.) through a simple HTTP POST. This service is transparent for Restlet applications. Complete Web Server * Static file serving similar to Apache HTTP Server, with metadata association based on file extensions. * Transparent content negotiation based on client preferences. * Conditional requests automatically supported for resources. * Remote edition of files based on PUT and DELETE methods (aka mini-WebDAV mode). * Decoder service transparently decodes compressed or encoded input representations. This service is transparent for Restlet applications. * Log service writes all accesses to your applications in a standard Web log file. The log format follows the W3C Extended Log File Format and is fully customizable. * Powerful URI based redirection support similar to Apache Rewrite module. Available Connectors * Multiple server HTTP connectors available, based on either Mortbay's Jetty or the Simple framework or Grizzly NIO framework. * AJP server connector available to let you plug behind an Apache HTT
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. 添加数据源
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值