由于多个项目需要帐号的互通,所以一开始就是用cas去做的,不得不说cas要配置的东西挺多的,但是项目安全性不需要太高,所以没有做https的请求,也就是没有弄证书,这虽然省了很多时间和精力,但是项目之间的安全性降低了不少。
1.从官网http://www.jasig.org下载CAS Server, 将cas-server-webapp-3.4.12.war解压, 可以看到是一个标准的java的web应用, 可以直接部署到tomcat的webapps目录下的,我这里假设部署的路径是{tomcat_home}/webapps/cas。
2. CAS默认需要tomcat配置SSL协议,使用https协议通信的。 由于项目是企事业单位内部的系统,不需要这么高的安全级别, 可以简化操作,不走SSL协议。修改下配置文件\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml, 如下, 将默认的true改成false即可。
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
3.配置登录的验证逻辑, 修改配置文件cas\WEB-INF\deployerConfigContext.xml。在authenticationHandlers中配置验证方式,我这里配置数据库查询语句来实现用户名和密码的验证。
p:httpClient-ref="httpClient" />
密码加密方法我这里使用MD5, 配置passwordEncoder的bean
在配置一个名称为dataSource的数据源
数据源的配置根据自己的实际情况来配置, 需要的jar如果lib下面没有,自己复制进去, 不然数据源连不上报错。
4. 现在服务端就配置好了, 如果需要定制登录/登出页面的话(实际项目基本上都需要修改), 修改cas\WEB-INF\view\jsp\default\ui\下面的casLoginView.jsp和casLogoutView.jsp就可以了
然后就是java客户端的配置
1. 从官网下载CAS Client, 将客户端的jar,如cas-client-core-3.2.1.jar引入到web应用程序的classpath中
2 .配置web.xml文件, 主要是添加过滤器拦截通信, 下面的实例代码, 假设web应用程序的端口是8080
CAS Single Sign Out Filter
org.jasig.cas.client.session.SingleSignOutFilter
CAS Single Sign Out Filter
/*
org.jasig.cas.client.session.SingleSignOutHttpSessionListener
CASFilter
org.jasig.cas.client.authentication.AuthenticationFilter
casServerLoginUrl
http://CAS服务端所在服务器IP:8080/cas/login
serverName
http://具体web应用程序所在服务器IP:8080
CASFilter
/*
CAS Validation Filter
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
casServerUrlPrefix
http://CAS服务端所在服务器IP:8080/cas
serverName
http://具体web应用程序所在服务器IP:8080
renew
false
gateway
false
CAS Validation Filter
/*
CAS HttpServletRequest Wrapper Filter
org.jasig.cas.client.util.HttpServletRequestWrapperFilter
CAS HttpServletRequest Wrapper Filter
/*
CAS Assertion Thread Local Filter
org.jasig.cas.client.util.AssertionThreadLocalFilter
CAS Assertion Thread Local Filter
/*
CasForInvokeContextFilter
com.cm.demo.filter.CasForInvokeContextFilter
appId
a5ea611bbff7474a81753697a1714fb0
CasForInvokeContextFilter
/*
4. 注意上步配置文件中,过滤器CasForInvokeContextFilter的实现是需要在具体的应用中实现的,他的目的是, CAS服务端登录验证成功后,会将登录用户的用户名携带回来, 这时客户端web应用程序需要根据用户名从数据库用户表中查询到用户的Id等信息, 并填充到Session中, 这样,客户端应用程序原来的验证逻辑就不会出问题了, 因为我们一般都是通过验证session中是否含有当前登录的用户的ID来进行登录验证的。
下面是CasForInvokeContextFilter的一个简单实现。
/**
* 该过滤器用户从CAS认证服务器中获取登录用户用户名,并填充必要的Session.
* @author jiarong_cheng
* @created 2012-7-12
*/
public class CasForInvokeContextFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
//如果session中没有用户信息,则填充用户信息
if (session.getAttribute("j_userId") == null) {
//从Cas服务器获取登录账户的用户名
Assertion assertion = AssertionHolder.getAssertion();
String userName = assertion.getPrincipal().getName();
try {
//根据单点登录的账户的用户名,从数据库用户表查找用户信息, 填充到session中
User user = UserDao.getUserByName(userName);
session.setAttribute("username", userName);
session.setAttribute("userId", user.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
}
}
到此,就完成了, 当你访问具体应用的网址, 如http://具体应用IP: 8080/ ,就会跳转到CAS服务器的登陆页面: http://CAS服务器IP: 8080/ 进行登录验证, 验证通过后, 又会跳转回应用的网址。
新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,将docs/examples/example_simple.php
复制到工程中,修改如下:
//
// phpCAS simple client
//
// import phpCAS lib
include_once('CAS.php');
phpCAS::setDebug();
// initialize phpCAS
phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');
// no SSL validation for the CAS server
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().
// logout if desired
if (isset($_REQUEST['logout'])) {
$param=array("service"=>"http://localhost/Phpcasclient1/example_simple.php");//退出登录后返回
phpCAS::logout($param);
}
// for this test, simply print that the authentication was successfull
?>
phpCAS simple clientSuccessfull Authentication!这是客户端1
the user's login is <?php echo phpCAS::getUser(); ?>.
phpCAS version is <?php echo phpCAS::getVersion(); ?>.
去java客户端1
退出
php配置需要开启php_curl,可以复制Phpcasclient1为Phpcasclient2
php单点登录成功,这时再访问java客户端发现也不需要登录,php和java应用之间单点登录成功。
注:php的phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');地址需要和java的web.xml中的cas服务器地址一致,我开始一个写的ip:192.168.18.8,一个写的localhost,
php和java总是不能同步登录,郁闷了好久
----------------到这里java和php的客户端已经配置完成,现在你会发现php和java之间不能单点登出,php端退出java客户端也退出,反之java退出但是php却没有同步退出
这里需要做一个配置,在
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
这里加上
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::handleLogoutRequests(); 这里会检测服务器端java退出的通知,就能实现php和java间同步登出了。
phpCAS::forceAuthentication();
在这里还有一个坑,就是假如你的php项目是用函数式的退出和登录方式,也就是按照cookie去验证的,所以在java端退出后,php端不会退出。
所以这里需要对你的验证方式进行修改,至于修改方式要根据你的项目来看。
附上文件
发现不知道在哪上传文件 大家有需要可以去百度 这些资源都很好找的 有抱歉之处请见谅= =