1. 程式人生 > >CAS 4.0 配置開發手冊

CAS 4.0 配置開發手冊

tab mysql 獲得 secure enter max 否則 exceptio track

1 下載

地址http://downloads.jasig.org/

cas-server-4.0.0-release.tar.gz

cas-client-3.3.3-release.tar.gz

2 配置

解壓cas-server-4.0.0。將當中module/cas-server-webapp-4.0.0.war拷貝到Tomcat的webapps文件夾下,重命名為cas.war。啟動Tomcat解開壓縮。

2.1 CAS的HTTP模式與HTTPS設置

1)cas\WEB-INF\deployerConfigContext.xml。新增p:requireSecure="false"

<bean id="proxyAuthenticationHandler" class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"

p:httpClient-ref="httpClient" p:requireSecure="false"/>

2)cas\WEB-INF\spring-configuration

ticketGrantingTicketCookieGenerator.xml設置p:cookieSecure="false"

warnCookieGenerator.xml設置p:cookieSecure="false"

http://localhost:8080/cas。進入登錄頁面。

默認用戶為casuser/Mellon,登錄成功即配置完畢。

2.2 設置利用數據庫來驗證用戶

需依賴:c3p0-0.9.1.2.jar。mysql-connector-java-5.1.21.jar。cas-server-support-jdbc-4.0.0.jar

cas\WEB-INF\deployerConfigContext.xml

1)更換驗證方式

<!--

<bean id="primaryAuthenticationHandler"

class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">

<property name="users">

<map>

<entry key="casuser" value="Mellon"/>

</map>

</property>

</bean>

-->

<!-- Define the DB Connection -->

<bean id="dataSource"

class="com.mchange.v2.c3p0.ComboPooledDataSource"

p:driverClass="com.mysql.jdbc.Driver"

p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/hztraffic?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"

p:user="root"

p:password="root" />

<!-- Define the encode method-->

<!--<bean id="passwordEncoder"

class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">

<constructor-arg value="MD5"/>

</bean> -->

<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 hztraffic.user_data where name=?

and used=1"

p:passwordEncoder-ref="passwordEncoder"/>

<!-- p:passwordEncoder-ref="passwordEncoder" -->

2)更換驗證Handle

<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" />

<!-- <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" /> -->

</map>

</constructor-arg>

http://localhost:8080/cas,進入登錄頁面。

默認用戶為casuser/Mellon,登錄成功即配置完畢。

3 編譯

3.1 Eclipse導入project

3.2 License文件設置

Failed goal com.mycila.maven-license-plugin

註意src文件夾與cas-server-webapp平級,此外,不論什麽關於Sever文件的改動,文件頭的License聲明須要加入。

JSP/JS/CSS。否則編譯檢測通過不了。

<%--

Licensed to Jasig under one or more contributor license

agreements. See the NOTICE file distributed with this work

for additional information regarding copyright ownership.

Jasig licenses this file to you under the Apache License,

Version 2.0 (the "License"); you may not use this file

except in compliance with the License. You may obtain a

copy of the License at the following location:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,

software distributed under the License is distributed on an

"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

KIND, either express or implied. See the License for the

specific language governing permissions and limitations

under the License.

--%>

4 定制

4.1 登錄/登出頁面

4.2 同意退出後重定向

5 接入

5.1 WEB接入

5.1.1 非Spring模式

在ClientprojectWEB-INF/lib下加入cas-client-core-3.2.1.jar包。

改動web.xml例如以下:

<!-- ======================== 單點登錄/登出 ======================== -->

<!-- 該過濾器用於實現單點登出功能,可選配置。 -->

<filter>

<filter-name>CAS Single Sign Out Filter</filter-name>

<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>

</filter>

<!-- 該過濾器負責用戶的認證工作,必須啟用它 -->

<filter>

<filter-name>CAS Authentication Filter</filter-name>

<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>

<init-param>

<param-name>casServerLoginUrl</param-name>

<param-value>https://localhost:8443/cas/login</param-value>

</init-param>

<init-param>

<param-name>serverName</param-name>

<param-value>http://localhost:8080</param-value>

</init-param>

</filter>

<!-- 該過濾器負責對Ticket的校驗工作,必須啟用它 -->

<filter>

<filter-name>CAS Validation Filter</filter-name>

<filter-class>org.jasig.cas.client.validation.Cas10TicketValidationFilter</filter-class>

<init-param>

<param-name>casServerUrlPrefix</param-name>

<param-value>https://localhost:8443/cas</param-value>

</init-param>

<init-param>

<param-name>serverName</param-name>

<param-value>http://localhost:8080</param-value>

</init-param>

<init-param>

<param-name>redirectAfterValidation</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<!-- 該過濾器負責實現HttpServletRequest請求的包裹,

比方同意開發人員通過HttpServletRequestgetRemoteUser()方法獲得SSO登錄用戶的登錄名。可選配置。 -->

<filter>

<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>

</filter>

<!-- 該過濾器使得開發人員能夠通過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 Single Sign Out Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CAS Authentication Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CAS Validation Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter-mapping>

<filter-name>CAS Assertion Thread Local Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<listener>

<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>

</listener>

<!-- ======================== 單點登錄/登出結束 ======================== -->

如今執行Clientproject,首次訪問任一頁面就會跳轉到https://localhost:8443/cas/login進行認證。同一時候,把你的退出鏈接設置為:https://sso.wsria.com/cas/logout 就可以實現單點推出。

5.1.2 Spring方式

引用cas-client-core.jar

Web.xml。註意casSingleSignOutFilter必須位於最前面

<listener> <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>

</listener>

