您的位置:首页 > 其它

配置简单cas客户端

2016-02-26 16:48 295 查看
配置简单cas客户端
下载cas-client(地址:http://downloads.jasig.org/cas-clients/)然后解压cas-client-3.1.12.zip,在modules文件夹中有需要的jar包,请根据自己的项目情况选择使用,把相应的jar包放到你项目WEB-INF/lib下

环境准备:

为了更实际的测试单点登录,先在本地准备两个tomcat,一个用来做cas服务端,一个用来运行cas客户端应用,关于一个电脑上配置两个tomcat的具体细节可以参考博文:http://blog.csdn.net/cb2474600377/article/details/44178131。这里把使用端口8090的tomcat做cas认证服务端,使用端口8080的tomcat运行cas客户端应用。

关于cas服务端的配置可以参考上一篇博文:
http://blog.csdn.net/cb2474600377/article/details/50732343
这里cas服务器的配置地址为:http://localhost:8090/cas,先检验该认证服务能否正常登录认证。

cas客户端配置:

1.新建一个web项目casClient,作为cas客户端应用demo。然后项目webContent目录下添加index.jsp做项目的默认页面。如下:

<%@page import="org.jasig.cas.client.util.AssertionHolder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is a page.<br/>
<%
String userName = AssertionHolder.getAssertion().getPrincipal().getName();
%>
用户名:<%=userName %>
<br/>
<%
String name = request.getRemoteUser();
%>
用户名:<%=name %>
</body>
</html>


然后配置web.xml,如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>casClient</display-name>
<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
<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>http://localhost:8090/cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:8080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<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:8090/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost: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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


2.将该客户端项目导包成war包,复制到cas客户端服务器中的tomcat/webapps目录下

3.先运行cas服务端tomcat,再运行cas客户端tomcat

注意:如果cas客户端出现如下错误:



则需要修改cas客户端tomcat的配置文件,打开tomcat/conf/context.xml,在<context>中添加

<Loader delegate="true" />配置,如图



4.再访问http://localhost:8080/casClient/index.jsp测试,将会先跳到验证界面,登录后转到index.jsp页面,同时看到登录时的用户名

参考博文:http://blog.csdn.net/small_love/article/details/6664831
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: