CAS做单点登陆(SSO)——集成Java Web 项目

来源:互联网 发布:电脑编程游戏 编辑:程序博客网 时间:2024/06/08 15:01

添加cas-client的jar包

下载cas-client,地址:http://www.ja-sig.org/downloads/cas-clients/当前最新版本是cas-client-3.2.1-release.zip然后解压cas-client-3.2.1-release.zip,在modules拷贝cas-client-core-3.2.1.jar应用的WEB-INF/lib目录中

撰写支持CAS集成的客户化包

除了在web.xml添加CAS内置的filter外(具体看配置web.xml),我们需要撰写自己支持CAS集成的客户化包。大致思路如下:

@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest)servletRequest;HttpServletResponse response = (HttpServletResponse)servletResponse;HttpSession session = request.getSession();//在session中自定义一个参数,以它来校验是否完成过自动登陆Object user_login = session.getAttribute(AURORA_USER_LOGIN);if (user_login != null){//登陆过,就继续执行其他filterfilterChain.doFilter(request, response);return;}//通过CAS的API获得登陆账号String loginName = AssertionHolder.getAssertion().getPrincipal().getName();try {//执行本系统的登陆。跟平常同时校验用户名和密码不同,这里只有用户名。executeLoginProc(request,response,loginName);} catch (Exception e) {logger.log(Level.SEVERE, "executeLoginProc error:", e);return;}//登陆成功session.setAttribute(AURORA_USER_LOGIN, Boolean.TRUE);//跳转到登陆成功后的页面response.sendRedirect(roleSelectPageUrl);}

把这个class打包成一个jar拷贝到应用的WEB-INF/lib目录中

如果有兴趣,还可以简单了解下org.jasig.cas.client.authentication.AuthenticationFilter这个CAS内置filter的功能

