SSO(单点登录)、CAS
度娘、谷妹上一搜一大把,在这里就不赘述辽
Demo
本地 Tomcat 部署CAS 服务器
cas-server-webapp-4.0.0.war 下载:
- cas-server-webapp-4.0.0.war,将其改名为cas.war (非必须,易于后续操作)后直接放置在/tomcat/webapps下。
- 启动tomcat服务(双击/tomcat/bin/startup.bat),出现"Catalina start Server startup in xxxx ms"代表启动成功。
- 浏览器输入http://localhost:8080/cas,可看到默认登录页面,默认账号:casuser 默认密码:Mellon
Demo客户端[1]部署
创建Maven工程 (war)demo_casclient
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.icnfox</groupId>
<artifactId>demo_casclient</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- war包项目 -->
<packaging>war</packaging>
<properties>
<webVersion>3.0</webVersion>
</properties>
<dependencies>
<!--cas-->
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.3.3</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--maven plugins-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--tomcat plugin-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8010</port>
<path>/</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置web.xml(自行创建创建webapp/WEB-INF/web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-->>>>>>>>>>>>>>>>>>>>>>单点退出配置开始-->
<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!-- 该过滤器用于实现单点登出功能,可选配置 -->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-->>>>>>>>>>>>>>>>>>>>>>单点退出配置结束-->
<!-->>>>>>>>>>>>>>>>>>>>>>认证票据管理配置开始-->
<!-- 该过滤器负责用户的认证工作,必须启用它 -->
<filter>
<filter-name>CASFilter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<!--这里的server是服务端的IP:认证服务中心地址(这里是将CAD放置在本地的tomcat容器中) -->
<param-value>http://localhost:8080/cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<!--当前Demo项目的tomcat地址-->
<param-value>http://localhost:8010</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
<!-- 发 '票' 及 '票' 的认证-->
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>http://localhost:8080/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:8010</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-->>>>>>>>>>>>>>>>>>>>>>认证票据管理配置结束-->
<!-->>>>>>>>>>>>>>>>>>>>>>登录成功后回显用户信息 配置开始-->
<!-- 该过滤器负责实现HttpServletRequest请求的包裹, 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置 -->
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>
org.jasig.cas.client.util.HttpServletRequestWrapperFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器使开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 比如AssertionHolder.getAssertion().getPrincipal().getName() -->
<filter>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-->>>>>>>>>>>>>>>>>>>>>>登录成功后回显用户信息 配置结束-->
</web-app>
配置欢迎页面 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>icnfox.cn</title>
</head>
<body>
欢迎
<%=request.getRemoteUser()%>
来到 cnfox'Blog
</body>
</html>
登录测试
- 启动项目(Maven --> demo_casclient --> Plugins --> tomcat7 -->
tomcat7:run) - 浏览器输入http://localhost:8010,可看到默认登录页面,默认账号:casuser 默认密码:Mellon
- 看到欢迎页面,测试成功
Demo客户端[2]部署
- 创建Maven工程 (war)demo_casclient2
参考上文pom.xml引入cas客户端依赖并指定tomcat运行端口为8011 - 创建web.xml,参照demo_casclient
,将serverName的值改为http://localhost:8011,一共两处 - 创建index.jsp,内容显示“欢迎xxx第二次来到...”
单点登录测试
- 启动部署cas的tomcat
- 启动客户端工程1、客户端工程2
- 地址栏输入http://localhost:8010/,地址会跳转到CAS登录页
- 输入用户名和密码后,页面跳转回8010
- 访问http://localhost:8011/,也可以打开主页面。
单点退出登录配置测试
将链接添加到index.jsp中
<a href="http://localhost:8080/cas/logout">退出登录</a>
退出登录自动跳转
修改/tomcat/webapp/cas/WEB-INF(本地tomcat下的文件夹)的配置文件cas-servlet.xml (cas.logout.followServiceRedirects:false-->true)
<bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"
p:servicesManager-ref="servicesManager"
p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>
改为true后,可以在退出时跳转页面到目标页面,修改index.jsp的退出链接:
<a href="http://localhost:8080/cas/logout?service=https://www.icnfox.cn">退出登录</a>
配置数据源
使用数据库中的用户名和密码进行登录,一共有三步
- 修改/tomcat/webapp/cas/WEB-INF(本地tomcat下的文件夹)的配置文件deployerConfigContext.xml,文件之前添加以下配置
标签内做以下修改 - 复制c3p0、mysql、cas的jar粘贴到本地tomcar/lib文件夹下
解释下原理是怎么实现的:
第一步的参数内容已经在下面代码注释中写的很清楚,因为使用了c3po、mysql、cas所以需要导入相应的jar包,这就是第三步的原理
第二步:我们查看配置文件发现,CAS的默认登录账号和密码配置在id=primaryAuthenticationHandler的bean中,选中id我们发现这个id还出现在了我们第二步配置的bean key-ref中,他的上上级标签是.
我们都知道constructor是构造方法的意思,这里就是spring的构造注入.上一级标签 id = authenticationManager(认证管理) 创建这个认证管理bean 的时候创建了两个构造方法形参 ,其中一个是代理,另外一个就是告诉CAS怎么做认证的,默认配置是走id=primaryAuthenticationHandler这个bean,现在我们配置了自己的数据源,当然要用自己的配置,所以才有了第二步
第一步添加内容
<!--
连接数据源的配置开始 我的上面是</util:list> 标签
-->
<!--
p:jdbcUrl=适用于Mysql数据库 填写正确的数据库地址及库名
p:user= 数据库用户名
p:password=数据库密码
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://10.10.127.75:3306/cnfox?characterEncoding=utf8"
p:user="root"
p:password="root"/>
<!--
用户名密码使用MD5加密的配置此bean 没有使用MD5加密可以去掉
-->
<bean id="passwordEncoder"
c lass="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
c:encodingAlgorithm="MD5"
p:characterEncoding="UTF-8"/>
<!--
配置文件
p:sql= 从库中查找用户名密码对应表的sql语句
p:passwordEncoder-ref= 没有使用MD5加密 可以去掉此项
-->
<bean id="dbAuthHandler"
class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"
p:dataSource-ref="dataSource"
p:sql="select password from tb_user where username = ?"
p:passwordEncoder-ref="passwordEncoder"/>
<!--
连接数据源的配置开始 我的下面是</beans> 标签
-->
第二步修改内容
<constructor-arg>
<map>
<!--
| IMPORTANT
| Every handler requires a unique name.
| If more than one instance of the same handler class is configured, you must explicitly
| set its name to something other than its default name (typically the simple class name).
-->
<entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver" />
</map>
</constructor-arg>
jar包下载:
不错|´・ω・)ノ
静悄悄的收藏博主的干货。。OωO
文章模板不错,在哪里可以下载呀1
评论之后文章中就会出现下载地址的
电商专用快递网站www.**.com单号无忧
博客模板很好,在哪里可以下载到的
付费主题呢 我友情链接里的第一个是主题作者 或者说加我qq(页面-时光机-有QQ) 我帮你去联系 老用户带说不定能便宜点
主题好看,文章也不错。
主要是人还帅气 治不了 了