浏览 3468 次
锁定老帖子 主题:CAS 之 集成RESTful API
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-23
最后修改:2011-06-25
关于CAS的登录验证流程,可以参考“CAS 之 实现用户注册后自动登录”,这里的RESTful登录验证流程与其大致相似,大体流程为:首先客户端提交用户名、密码、及Service三个参数,如果验证成功便返回用户的TGT(Ticket Granting Ticket)至客户端, 然后客户端再根据 TGT 获取用户的 ST(Service Ticket)来进行验证登录。 故名思意,TGT是用于生成一个新的Ticket(ST)的Ticket,而ST则是提供给客户端用于登录的Ticket,两者最大的区别在于,TGT是用户名密码验证成功之后所生成的Ticket,并且会保存在Server中及Cookie中,而ST则必须是是根据TGT来生成,主要用于登录,并且当登录成功之后 ST 则会失效。 CAS本身已经提供了 restlet 的集成包,如果你用的是 maven 的话直接加入,我这里的Cas-server的版本是 3.4.2.1: <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-integration-restlet</artifactId> <version>3.4.2.1</version> <type>jar</type> </dependency> 然后再在 web.xml 中加入: <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> 因为使用到了 Restlat 框架,所以还需要依赖几个 jar 包,分别是:
这几个jar已经打包在附件中了,另外 restlet.org 的 maven库中也有,需要的话可以去 maven.restlet.org 上找。 另外关于 restlet的配置在 cas-server中已经存在在: /WEB-INF/restlet-servlet.xml文件。 配置OK之后直接启动Server,下面来进行简单登录验证的测试: 1. 提交用户名密码及Service 进行登录验证 DengerMacBook:cas-server denger$ curl -i -X POST -d "username=admin&password=123456&service=http://www.google.com" http://192.168.41.107:8080/member/v1/tickets/ HTTP/1.1 201 Created Date: Wed, 23 Mar 2011 12:42:52 GMT Location: http://192.168.41.107:8080/member/v1/tickets/TGT-14-gDOn9hhSYmq3xfeTRNhTAjZgOMdCdyuVNfsuLRs6onNv7fVmmX-cas Accept-Ranges: bytes Server: Noelios-Restlet-Engine/1.1.6 Content-Type: text/html;charset=ISO-8859-1 Content-Length: 437 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>201 The request has been fulfilled and resulted in a new resource being created</title></head><body><h1>TGT Created</h1><form action="http://192.168.41.107:8080/member/v1/tickets/TGT-14-gDOn9hhSYmq3xfeTRNhTAjZgOMdCdyuVNfsuLRs6onNv7fVmmX-cas" method="POST">Service:<input type="text" name="service" value=""><br><input type="submit" value="Submit"></form></body></html> 在以上Response信息及 Header中可以看到生成的 TGT,接下来是再重新根据 TGT 获取 ST,将请求的 URI 地址就是以上 Header中的 Location地址。 2. 根据返回的 TGT 来获取 ST DengerMacBook:cas-server denger$ curl -i -X POST -d "service=http://www.google.com" http://192.168.41.107:8080/member/v1/tickets/TGT-14-gDOn9hhSYmq3xfeTRNhTAjZgOMdCdyuVNfsuLRs6onNv7fVmmX-cas HTTP/1.1 200 OK Date: Wed, 23 Mar 2011 12:48:03 GMT Accept-Ranges: bytes Server: Noelios-Restlet-Engine/1.1.6 Content-Type: text/plain;charset=ISO-8859-1 Content-Length: 29 ST-2-lJfQyJMMEnNGnKcglf1d-cas 获取成功之后则返回了 ST,这时候对于客户端而言就已经拿到了登录的TIcket, 如果需要在Web中自动登录的话,只需要弹出浏览器,将ST作为 ticket参数传入即可。如,用户中心的后台地址首页是:http://www.google.com.hk/userCenter 则URL为: http://www.google.com.hk/userCenter?ticket=ST-3-9QkpLsFmCEqIXSVvGH9P-cas 并可进行登录。 当然前提是在该Web应用中需要部署cas-client应用。 3. 注销用户 DengerMacBook:cas-server denger$ curl -i -X DELETE http://192.168.41.107:8080/member/v1/tickets/TGT-14-gDOn9hhSYmq3xfeTRNhTAjZgOMdCdyuVNfsuLRs6onNv7fVmmX-cas HTTP/1.1 200 OK Date: Wed, 23 Mar 2011 12:54:28 GMT Accept-Ranges: bytes Server: Noelios-Restlet-Engine/1.1.6 Content-Length: 0注销用户就很简单了,直接 SUBMIT DELETE 删除 TGT即可. Java代码调用示例: package cas; import java.io.IOException; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public final class Client { private static final Logger LOG = Logger.getLogger(Client.class.getName()); private Client() { // static-only access } 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); } 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: 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(e.getMessage()); } finally { post.releaseConnection(); } return null; } 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(); switch (post.getStatusCode()) { case 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(e.getMessage()); } finally { post.releaseConnection(); } return null; } private static void notNull(final Object object, final String message) { if (object == null) throw new IllegalArgumentException(message); } public static void main(final String[] args) { final String server = "http://192.168.41.107:8080/member/v1/tickets"; final String username = "admin"; final String password = "111111"; final String service = "http://localhost:8080/service"; LOG.info(getTicket(server, username, password, service)); } } 参考:https://wiki.jasig.org/display/CASUM/RESTful+API 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-03-21
最后修改:2012-03-21
没有用过maven,不知道加
== CAS本身已经提供了 restlet 的集成包,如果你用的是 maven 的话直接加入 == 这句话有什么作用,因为我按照上面的步骤,在启动tomcat的时候,就报WARN,提示attr没有attachment属性之类的,然后从Client.java中直接运行时,cas server出现大片异常。 == 不知道是不是要用maven编译一下什么地方后,再去启动tomcat,是不是呢?请指点一二。 还有在Client.java里面response得到的总是空,试过好几次都失败了 |
|
返回顶楼 | |