CAS实现单点登录
1.单点登录的概念
比如我们做了一个平台,baidu,平台中有很多的产品,那么这些产品要共用一个登录系统来进行登录,这个登录系统就叫做单点登录SSO。
2.单点登录的配置
可以从GitHub上下载CAS的源码,但是打包工具要用gradle.
3>:安装Tomcat的配置,更改端口号为8066(自定义的端口号)
7>:测试:127.0.0.1:8066/cas/login出现登录页面
默认的用户名: casuser
在webapp/cas/WEB-INF/cas.properties文件中添加如下内容汉化:
G:\CASTomcat\apache-tomcat-7.0.84\webapps\cas\WEB-INF\view\jsp\default\ui\casLogoutView 更改中间的那一部分
G:\CAS Tomcat\apache-tomcat-7.0.84\webapps\cas\WEB-INF\view\jsp\default\ui\includes\top.jsp 修改顶部
G:\CAS Tomcat\apache-tomcat-7.0.84\webapps\cas\WEB-INF\view\jsp\default\ui\includes\bottom.jsp 修改的尾部
3>:SpringBoot整合CAS实现单点登录
1>:创建MAVEN工程,导入web、thymeleaf的依赖
<!--导入cas的包-->
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.4.0-GA</version>
</dependency>
#配置前缀
spring.thymeleaf.prefix=classpath:/templates/
#配置后缀
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#是否开启缓存
spring.thymeleaf.cache=false
## CAS 配置
cas.validation-type = CAS
#配置的是cas服务器的前缀,端口自定义
cas.server-url-prefix = http://127.0.0.1:8066/cas
#这个配置的是 cas登录的页面,端口自定义
cas.server-login-url = http://127.0.0.1:8066/cas/login
#配置的是cas的退出功能的路径,端口自定义
cas-server-logout-url = http://127.0.0.1:8066/cas/logout
#当前SpringBoot程序的主机和端口,端口自定义
cas.client-host-url = http://localhost:8080
3>:准备首页的URL地址
在工程的template目录下创建一个html文件index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
You made it!
</body>
</html>
4>:编写Controll来接受前端的请求
package com.nice.cas.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/toIndex")
public String toIndex(){
System.out.println("toIndex");
return "index";
}
}
5>:配置apache-tomcat-7.0.84\webapps\cas\WEB-INF目录下的deployerConfigContext.xml文件
详情见配置:直接将原文件的内容替换即可。但是数据库相关参数自定义配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:map id="authenticationHandlersResolvers">
<entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />
<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
</util:map>
<util:list id="authenticationMetadataPopulators">
<ref bean="successfulHandlerMetaDataPopulator" />
<ref bean="rememberMeAuthenticationMetaDataPopulator" />
</util:list>
<bean id="attributeRepository" class="org.jasig.services.persondir.support.NamedStubPersonAttributeDao"
p:backingMap-ref="attrRepoBackingMap" />
<!-- <alias name="acceptUsersAuthenticationHandler" alias="primaryAuthenticationHandler" /> -->
<alias name="personDirectoryPrincipalResolver" alias="primaryPrincipalResolver" />
<!--begin 从数据库中的用户表中密码读取出来出来之后要进行散列 -->
<bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
<constructor-arg value="MD5"/>
</bean>
<bean id = "queryDatabaseAuthenticationHandler" name="primaryAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="passwordEncoder" ref="MD5PasswordEncoder"/>
</bean>
<alias name="dataSource" alias="queryDatabaseDataSource" />
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://47.102.221.146:3306/casdb"
p:user="root"
p:password="mysql4586root"
p:initialPoolSize="6"
p:minPoolSize="6"
p:maxPoolSize="18"
p:maxIdleTimeExcessConnections="120"
p:checkoutTimeout="10000"
p:acquireIncrement="6"
p:acquireRetryAttempts="5"
p:acquireRetryDelay="2000"
p:idleConnectionTestPeriod="30"
p:preferredTestQuery="select 1" />
<!--end 从数据库中的用户表中读取 -->
<util:map id="attrRepoBackingMap">
<entry key="uid" value="uid" />
<entry key="eduPersonAffiliation" value="eduPersonAffiliation" />
<entry key="groupMembership" value="groupMembership" />
<entry>
<key><value>memberOf</value></key>
<list>
<value>faculty</value>
<value>staff</value>
<value>org</value>
</list>
</entry>
</util:map>
<alias name="serviceThemeResolver" alias="themeResolver" />
<alias name="jsonServiceRegistryDao" alias="serviceRegistryDao" />
<alias name="defaultTicketRegistry" alias="ticketRegistry" />
<alias name="ticketGrantingTicketExpirationPolicy" alias="grantingTicketExpirationPolicy" />
<alias name="multiTimeUseOrTimeoutExpirationPolicy" alias="serviceTicketExpirationPolicy" />
<alias name="anyAuthenticationPolicy" alias="authenticationPolicy" />
<alias name="acceptAnyAuthenticationPolicyFactory" alias="authenticationPolicyFactory" />
<bean id="auditTrailManager"
class="org.jasig.inspektr.audit.support.Slf4jLoggingAuditTrailManager"
p:entrySeparator="${cas.audit.singleline.separator:|}"
p:useSingleLine="${cas.audit.singleline:false}"/>
<alias name="neverThrottle" alias="authenticationThrottle" />
<util:list id="monitorsList">
<ref bean="memoryMonitor" />
<ref bean="sessionMonitor" />
</util:list>
<alias name="defaultPrincipalFactory" alias="principalFactory" />
<alias name="defaultAuthenticationTransactionManager" alias="authenticationTransactionManager" />
<alias name="defaultPrincipalElectionStrategy" alias="principalElectionStrategy" />
<alias name="tgcCipherExecutor" alias="defaultCookieCipherExecutor" />
</beans>
6>:配置cas.properties文件
cas.jdbc.authn.query.sql=select password from userpassword where username=?
#accept.authn.users=casuser::Mellon
G:\CAS Tomcat\apache-tomcat-7.0.84\webapps\cas\WEB-INF\classes\services\HTTPSandIMAPS-10000001.json文件添加”””http”
"@class" : "org.jasig.cas.services.RegexRegisteredService",
"serviceId" : "^(https|imaps|http)://.*",
8>:导包
9>:在SpringBoot上的启动文件上 添加如下的注解
浏览器输入: localhost:8080/toIndex会跳转到cas的认证页面:
输入自己存在数据库的账户密码之后成功跳转,就成功了。