CAS Restful API 文档


第1页共11页 CAS RESTful API 文档 版本 版本 版本 版本 日期 日期 日期 日期 修改人 修改人 修改人 修改人 描述 描述 描述 描述 1.0.0 2010-06-05 yinchong 创建文档 2010-06-07 yinchong 增加客户端开发配置 目录 1.1.1.1.下载................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................2222 2.2.2.2. 安装SERVERSERVERSERVERSERVER.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... 2222 3.3.3.3. 配置SERVERSERVERSERVERSERVER.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... 2222 3.1. 添加 CAS SERVER 依赖的 JAR................................................................................................................... 2 3.2. 添加数据源 ..........................................................................................................................................3 3.3. 配置 MD5 加密 BEAN............................................................................................................................ 3 3.4. 添加 CAS服务的验证 ........................................................................................................................... 3 3.5. 取消 HTTPS 验证 .................................................................................................................................... 4 3.6. 中文登录 ..............................................................................................................................................5 3.7. 配置 RESTLET.......................................................................................................................................... 5 3.8. 配置 SESSION 有效时间 ......................................................................................................................... 6 4.4.4.4. 客 户端 开发 ////配置....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................6666 4.1. 下载客户端依赖 JAR.............................................................................................................................6 4.2. RESTFUL 示例 .........................................................................................................................................6 第2页共11页 1.1.1.1. 下载 下载 下载 下载 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.2.2.2. 安装 安装 安装 安装 serverserverserverserver � 解压cas-server-3.4.2,将cas-server-3.4.2\modules\cas-server-webapp-3.4.2.war 拷贝到tomcat 的webapps 下。 3.3.3.3. 配置 配置 配置 配置 serverserverserverserver 3.1.3.1.3.1.3.1. 添加添加添加添加cascascascas server server server server 依赖的依赖的依赖的依赖的jarjarjarjar � 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 目录下。 � 数据库驱动 jar 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib 目录下。 � 到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 � 下载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 � 下载CGlib http://sourceforge.net/projects/cglib/files/ 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib。 � 下载ASM/OW2 http://forge.ow2.org/projects/asm/ 拷贝到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\lib。 第3页共11页 3.2.3.2.3.2.3.2. 添加数据源添加数据源添加数据源添加数据源 将下面代码复制到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml 配置文件 的beans 节点里面: com.mysql.jdbc.Driver jdbc:mysql://192.168.1.22:3306/databaseName?useUnicode=true&useServerPrepStmts =false&characterEncoding=UTF-8&autoReconnect=true root root 3.3.3.3.3.3.3.3. 配置配置配置配置MD5 MD5 MD5 MD5 加密加密加密加密beanbeanbeanbean 将下面代码复制到 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml 配置文件 的beans 节点里面: 3.4.3.4.3.4.3.4. 添加添加添加添加cas cas cas cas 服务的验证服务的验证服务的验证服务的验证 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\deployerConfigContext.xml \cas\WEB-INF\deployerConfigContext.xml \cas\WEB-INF\deployerConfigContext.xml \cas\WEB-INF\deployerConfigContext.xml 配置文件 找到下面代码: 第4页共11页 替换为下面代码: 3.5.3.5.3.5.3.5. 取消取消取消取消https https https https 验证验证验证验证 打开 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\spring-configuration\ticketGrantingcas\WEB-INF\spring-configuration\ticketGrantingcas\WEB-INF\spring-configuration\ticketGrantingcas\WEB-INF\spring-configuration\ticketGranting TicketCookieGenerator.xml TicketCookieGenerator.xml TicketCookieGenerator.xml TicketCookieGenerator.xml 配置文件,将p:cookieSecure="true" 改为p:cookieSecure="false",改 完后如下: 打开 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\spring-configuration\warnCookieGecas\WEB-INF\spring-configuration\warnCookieGecas\WEB-INF\spring-configuration\warnCookieGecas\WEB-INF\spring-configuration\warnCookieGe nerator.xml nerator.xml nerator.xml nerator.xml 配置文件,将 p:cookieSecure="true" 改为 p:cookieSecure="false",改完后如下: 打开D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml cas\WEB-INF\deployerConfigContext.xml 配置 文件,找到HttpBasedServiceCredentialsAuthenticationHandler 增加p:requireSecure="false",改完后 如下: 第5页共11页 3.6.3.6.3.6.3.6. 中文登录中文登录中文登录中文登录 打开 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\web.xmlcas\WEB-INF\web.xmlcas\WEB-INF\web.xmlcas\WEB-INF\web.xml,增加下面蓝色部分代码: contextConfigLocation /WEB-INF/spring-configuration/*.xml /WEB-INF/deployerConfigContext.xml encoding-filter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encoding-filter /* 这个时候启动 tomcat,cas 可以和你配置的数据库进行交互。 http://localhost:8080/cas 输入配置数据库中的 tblUser 表中的用户名密码即可以登录。 3.7.3.7.3.7.3.7. 配置配置配置配置restletrestletrestletrestlet 打开 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\web.xml cas\WEB-INF\web.xml cas\WEB-INF\web.xml cas\WEB-INF\web.xml 文件,添加下面 servlet: restlet com.noelios.restlet.ext.spring.RestletFrameworkServlet 1 第6页共11页 restlet /v1/* 参考: http://jasig.275507.n4.nabble.com/Setting-up-the-RESTlet-servlet-on-CAS-3-3-5-td2068602.html 3.8.3.8.3.8.3.8. 配置配置配置配置session session session session 有效时间有效时间有效时间有效时间 打开配置文件 D:\server\apache-tomcat-6.0.18\webapps\cas\WEB-INF\spring-configuration\ticketExpirationPolicies.xmlcas\WEB-INF\spring-configuration\ticketExpirationPolicies.xmlcas\WEB-INF\spring-configuration\ticketExpirationPolicies.xmlcas\WEB-INF\spring-configuration\ticketExpirationPolicies.xml ,配置文件很简单,分别是修改 serviceTicket 和grantingTicket,打开一看就知道怎么改了。 4.4.4.4. 客户端开发客户端开发客户端开发客户端开发////配置配置配置配置 RESTful API 作 用应 该是 service to service 的 验证 ,我 的理 解。 使用RESTful API 开 发非 常简 单, 也不 需要 配置 什么 xml 文 件, 只需 要写 code 就 可以 了。 4.1.4.1.4.1.4.1. 下载客户端依赖 下载客户端依赖 下载客户端依赖 下载客户端依赖 jarjarjarjar http://commons.apache.org/codec/download_codec.cgi http://hc.apache.org/downloads.cgi 4.2.4.2.4.2.4.2. RESTful RESTful RESTful RESTful 示例示例示例示例 参考最下面 java 示例: https://wiki.jasig.org/display/CASUM/RESTful+API 根据官方例子修改后如下 : /** * Created by IntelliJ IDEA. * User: yinchong * Date: 2010-6-7 * Time: 14:16:14 * To change this template use File | Settings | File Templates. *
SSO 验证实现类 */ public final class SSO{ private static final Logger LOG = Logger.getLogger(SSODaoImpl.class.getName()); /** * http 请求状态码 201 */ private static final int HTTP_STATUS_CODE_201 = 201; /** 第7页共11页 * http 请求状态码 200 */ private static final int HTTP_STATUS_CODE_200 = 200; /** * 获取 Service ticket * *@param server cas 服务 url *@param ticketGrantingTicket ticket granting ticket *@param service 被验证的服务 url *@return service ticket */ public String getServiceTicket(final String server, final String ticketGrantingTicket, final String service) { notNull(server, "server must not be null"); notNull(ticketGrantingTicket, "ticketGrantingTicket must not be null"); notNull(service, "service must not be 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 HTTP_STATUS_CODE_200: return response; default: LOG.warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!"); 第8页共11页 LOG.info("Response (1k): " + response.substring(0, Math.min(1024, response.length()))); break; } } catch (final IOException e) { LOG.warning("getServiceTicket" + e); } finally { post.releaseConnection(); } return null; } /** * 获取 ticket granting ticket * *@param server cas 服务 url *@param username 验证的用户名 *@param password 验证的用户密码 *@return ticket granting ticket */ public String getTicketGrantingTicket(final String server, final String username, final String password) { notNull(server, "server must not be null"); notNull(username, "username must not be null"); notNull(password, "password must not be null"); final HttpClient client = new HttpClient(); final PostMethod post = new PostMethod(server); post.setRequestBody(new NameValuePair[]{ new NameValuePair("username", username), new NameValuePair("password", password)}); try { 第9页共11页 client.executeMethod(post); final String response = post.getResponseBodyAsString(); switch (post.getStatusCode()) { case HTTP_STATUS_CODE_201: { final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(response); if (matcher.matches()) { return matcher.group(1); } LOG.warning("Successful ticket granting request, but no ticket found!"); LOG.info("Response (1k): " + response.substring(0, Math.min(1024, response.length()))); break; } default: LOG.warning("Invalid response code (" + post.getStatusCode() + ") from CAS server!"); LOG.info("Response (1k): " + response.substring(0, Math.min(1024, response.length()))); break; } } catch (final IOException e) { LOG.warning("getTicketGrantingTicket:" + e); } finally { post.releaseConnection(); } return null; } /** * 删除 cas 服务中制定 ticket granting ticket,也就是 logout. * 第10页共11页 *@param server cas 服务 url *@param ticket ticket granting ticket */ public void deleteTicket(String server, String ticket) { notNull(server, "server must not be null"); notNull(ticket, "username must not be null"); final HttpClient client = new HttpClient(); final DeleteMethod delete = new DeleteMethod(server + "/" + ticket); try { client.executeMethod(delete); final String response = delete.getResponseBodyAsString(); switch (delete.getStatusCode()) { case HTTP_STATUS_CODE_200: LOG.info("Successful delete ticket granting ticket."); break; default: LOG.warning("Invalid response code (" + delete.getStatusCode() + ") from CAS server!"); LOG.info("Response (1k): " + response.substring(0, Math.min(1024, response.length()))); break; } } catch (final IOException e) { LOG.info("deleteTicket:" + e); } finally { delete.releaseConnection(); } } /** * 参数验证方法,保证参数不为 null 第11页共11页 * *@param object 需要验证的参数 *@param message 验证的异常信息 */ private static void notNull(final Object object, final String message) { if (object == null) { throw new IllegalArgumentException(message); } } }
还剩10页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 7 金币 [ 分享pdf获得金币 ] 2 人已下载

下载pdf

pdf贡献者

lucien0721

贡献于2013-11-30

下载需要 7 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf