最近刚开始做后台,有很多知识亟需补充。
几个平台要统一登录接口,由于投资方的系统使用CAS进行单点登录,我们也只能部署cas。架构师表示有很多比CAS更好的框架,比如openAM之类的。
结果从网上搜到的看起来貌似官方的教程略不靠谱,很多网站的教程还都是3.n的版本号,里面getticket时候需要com.noelios.restlet的包,尼玛至少4.0已经不需要这些包了,把它们配置进去是不科学的。
环境是win10 64bit
使用restful API只需要在服务端做以下几个动作:
一:下载源码(https://www.apereo.org/projects/cas/download-cas我选的是cas server 4.0.0,需要下的是source code:https://github.com/apereo/cas/archive/v4.0.0.zip),解压zip后在cas-4.0.0\cas-server-webapp\pom.xml中配置cas-server-integration-restlet
![](/qrcode.jpg)
<dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-integration-restlet</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> 或也可以 <!-- https://mvnrepository.com/artifact/org.jasig.cas/cas-server-integration-restlet --> <dependency> <groupId>org.jasig.cas</groupId> <artifactId>cas-server-integration-restlet</artifactId> <version>4.0.0</version> <type>jar</typee> </dependency>
二:编译
我特别傻缺地把源码导入了Eclipse,想要Eclipse自己编译顺便还能好好看看代码,结果Eclipse一直在校验,环境都挂了不知道多少回。然后同事说直接用maven自带命令去执行就好了。编译时候只需要编译webapp就可以了。
打开cmd,在cas-4.0.0\cas-server-webapp目录下执行mvn clean package即可,生成的文件在cas-4.0.0\cas-server-webapp\target下。
三:将编译好的cas.war部署到tomcat中,这个应该都会,就不赘述了。重点来了,配置webapps\cas\WEB-INF\web.xml。这里servlet-name是restlet,然后对应的配置已经存在于WEB-INF\restlet-servlet.xml中,两个名字是对应的,如果servlet-name是api,对应配置就是api-servlet.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>
重启启动,我没有配https,直接调http://localhost:8090/cas/v1/tickets,如果结果如下图,就是部署restful成功了
用postman可以请求出结果,如下图
四:测试代码
代码参照了http://www.cnblogs.com/sunshineatnoon/p/4119565.html,不过她用的https,我用的http,强制转换的类不一样,大家各取所需了。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class CasTest { public static void main(String... args) throws Exception { String username = "casuser"; String password = "Mellon"; validateFromCAS(username, password); } public static boolean validateFromCAS(String username, String password) throws Exception { String url = "http://localhost:8090/cas/v1/tickets"; try { HttpURLConnection hsu = (HttpURLConnection) openConn(url); String s = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); s += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); System.out.println(s); OutputStreamWriter out = new OutputStreamWriter(hsu.getOutputStream()); BufferedWriter bwr = new BufferedWriter(out); bwr.write(s); bwr.flush(); bwr.close(); out.close(); String tgt = hsu.getHeaderField("location"); System.out.println(hsu.getResponseCode()); if (tgt != null && hsu.getResponseCode() == 201) { System.out.println(tgt); System.out.println("Tgt is : " + tgt.substring(tgt.lastIndexOf("/") + 1)); tgt = tgt.substring(tgt.lastIndexOf("/") + 1); bwr.close(); closeConn(hsu); String serviceURL = "http://localhost:8080/CasClient"; String encodedServiceURL = URLEncoder.encode("service", "utf-8") + "=" + URLEncoder.encode(serviceURL, "utf-8"); System.out.println("Service url is : " + encodedServiceURL); String myURL = url + "/" + tgt; System.out.println(myURL); hsu = (HttpURLConnection) openConn(myURL); out = new OutputStreamWriter(hsu.getOutputStream()); bwr = new BufferedWriter(out); bwr.write(encodedServiceURL); bwr.flush(); bwr.close(); out.close(); System.out.println("Response code is: " + hsu.getResponseCode()); BufferedReader isr = new BufferedReader(new InputStreamReader(hsu.getInputStream())); String line; System.out.println(hsu.getResponseCode()); while ((line = isr.readLine()) != null) { System.out.println(line); } isr.close(); hsu.disconnect(); return true; } else { return false; } } catch (MalformedURLException mue) { mue.printStackTrace(); throw mue; } catch (IOException ioe) { ioe.printStackTrace(); throw ioe; } } static URLConnection openConn(String urlk) throws MalformedURLException, IOException { URL url = new URL(urlk); HttpURLConnection hsu = (HttpURLConnection) url.openConnection(); hsu.setDoInput(true); hsu.setDoOutput(true); hsu.setRequestMethod("POST"); return hsu; } static void closeConn(HttpURLConnection c) { c.disconnect(); } }
里面用户名密码,子服务什么的,自己填入吧。
以上就没什么问题了:)
如果觉得文章好的话,鼓励一下吧:D