---
title: "Jasig CAS使用手札——一、了解Jasig CAS，简单运行！"
author: "snowolf"
date: "2010-08-10T17:13:12Z"
tags: []
categories: ["编程语言"]
slug: "jasig-cas-jasig-cas"
draft: false
lastmod: "2010-08-10T17:20:00Z"
id: 733882
url: "https://snowolf.iteye.com/blog/733882"
---

**SSO ： 单点登录（Single Sign On , 简称 SSO ）**
**CAS ： CAS(Central Authentication Service)是一款不错的针对 Web 应用的单点登录框架**
有关CAS、SSO的详细信息我就不在这里罗嗦了，其实都是很简单理论，如果日后有时间，补充几张图，大家就很明白了～ :D 
今天要讨论的是了解[Jasig CAS](http://www.jasig.org/cas)这个成熟的系统，并让它能够运行。当然，如果要让它正式投入使用，还是需要一点时间的！ :D 
首先，[下载](http://www.jasig.org/cas_server_3_4_2_1)cas_server当前版本为3.4.2.1。
![](http://dl.iteye.com/upload/attachment/290663/97ca5ecb-3489-3127-9f2e-5c0f10d3df0e.jpg)
然后，我们来了解下这个压缩包！ :D 
打开这个压缩包，真是物产丰富。
![](http://dl.iteye.com/upload/attachment/290665/8ad0dcf0-97d4-3670-a702-69b347ee6edf.jpg)
我们先不关注其它内容，单看这个modules，单看modules中的cas-server-webapp-3.4.2.1.war
![](http://dl.iteye.com/upload/attachment/290667/ec941651-ac34-36cd-bed9-f900218a5eb4.jpg)
我们从这里入手！ :D 
![](http://dl.iteye.com/upload/attachment/290669/20d43e51-611b-33a6-b19a-e9a24e72f5b9.jpg)
把cas-server-webapp-3.4.2.1.war当作是一般的应用就好，我们把它导入eclipse，当作是一个简单的应用！
![](http://dl.iteye.com/upload/attachment/290672/338a2d0a-d233-3a0d-9609-b69e4c7366f0.jpg)
这里众多的xml令人眼花缭乱，我们不必惊慌，除了web.xml外这里能起作用的只有**cas-servlet.xml**和**deployerConfigContext.xml**!
我们需要做点什么？Nothing！即使我们不对这个系统进行任何改动，它仍旧可以运行！
![](http://dl.iteye.com/upload/attachment/290680/956cffe6-e7f3-394d-94f6-ad3ca0584132.jpg)
输入用户名、密码试试登录试试？！不知道用户名？！没关系，就用最常用的admin：admin（用户名：密码）登录试试！
![](http://dl.iteye.com/upload/attachment/290682/d2513110-4b34-3f64-a3ea-6d70522c2345.jpg)
点击登录，看结果！
![](http://dl.iteye.com/upload/attachment/290686/f982e4ec-9ff7-3ce5-82be-880824e5582b.jpg)
成功！！！ :D 难道真的内置了这样的用户，正好被我蒙对了？！我可没有那么神奇！！！
再来个admin:manage试试！！！
![](http://dl.iteye.com/upload/attachment/290688/bed3c0c4-d469-3862-b583-504e9d9f0f4b.jpg)
注意控制台日志！
登录成功：

> 2010-08-10 13:04:45,542 INFO [org.jasig.cas.authentication.authenticationmanagerimpl] - <AuthenticationHandler: org.jasig.cas.authentication.handler.support.[b]SimpleTestUsernamePasswordAuthenticationHandler[/b] successfully authenticated the user which provided the following credentials: [username: admin]>

登录失败：

> 2010-08-10 13:08:38,333 INFO [org.jasig.cas.authentication.authenticationmanagerimpl] - <AuthenticationHandler: org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler failed to authenticate the user which provided the following credentials: [username: admin]>


这个**SimpleTestUsernamePasswordAuthenticationHandler.java**默认实现了用户名与密码一致时，成功登录！
看看**SimpleTestUsernamePasswordAuthenticationHandler.java**的源代码就明白了！

```java

public final class SimpleTestUsernamePasswordAuthenticationHandler extends
    AbstractUsernamePasswordAuthenticationHandler {

    public SimpleTestUsernamePasswordAuthenticationHandler() {
        log
            .warn(this.getClass().getName()
                + " is only to be used in a testing environment.  NEVER enable this in a production environment.");
    }

    public boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials) {
        final String username = credentials.getUsername();
        final String password = credentials.getPassword();

        if (StringUtils.hasText(username) && StringUtils.hasText(password)
            && username.equals(getPasswordEncoder().encode(password))) {
            log.debug("User [" + username + "] was successfully authenticated.");
            return true;
        }

        log.debug("User [" + username + "] failed authentication");

        return false;
    }
}

```

这里
```java
StringUtils.hasText(username) && StringUtils.hasText(password)
            && username.equals(getPasswordEncoder().encode(password))
```

就是关键！ :D 这个类是用于演示的！
打开deployerConfigContext.xml

```xml

				<!--
					| This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS 
					| into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
					| where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your
					| local authentication strategy.  You might accomplish this by coding a new such handler and declaring
					| edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
					+-->
				<bean
					class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />

```

如果我们想让他具有真正意义的作用，就需要使用以下这些包：
![](http://dl.iteye.com/upload/attachment/290949/cc5a270f-4c5a-35aa-a25d-b33006ddfc2b.jpg)

最常用的莫过于使用JDBC的方式，也就是使用cas-server-support-jdbc-3.4.2.1.jar这个包来完成操作！
今天先到这里，时间有限，文档不齐，一时半会吃不透！就不在这里献丑了！ :D 
参考资料
<http://www.ja-sig.org/products/cas/>
<http://www.ja-sig.org/wiki/display/CASUM/Home>
<http://static.springframework.org/spring-security/site/index.html>