关闭

CAS4 之 集成RESTful API

标签: CAS4restletRESTFUL
2758人阅读 评论(8) 收藏 举报
分类:

       由于公司的需求,需要将现有的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
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3098次
    • 积分:52
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:8条
    文章分类
    文章存档
    最新评论