<!-- ======================== 單點登錄/登出 ======================== -->

<filter>

<filter-name>casSingleSignOutFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>casSingleSignOutFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter>

<filter-name>casAuthenticationFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>casAuthenticationFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter>

<filter-name>casTicketValidationFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>casTicketValidationFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter>

<filter-name>casHttpServletRequestWrapperFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>casHttpServletRequestWrapperFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<filter>

<filter-name>casAssertionThreadLocalFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>casAssertionThreadLocalFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- ======================== 單點登錄/登出結束 ======================== -->

appliationContext.xml

<bean id="casSingleSignOutFilter"

class="org.jasig.cas.client.session.SingleSignOutFilter" />

<bean name="casAuthenticationFilter"

class="org.jasig.cas.client.authentication.AuthenticationFilter"

p:casServerLoginUrl="http://localhost:9000/uq-w-cas/login"

p:renew="false"

p:gateway="false" p:serverName="http://localhost:8080" />

<bean name="casTicketValidationFilter"

class="org.jasig.cas.client.validation.Cas10TicketValidationFilter"

p:serverName="http://localhost:8080"

p:redirectAfterValidation="true">

<property name="ticketValidator">

<bean class="org.jasig.cas.client.validation.Cas10TicketValidator">

<!-- 相應於casServerUrlPrefix -->

<constructor-arg index="0" value="http://localhost:9000/uq-w-cas" />

</bean>

</property>

</bean>

<bean id="casHttpServletRequestWrapperFilter"

class="org.jasig.cas.client.util.HttpServletRequestWrapperFilter" />

<bean id="casAssertionThreadLocalFilter"

class="org.jasig.cas.client.util.AssertionThreadLocalFilter" />

頁面

<%

AttributePrincipal principal= (AttributePrincipal)request.getUserPrincipal();

String username = "i am username";

if(null!=principal){

username=principal.getName();

%>

<h1>登錄成功,這是client1啊</h1><br/>

用戶名:<%=username %><br/>

<a href="http://localhost:8989/Casclient2/index.jsp">進入客戶端2</a><br/>

<a href="http://localhost:9000/uq-w-cas/logout?service=http://localhost:9000/uq-w-cas/">退出</a><br/

<%

}

%>

5.2 client

project依賴例如以下:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.cookie.Cookie;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

public class CasUtil {

//Cas server address

static final String server = "http://localhost:9000/cas/login";

public static void main(String[] args) throws IOException {

//Login and get the cookie

Cookie cookie = getTicketGrantingTicket(server, "13082838818",

"13082838818");

if (cookie != null) {

System.out.println(cookie);

}

}

private static Cookie getTicketGrantingTicket(String server,

String username, String password) throws IOException {

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(server);

//Login parameters

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("username", username));

nvps.add(new BasicNameValuePair("password", password));

String[] dynamicPara = doCasLoginRequest(client, server);

nvps.add(new BasicNameValuePair("lt", dynamicPara[0]));

nvps.add(new BasicNameValuePair("execution", dynamicPara[1]));

nvps.add(new BasicNameValuePair("_eventId", "submit"));

//Font Code

post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

try {

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();

if (entity != null) {

//CASTGC: the default cookie name

Cookie cookie = getCookieValue(client, "CASTGC");

entity.consumeContent();

return cookie;

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

private static Cookie getCookieValue(DefaultHttpClient httpclient,

String name) {

List<Cookie> cookies = httpclient.getCookieStore().getCookies();

if (cookies.isEmpty()) {

return null;

} else {

for (int i = 0; i < cookies.size(); i++) {

Cookie cookie = cookies.get(i);

if (cookie.getName().equalsIgnoreCase(name)) {

return cookie;

}

}

}

return null;

}

//Simulate the login action and get the dynamic parameters lt and execution

private static String[] doCasLoginRequest(DefaultHttpClient httpclient,

String url) throws IOException {

String[] result = new String[2];

HttpGet httpget = new HttpGet(url);

HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

BufferedReader rd = new BufferedReader(new InputStreamReader(

entity.getContent(), "UTF-8"));

String tempLine = rd.readLine();

String sLt = "<input type=\"hidden\" name=\"lt\" value=\"";

String sEx = "<input type=\"hidden\" name=\"execution\" value=\"";

while (tempLine != null) {

int iLt = tempLine.indexOf(sLt);

int iEx = tempLine.indexOf(sEx);

if (iLt != -1) {

String s1 = tempLine.substring(iLt + sLt.length());

int index1 = s1.indexOf("\"");

if (index1 != -1)

result[0] = s1.substring(0, index1);

}

if (iEx != -1) {

String s1 = tempLine.substring(iEx + sEx.length());

int index1 = s1.indexOf("\"");

if (index1 != -1)

result[1] = s1.substring(0, index1);

}

tempLine = rd.readLine();

}

if (entity != null) {

entity.consumeContent();

}

return result;

}

//Cookie convert

private javax.servlet.http.Cookie convertToServletCookie(Cookie cookie) {

javax.servlet.http.Cookie retCookie = new javax.servlet.http.Cookie(

cookie.getName(), cookie.getValue());

retCookie.setComment(cookie.getComment());

retCookie.setDomain(cookie.getDomain());

retCookie.setHttpOnly(false);

retCookie.setSecure(false);

retCookie.setPath(cookie.getPath());

retCookie.setVersion(cookie.getVersion());

retCookie.setMaxAge((int) ((cookie.getExpiryDate().getTime() - System

.currentTimeMillis()) / 1000));

return retCookie;

}

}

部分圖片未上傳。如有須要,請給我留言。

CAS 4.0 配置開發手冊