三步搞定通过Rest client访问被CAS保护的资源:
Step 1: 获取一个Ticket granting ticket
final RestClient client = new RestClient();
String ticketGrantingTicket = null;
final String tgtResponse = client
.resource("https://yourcasserver/v1/tickets")
.accept(MediaType.TEXT_HTML)
.post("username=$username&password=$password")
.getEntity(String.
class);
final Matcher matcher = Pattern.compile(".*action=\".*/(.*?)\".*").matcher(
tgtResponse);
if (matcher.matches())
{
ticketGrantingTicket = matcher.group(1);
}
Step 2: 获取一个
Service Ticket
final String serviceTicket = client
.resource(
"https://yourcasserver/v1/tickets/" + ticketGrantingTicket)
.accept(MediaType.TEXT_HTML)
.post("service=$serviceUrl")
.getEntity(String.class);
Step 3: 请求CAS protected resource
client.resource(RESOURCE_URL)
.queryParam("ticket", serviceTicket).accept(MediaType.
APPLICATION_
XML)
.get(xxx.class);