单点登陆解决方案-CAS

什么是单点登录

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案
之一。SSO 的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应
用系统。
我们目前的系统存在诸多子系统,而这些子系统是分别部署在不同的服务器中,那么使
用传统方式的 session 是无法解决的,我们需要使用相关的单点登录技术来解决。

流程:
    客户端  服务器  认证中心   客户端发送请求到 服务器 服务器跳到认证中心 检验是否登录 
    登录就放行 返回到你要访问的页面,如果没有登录就跳到你设定的登录页面进行登录
    登录之后 给票据令牌服务器 , 服务器再给客户端, 底层其实就是cookie  
    就像做车买票,上车买票,想坐哪坐哪,到站下车,就是关闭客户端,再上车就得再买票,就是这个板子;
    
    
    
    //配置相关
    
    在资源\cas\source\cas-server-4.0.0-release\cas-server-4.0.0\modules 目录下
        cas-server-webapp-4.0.0.war 将其改名为 cas.war 放入 tomcat 目录下的 webapps 下。启动
    tomcat 自动解压 war 包。浏览器输入 http://localhost:8080/cas/login ,可看到登录页面
    这里有个固定的用户名和密码 casuser /Mellon

    如果我们不希望用 8080 端口访问 CAS, 可以修改端口
    
    打开 tomcat 目录 conf\server.xml 找到下面的配置将端口 8080,改为 9100
    
    修改 CAS 配置文件
    
1**修改 cas 的 WEB-INF/cas.properties
    server.name=http://localhost:9100
    
2**    修改 cas 的 WEB-INF/deployerConfigContext.xml  添加p:requireSecure="false" 不启动HTTPS  
        <bean id="proxyAuthenticationHandler"
          class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
          p:httpClient-ref="httpClient"   p:requireSecure="false">
3** 修改 cas 的/WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
//p:cookieSecure 设置为false 为不启动 https  p:cookieMaxAge="3600" 是COOKIE 的最大生命周期  意思是3600秒内关闭或打开任意窗口  不需要验证

<bean id="ticketGrantingTicketCookieGenerator"
class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"                   
p:cookieMaxAge="3600"
p:cookieName="CASTGC"
p:cookiePath="/cas" />        
</beans>

4**修改 cas 的 WEB-INF/spring-configuration/warnCookieGenerator.xml  
<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
        p:cookieSecure="false"
        p:cookieMaxAge="3600"
        p:cookieName="CASPRIVACY"
        p:cookiePath="/cas" />
</beans>

5**引入依赖 配置端口 原生
        <dependency>  
            <groupId>org.jasig.cas.client</groupId>  
            <artifactId>cas-client-core</artifactId>  
            <version>3.3.3</version>  
        </dependency>          
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>  
            <scope>provided</scope>
        </dependency>

         <build>  
      <plugins>
          <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-compiler-plugin</artifactId>  
              <version>2.3.2</version>  
              <configuration>  
                  <source>1.8</source>  
                  <target>1.8</target>  
              </configuration>  
          </plugin>  
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>9002</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
      </plugins>  
    </build>

6**加入web.xml配置
            web.xml中需要修改的地方
             <!-- 该过滤器负责用户的认证工作,必须启用它 AuthenticationFilter认证过滤器类   -->  
    <filter>  
        <filter-name>CASFilter</filter-name>  
        <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
        <init-param>  
            <param-name>casServerLoginUrl</param-name>  
            <param-value>http://localhost:9100/cas/login</param-value>    这里是CAS认证中心的地址
            <!--这里的server是服务端的IP -->  
        </init-param>  
        <init-param>  
            <param-name>serverName</param-name>  
            <param-value>http://localhost:9002</param-value>              这个是本web的地址
        </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:9100/cas</param-value>  这里是CAS认证中心的地址区别在于没有/login
        </init-param>  
        <init-param>  
            <param-name>serverName</param-name>  
            <param-value>http://localhost:9002</param-value>  这个是本web的地址
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CAS Validation Filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    
    
    7** 单点退出
                地址栏输入 http://localhost:9100/cas/logout 可以实现退出
                
        实现退出跳转:
        
            修改 cas 系统的配置文件 cas-servlet.xml 改修为true
         <bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"
                p:servicesManager-ref="servicesManager"
                p:followServiceRedirects="${cas.logout.followServiceRedirects:true}"/>
    
    修改之后页面部分:?后面填入跳转网页
    
    <a href="http://localhost:9100/cas/logout?service=http://www.baidu.com">退出登录</a>
    
    
    8**从数据库中获取账号密码
    修改 cas 服务端中 web-inf 下 deployerConfigContext.xml ,添加如下配置    passwordEncoder是指密码是加密的  需要配置这个
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 p:driverClass="com.mysql.jdbc.Driver"
 p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/pinyougoudb?characterEncoding=utf8"
 p:user="root"
 p:password="root" />
<bean id="passwordEncoder"
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"
c:encodingAlgorithm="MD5"
p:characterEncoding="UTF-8" />
<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"/> 
 
 
 //并修改:成dbAuthHandler
 <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
        <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包放入webapps\cas\WEB-INF\lib 下

文件夹jar

9***改修登录界面

(1)将品优购的登陆页 login.html 拷贝到 cas 系统下 WEB-INF\view\jsp\default\ui 目录下
(2)将 css js 等文件夹拷贝到 cas 目录下
(3) 将原来的 casLoginView.jsp 改名(可以为之后的修改操作做参照),将 login.html 改
名为 casLoginView.jsp 

将原来登录界面的样式文件 css  img js  plugins 文件拷贝到WEBapp/cas下

在品优购 的jsp 登录界面中:
<%@ page pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


(2)修改 form 标签  这里所有什么都不能改 只能改样式
<form:form method="post" id="fm1" commandName="${commandName}" htmlEscape="true" class="sui-form"></form:form>


(3)修改用户名输入框  
<form:input id="username" tabindex="1"
accesskey="${userNameAccessKey}" path="username" autocomplete="off" htmlEscape="true"
placeholder="邮箱/用户名/手机号" class="span2 input-xfat" />

(4)修改密码框
<form:password id="password" tabindex="2" path="password"
 accesskey="${passwordAccessKey}" htmlEscape="true" autocomplete="off"
 placeholder="请输入密码" class="span2 input-xfat" />
 
 
 (5)修改登陆按钮提交表单
<input type="hidden" name="lt" value="${loginTicket}" />
<input type="hidden" name="execution" value="${flowExecutionKey}" />
<input type="hidden" name="_eventId" value="submit" />
<input class="sui-btn btn-block btn-xlarge btn-danger" accesskey="l" value="登陆" type="submit" />

在表单内加入错误提示框
<form:errors path="*" id="msg" cssClass="errors" element="div" htmlEscape="false" />
并修改:

WEB-INF\classes 目录下messages.properties 文件中添加

authenticationFailure.AccountNotFoundException=Invalid credentials.
authenticationFailure.FailedLoginException=Invalid credentials.


并修改 cas-servlet.xml
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
p:defaultLocale="zh_CN" />

将此信息拷贝到 messages_zh_CN.properties 下
authenticationFailure.AccountNotFoundException=\u7528\u6237\u4E0D\u5B58\u5728.
authenticationFailure.FailedLoginException=\u5BC6\u7801\u9519\u8BEF.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值