昨天尝试把cas的java client端部署到另外一台机器,结果就有问题了。(localhost部署cas server和java client端参见:http://www.cnblogs.com/sunshineatnoon/p/4119565.html

主要是client访问的时候报错:javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present。

后来在stackoverflow上找到了解决办法:http://stackoverflow.com/questions/9331087/how-to-setup-ssl-for-cas-and-client-different-machines?rq=1

根据jasig文档对这个错误的解释:

Sample Alt Name Stack Trace
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
In most cases this is a hostname/SSL certificate CN mismatch. This commonly happens when a self-signed certificate issued to localhost is placed on a machine that is accessed by IP address. It should be noted that generating a certificate with an IP address for a common name, e.g. CN=192.168.1.1,OU=Middleware,dc=vt,dc=edu, will not work in most cases where the client making the connection is Java. For example the Java CAS client will throw SSL errors on connecting to a CAS server secured with a certificate containing an IP address in the CN.

是由于生成的证书中域名(CN)和server的域名或者以后client端访问的域名不一致造成的,并且这里也说了,不能用ip地址当作生成证书时候的CN,只能用域名。

所以改变的方法就是用域名生成证书,并且配置client端电脑的hosts和lmhosts.sam文件解析服务器域名,具体步骤如下:

1. 编辑client端机器的C:\Windows\System32\Drivers\etc\hosts,添加一行:

your_ip(xxx.xxx.xxx.xxx) your_cn(sunshineatnoon.com)

2. 编辑client端机器的C:\Windows\System32\Drivers\etc\lmhosts.sam,添加一行:

your_ip(xxx.xxx.xxx.xxx) your_cn(sunshineatnoon.com)

3. 在server端所在的机器用keytool重新生成证书:

keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "g:\tomcat.keystore"

在回答"what's you name?"这个问题的时候回答上述你的域名:sunshineatnoon.com

【Tech】CAS多机部署Server和Java Client端-LMLPHP

4.配置server端机器的tomcat下%TOMCAT_PATH%/conf下的server.xml文件,参考http://www.cnblogs.com/sunshineatnoon/p/4064632.html的3(1)(2),如果生成证书和以前的位置密码都没变,就不用重新配置了。

5.重新用文件InstallCert.java生成证书放到client端机器的$JAVA_HOME\jre\lib\security下,参考http://www.cnblogs.com/sunshineatnoon/p/4070750.html我所解决的第2个bug。

6.client端的java程序中请求ticket的url就由https://localhost:8443/cas/v1/tickets变成了https://sunshineatnoon.com:8443/cas/v1/tickets,改变后的client.java如下所示:

 package cas;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import javax.net.ssl.HttpsURLConnection; public class Client { public static void main(String... args) throws Exception
{
String username ="test01";
String password ="psw01";
validateFromCAS(username,password);
} public static boolean validateFromCAS(String username, String password) throws Exception
{ //String url = "https://localhost:8443/cas/v1/tickets";
String url = "https://sunshineatnoon.com:8443/cas/v1/tickets";
try
{
HttpsURLConnection hsu = (HttpsURLConnection)openConn(url);
String s = URLEncoder.encode("username","UTF-8") + "=" + URLEncoder.encode("test01","UTF-8");
s+="&" +URLEncoder.encode("password","UTF-8") + "=" + URLEncoder.encode("psw01","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 serviceURL = "http://www.baidu.com";
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 = (HttpsURLConnection)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);
HttpsURLConnection hsu = (HttpsURLConnection) url.openConnection();
hsu.setDoInput(true);
hsu.setDoOutput(true);
hsu.setRequestMethod("POST");
return hsu; } static void closeConn(HttpsURLConnection c)
{
c.disconnect();
} }

注意红色那行改变的代码。

7.这时client端上的java client端应该就可以成功得到TGT和ST了。

05-11 11:28