由于项目需求,需要在现有的CAS 4.0.0 集成Restful API,在网上查找一些资料,并结合自己的修改,配置如下
一、需要的jar
cas-server-integration-restlet-4.0.0.jar
cglib-nodep-3.1.jar
org.restlet.ext.servlet-2.1.0.jar
org.restlet.ext.slf4j-2.1.0.jar
org.restlet.ext.spring-2.1.0.jar
org.restlet-2.1.0.jar
org.restlet的相应jar,直接去maven.restlet.org上找
二、在web.xml中增加一个servlet配置
<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-name是restlet,然后对应的配置已经存在于WEB-INF\restlet-servlet.xml中,两个名字是对应的,如果servlet-name是api,对应配置就是api-servlet.xml
重启启动,直接调http://localhost:8090/cas/v1/tickets,如果结果如下图,就是部署restful成功了
三、客户端测试代码
这里使用最新的httpclient-4.5.2进行接口调用测试,需要的jar包如下
commons-codec-1.9.jar
commons-logging-1.2.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
httpmime-4.5.2.jar
这几个jar都在httpcomponents-client-4.5.2-bin.zip的压缩包里,
下载地址 http://hc.apache.org/downloads.cgi
package CasTest;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public final class CasTest {
private static final Logger LOG = Logger.getLogger(CasTest.class.getName());
private static RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(15000)
.setConnectTimeout(15000)
.setConnectionRequestTimeout(15000)
.build();
private CasTest() {
// 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;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 创建httpPost
HttpPost httpPost = new HttpPost(server + "/"+ ticketGrantingTicket);
String params = "service="+service;
StringEntity stringEntity = new StringEntity(params, "UTF-8");
stringEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(stringEntity);
// 创建默认的httpClient实例
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
int code = response.getStatusLine().getStatusCode();
System.out.println("code="+code);
switch (code) {
case 200:
return responseContent;
default:
LOG.warning("Invalid response code (" + code + ") from CAS server!");
LOG.info("Response (1k): "+ responseContent.substring(0,Math.min(1024, responseContent.length())));
break;
}
}catch (Exception e) {
LOG.warning(e.getMessage());
e.printStackTrace();
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private static String getTicketGrantingTicket(final String server,
final String username, final String password) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 创建httpPost
HttpPost httpPost = new HttpPost(server);
// 创建参数队列
//List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("username", username));
//nameValuePairs.add(new BasicNameValuePair("password", password));
//设置参数
String params = "username="+username+"&password="+password;
StringEntity stringEntity = new StringEntity(params, "UTF-8");
stringEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(stringEntity);
// 创建默认的httpClient实例
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
int code = response.getStatusLine().getStatusCode();
System.out.println("code="+code);
switch (code) {
case 201: {
final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(responseContent);
if (matcher.matches()){
String str = matcher.group(1);
System.out.println("str="+str);
return str;
}
LOG.warning("Successful ticket granting request, but no ticket found!");
LOG.info("Response (1k): "+ responseContent.substring(0,Math.min(1024, responseContent.length())));
break;
}
default:
LOG.warning("Invalid response code (" + code + ") from CAS server!");
LOG.info("Response (1k): "+ responseContent.substring(0,Math.min(1024, responseContent.length())));
break;
}
}catch (Exception e) {
LOG.warning(e.getMessage());
e.printStackTrace();
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
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://127.0.0.1:18080/cas/v1/tickets";
final String username = "casuser";
final String password = "Mellon";
final String service = "http://localhost:18080/casclientweb";
LOG.info(getTicket(server, username, password, service));
}
}
执行测试结果如下,已经获取ST信息
备注多个bs项目测试时,需要修改如下设置
WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
修改p:cookieSecure="true" 为 p:cookieSecure="false",即不需要安全 cookie