分享

CAS配置(一)-集成RESTFul

 windli笔记 2013-05-29

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格式信息。

样例代码如下:

  1. public class Client {  
  2.   
  3.     public static String getTicket(final String server, final String username, final String password,  
  4.             final String service) {  
  5.         notNull(server, "server must not be null");  
  6.         notNull(username, "username must not be null");  
  7.         notNull(password, "password must not be null");  
  8.         notNull(service, "service must not be null");  
  9.   
  10.         return getServiceTicket(server, getTicketGrantingTicket(server, username, password), service);  
  11.     }  
  12.   
  13.     /** 
  14.      * 取得ST 
  15.      * @param server 
  16.      * @param ticketGrantingTicket 
  17.      * @param service 
  18.      */  
  19.     private static String getServiceTicket(final String server, final String ticketGrantingTicket, final String service) {  
  20.         if (ticketGrantingTicket == null)  
  21.             return null;  
  22.   
  23.         final HttpClient client = new HttpClient();  
  24.   
  25.         final PostMethod post = new PostMethod(server + "/" + ticketGrantingTicket);  
  26.   
  27.         post.setRequestBody(new NameValuePair[] { new NameValuePair("service", service) });  
  28.   
  29.         try {  
  30.             client.executeMethod(post);  
  31.   
  32.             final String response = post.getResponseBodyAsString();  
  33.   
  34.             switch (post.getStatusCode()) {  
  35.             case 200:  
  36.                 return response;  
  37.   
  38.             default:  
  39.                 warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");  
  40.                 info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));  
  41.                 break;  
  42.             }  
  43.         }  
  44.   
  45.         catch (final IOException e) {  
  46.             warning(e.getMessage());  
  47.         }  
  48.   
  49.         finally {  
  50.             post.releaseConnection();  
  51.         }  
  52.   
  53.         return null;  
  54.     }  
  55.   
  56.     /** 
  57.      * @param server 
  58.      * @param username 
  59.      * @param password 
  60.      */  
  61.     private static String getTicketGrantingTicket(final String server, final String username, final String password) {  
  62.         final HttpClient client = new HttpClient();  
  63.   
  64.         final PostMethod post = new PostMethod(server);  
  65.   
  66.         post.setRequestBody(new NameValuePair[] { new NameValuePair("username", username),  
  67.                 new NameValuePair("password", password) });  
  68.   
  69.         try {  
  70.             client.executeMethod(post);  
  71.   
  72.             final String response = post.getResponseBodyAsString();  
  73.             info("TGT="+response);  
  74.             switch (post.getStatusCode()) {  
  75.             case 201: {  
  76.                 final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(response);  
  77.   
  78.                 if (matcher.matches())  
  79.                     return matcher.group(1);  
  80.   
  81.                 warning("Successful ticket granting request, but no ticket found!");  
  82.                 info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));  
  83.                 break;  
  84.             }  
  85.   
  86.             default:  
  87.                 warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!");  
  88.                 info("Response (1k): " + response.substring(0, Math.min(1024, response.length())));  
  89.                 break;  
  90.             }  
  91.         }  
  92.   
  93.         catch (final IOException e) {  
  94.             warning(e.getMessage());  
  95.         }  
  96.   
  97.         finally {  
  98.             post.releaseConnection();  
  99.         }  
  100.   
  101.         return null;  
  102.     }  
  103.   
  104.     private static void ticketValidate(String serverValidate, String serviceTicket, String service) {  
  105.         notNull(serviceTicket, "paramter 'serviceTicket' is not null");  
  106.         notNull(service, "paramter 'service' is not null");  
  107.   
  108.         final HttpClient client = new HttpClient();  
  109.         GetMethod post = null;  
  110.   
  111.         try {  
  112.             post = new GetMethod(serverValidate+"?"+"ticket="+serviceTicket+"&service="+URLEncoder.encode(service, "UTF-8"));  
  113.             client.executeMethod(post);  
  114.   
  115.             final String response = post.getResponseBodyAsString();  
  116.             info(response);  
  117.             switch (post.getStatusCode()) {  
  118.             case 200: {  
  119.                 info("成功取得用户数据");  
  120.             }  
  121.             default: {  
  122.   
  123.             }  
  124.             }  
  125.   
  126.         } catch (Exception e) {  
  127.             warning(e.getMessage());  
  128.         } finally {  
  129.             //释放资源  
  130.             post.releaseConnection();  
  131.         }  
  132.   
  133.     }  
  134.   
  135.     private static void notNull(final Object object, final String message) {  
  136.         if (object == null)  
  137.             throw new IllegalArgumentException(message);  
  138.     }  
  139.   
  140.     public static void main(final String[] args) throws Exception {  
  141.         final String server = "http://localhost:8080/TFP-S/v1/tickets";  
  142.         final String username = "username";  
  143.         final String password = "username";  
  144.         final String service = "http://localhost:8080/service";  
  145.         final String proxyValidate = "http://localhost:8080/TFP-S/proxyValidate";  
  146.   
  147.           
  148.         ticketValidate(proxyValidate, getTicket(server, username, password, service), service);  
  149.           
  150.     }  
  151.   
  152.     private static void warning(String msg) {  
  153.         System.out.println(msg);  
  154.     }  
  155.   
  156.     private static void info(String msg) {  
  157.         System.out.println(msg);  
  158.     }  
  159.   
  160. }  

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多