public final void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {    HttpServletRequest request = (HttpServletRequest)servletRequest;    HttpServletResponse response = (HttpServletResponse)servletResponse;    HttpSession session = request.getSession(false); //检查自定义属性"_const_cas_assertion_"    Assertion assertion = session != null ? (Assertion)session.getAttribute("_const_cas_assertion_") : null;

    if (assertion != null) {   //已经成功登陆过CAS      filterChain.doFilter(request, response);      return;    }  //拿到url,并检查url参数中的ticket是否有效    String serviceUrl = constructServiceUrl(request, response);    String ticket = CommonUtils.safeGetParameter(request, getArtifactParameterName());    boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);

    if ((CommonUtils.isNotBlank(ticket)) || (wasGatewayed)) {   //ticket有效      filterChain.doFilter(request, response);      return;    }

    this.log.debug("no ticket and no assertion found");    String modifiedServiceUrl;    String modifiedServiceUrl;    if (this.gateway) {      this.log.debug("setting gateway attribute in session");      modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl);    } else {      modifiedServiceUrl = serviceUrl;    }

    if (this.log.isDebugEnabled()) {      this.log.debug("Constructed service url: " + modifiedServiceUrl);    }

    String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);

    if (this.log.isDebugEnabled()) {      this.log.debug("redirecting to \"" + urlToRedirectTo + "\"");    }  //重定向到cas的登陆页面    response.sendRedirect(urlToRedirectTo);  }


 

修改web.xml

在应用WEB-INF/web.xml添加filter的内容,效果如下所示

<!-- ======================== 单点登录开始 ======================== --><!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置--><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><param-value>https://sso.aurora-framework.org:8080/cas/login</param-value><!--这里的server是服务端的IP--></init-param><init-param><param-name>serverName</param-name><param-value>https://sso.aurora-framework.org:8080</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>https://sso.aurora-framework.org:8080/cas</param-value></init-param><init-param><param-name>serverName</param-name><param-value>https://sso.aurora-framework.org:8080</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><!-- 自动根据单点登录的结果设置本系统的用户信息--><filter><display-name>AutoSetUserAdapterFilter</display-name><filter-name>AutoSetUserAdapterFilter</filter-name><filter-class>aurora.plugin.sso.cas.AutoSetUserFilter</filter-class><init-param><param-name>roleSelectPageUrl</param-name><param-value>https://sso.aurora-framework.org:8080/yourapp/role_select.screen</param-value></init-param></filter><filter-mapping><filter-name>AutoSetUserAdapterFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- ======================== 单点登录结束 ======================== -->

 

前面几个都是CAS标准配置,最后一个AutoSetUserAdapterFilter(自定义,可以取其他任意名字)才是我们支持cas的客户化程序。其中roleSelectPageUrl是指用户完成单点登录后跳转的页面

本文档撰写时java web项目和CAS用同一个tomcat,所以都用的https。否则只需要配置CAS的链接为HTTPS,本项目连接用HTTP。

 

修改CAS的认证逻辑

CAS默认的逻辑是用户名和密码一致就可以登陆,现在需要把原web系统的用户名和密码校验挪到CAS中。这里假设原先web系统中有一张sys_user表存储了用户名和MD5散列后的密码。

 

打开cas/WEB-INF/deployerConfigContext.xml

  1. 注释掉SimpleTestUsernamePasswordAuthenticationHandler这个Handler,并添加

     

    <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler"><property ref="dataSource" name="dataSource"></property><property name="sql" value="select t.encrypted_user_password from sys_user t where t.user_name=?"></property>                     <property ref="MD5PasswordEncoder" name="passwordEncoder"></property></bean>


     

  2.  在文件末尾之前加入数据库的链接:

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName">            <value>oracle.jdbc.driver.OracleDriver</value>        </property>        <property name="url">            <value>jdbc:oracle:thin:@yourIP:1521:yourOracleInstanceId</value>        </property>        <property name="username">            <value>yourName</value>        </property>        <property name="password">            <value>yourPassword</value>        </property>    </bean>    <bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">        <constructor-arg index="0">            <value>MD5</value>        </constructor-arg>    </bean>

     

  3. cas加入jdbc支持
    复制cas-server-3.5.2\modules\cas-server-support-jdbc-3.5.2.jaroracle驱动(这里采用oracle数据)的ojdbc14.jar或者classes12.jar放到cas/WEB-INF/lib目录下。

  4. 重新登陆Web系统

    重启tomcat,在浏览器中输入https://sso.aurora-framework.org:8080/yourapp/,自动跳转到如下页面:

  5. 输入web系统预先定义的用户名和密码,并跳转到自定义(web.xml中定义的)登陆成功后的页面。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 全屏钢化膜总是翘边怎么办 华为麦芒屏幕触屏失灵怎么办 华为麦芒5运行慢怎么办 手机屏保密码忘记了怎么办 麦芒5密码锁忘了怎么办 超薄手机壳松了怎么办 华为麦芒5声音小怎么办 笔记本外壳a面裂了怎么办 苹果手机外壳摔坏了怎么办 挂衣服肩膀出包怎么办 摩拜单车手机号注销了怎么办 摩拜单车手机号码换了怎么办 摩拜单车换手机号码打不开怎么办 摩拜单车丢了怎么办 摩拜单车忘锁了怎么办 透明手机壳粘指纹怎么办 tpu手机壳变黄了怎么办 0pp0手机声音小怎么办 橡胶皮套晒坏了怎么办 宝宝晚上吹空调发烧怎么办 玩手机手指尖疼怎么办 手机型号不支持微信运动怎么办 手腕向下压会疼怎么办 手腕韧带拉伤怎么办恢复快 华为手机用车载充电慢怎么办 华为麦芒6充电慢怎么办 oppo手机压弯了怎么办 麦芒5电池不耐用怎么办 华为7x照相模糊怎么办 华为麦芒6照相虚怎么办 荣耀8gps信号弱怎么办 华为麦芒4手机卡顿怎么办 华为麦芒4玩游戏卡怎么办 sim卡换卡通讯录丢了怎么办 换sim卡通讯录怎么办 麦芒4开不了机怎么办 麦芒5开不了机怎么办 麦芒6针丢了怎么办 麦芒6扬声器坏了怎么办 华为手机2s太卡怎么办 华为麦芒6网速慢怎么办