在web.xml中添加如下配置
POST http://192.168.0.99:9000/cas/v1/tickets username=adminoue&password=4009696109
发送一个POST请求到/cas/v1/tickets,传递请求参数为username=adminoue&password=4009696109。请求成功后会返回http://192.168.0.99:9000/cas/v1/tickets/TGT-7-U71jox1ykWosLwQQSpcc5g47jdKul9ccgjgxgIchl64rSTpa1K-localhost。
其TGT-7-U71jox1ykWosLwQQSpcc5g47jdKul9ccgjgxgIchl64rSTpa1K-localhost为服务器端返回的请求票根。下文在请求服务的时候需携带该信息。
9.4. 请求服务授权
POST
http://192.168.0.99:9000/cas/v1/tickets/TGT-7-U71jox1ykWosLwQQSpcc5g47jdKul9ccgjgxgIchl64rSTpa1K-localhost service=http://www.aimeizi.net即可,最终返回一串ST-4-3V9Fs9PbbcUZ5PiIJaR6-localhost表示登录成功。
这里需要携带上文请求后返回的请求票据,请求该票据并传递service=你的应用地址最终返回登录成功后的票据信息。
9.5. Java Client测试
这里使用commons-httpclient来模拟上述中的请求流程。 import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import java.io.IOException; import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by Administrator on 2016/5/11 0011. */ public class Client { public static String getTicket(final String server, final String username, final String password, final String service) { notNull(server, \ notNull(username, \ notNull(password, \ notNull(service, \ 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 + \ post.setRequestBody(new NameValuePair[]{new NameValuePair(\ try { client.executeMethod(post); final String response = post.getResponseBodyAsString(); switch (post.getStatusCode()) { case 200: return response; default: warning(\server!\ info(\(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), new NameValuePair(\ try { client.executeMethod(post); final String response = post.getResponseBodyAsString(); info(\ switch (post.getStatusCode()) { case 201: { final Matcher matcher = Pattern.compile(\ if (matcher.matches()) return matcher.group(1); warning(\ info(\(1k): \+ response.substring(0, Math.min(1024, response.length()))); break; } default: warning(\server!\ info(\(1k): \+ response.substring(0, Math.min(1024, response.length()))); break; } } catch (final IOException e) { warning(e.getMessage()); } finally { post.releaseConnection();