由于公司的需求,需要将现有的CAS4Server 集成Restful API,网上搜了一些资料、以及官方文档,但都是CAS3的,不过CAS3和CAS4是有一些区别的,以下看具体配置吧!
CAS本身已经提供了 restlet 的集成包,如果你用的是 maven 的话直接加入,我这里的Cas-server的版本是 4.0.0:
- <dependency>
- <groupId>org.jasig.cas</groupId>
- <artifactId>cas-server-integration-restlet</artifactId>
- <version>4.0.0</version>
- <type>jar</type>
- </dependency>
- 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
- cglib-nodep-2.1_3.jar
如果自己编译的话工程的话,对应的JAR包也会下载到本地的maven仓库中的,或者直接去 maven.restlet.org 上找。
然后再在 web.xml 中加入:
- <servlet>
- <servlet-name>restlet</servlet-name>
- <servlet-class>org.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>
这里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