---
title: "使用JAAS框架和LDAP做验证(Authentication)、授权(Authorization)"
author: "dongming"
date: "2009-02-10T22:26:47Z"
tags: []
slug: "jaas-ldap-authentication-authorization"
draft: false
lastmod: "2009-02-10T22:26:47Z"
id: 326453
url: "https://dongming.iteye.com/blog/326453"
---

前言：  
网上很多文章都在讲LDAP Server，但是很少有人提及如何使用它来做验证和授权，今天写出此贴，希望能够帮助各位开发人员。

目标：  
使用JAAS框架，使用struts框架，使用LDAP Server，使用jboss，做用户的验证和授权  
（验证的含义是用户有效，即用户名、密码输入正确；授权的含义是用户被授予某种角色）

基础要求：  
精通J2EE框架  
会使用Struts开发  
能够正确安装iPl.NET Directory Server，并有使用经验  
熟悉JBOSS开发  
熟悉JAAS框架

准备：  
windows 2k (要加入域)  
j2sdk1.4.2 installed  
jboss3.2.5 higher installed  
iPlanet Directory Server 5.1 Service Pack 2 installed

首先说一下JAAS和LDAP:  
在实际开发过程中，很多项目都会使用同一个用户信息源，比如客户的Notes系统、ERP系统等等。  
如果我们授命为客户开发一套新的应用，就需要和上述系统一同使用一个用户信息源“LDAP Server”。  
JAAS是一个Java应用的验证、授权框架。很多Application Server都会去实现这个框架（JBOSS就是其中之一），从而使开发人员不需要自己动手就可以在项目中使用高可靠的安全验证体系。

首先假设我们在JBOSS下开发一个应用,叫做“myapp”  
LDAP Server使用Sun iPlanet Directory Server  
LDAP中增加一个组织结构  
Group: admin, Engineer  
Role:  SysUser, admin, Engineer  
User:  JZhao (密码：password，角色：SysUser、admin、Engineer)

如果不愿意手动建立用户，可以copy下面的内容存入temp.ldif文件中，使用iPlanet的导入功能建立用户  
temp.ldif文件的内容：  
\# ================================== OU DEFINITIONS  
\# People OU - for holding records of all individuals  
dn: ou=People,dc=china,dc=xxx,dc=com  
ou: People  
objectClass: top  
objectClass: organizationalUnit

\# Groups OU - for holding records of groupings of individuals  
dn: ou=Groups,dc=china,dc=xxx,dc=com  
ou: Groups  
objectClass: top  
objectClass: organizationalUnit

\# Roles OU - for holding records of roles and the groups to which those roles have been assigned  
dn: ou=Roles,dc=china,dc=xxx,dc=com  
ou: Roles  
objectClass: top  
objectClass: organizationalUnit

\# ================================== PEOPLE ENTRIES  
dn: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: person  
objectClass: organizationalPerson  
objectClass: .netorgperson  
sn: Zhao  
cn: Jun Zhao  
uid: JZhao  
userPassword: {SSHA}UvEbEwmjbUYCjugQ8p53EPj0z22bbAxiWmSCaA==  
Mail: junzhao@vandagroup.com.cn

\# ================================== GROUPS ENTRIES  
dn: cn=SysUser,ou=Groups,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: SysUser  
uniqueMember: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com

dn: cn=admin,ou=Groups,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: admin  
uniqueMember: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com

dn: cn=Engineer,ou=Groups,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: Engineer

\# ================================== ROLES ENTRIES  
dn: cn=SysUser,ou=Roles,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: SysUser  
uniqueMember: cn=Everyone,ou=Groups,dc=china,dc=xxx,dc=com  
uniqueMember: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com

\# =======================  
dn: cn=admin,ou=Roles,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: admin  
uniqueMember: cn=admin,ou=Groups,dc=china,dc=xxx,dc=com  
uniqueMember: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com

\# =======================  
dn: cn=Engineer,ou=Roles,dc=china,dc=xxx,dc=com  
objectClass: top  
objectClass: groupOfUniqueNames  
cn: Engineer  
uniqueMember: cn=Engineer,ou=Groups,dc=china,dc=xxx,dc=com  
uniqueMember: uid=JZhao,ou=People,dc=china,dc=xxx,dc=com

web-INF/web.XML中增加下面内容：

  &lt;welcome-file-list&gt;  
  &lt;welcome-file&gt;index.JSP&lt;/welcome-file&gt;  
  &lt;/welcome-file-list&gt;  
   
  &lt;!-- 添加入口角色检查，本段内容规定SysUser可以进入系统 --&gt;  
  &lt;security-constraint&gt;  
  &lt;web-resource-collection&gt;  
  &lt;web-resource-name&gt;Restricted&lt;/web-resource-name&gt;  
  &lt;description&gt;Declarative security tests&lt;/description&gt;  
  &lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;  
  &lt;url-pattern&gt;/servlet/ComandLineServlet&lt;/url-pattern&gt;  
  &lt;http-method&gt;HEAD&lt;/http-method&gt;  
  &lt;http-method&gt;GET&lt;/http-method&gt;  
  &lt;http-method&gt;POST&lt;/http-method&gt;  
  &lt;http-method&gt;PUT&lt;/http-method&gt;  
  &lt;http-method&gt;DELETE&lt;/http-method&gt;  
  &lt;/web-resource-collection&gt;  
  &lt;auth-constraint&gt;  
  &lt;role-name&gt;SysUser&lt;/role-name&gt;  
  &lt;/auth-constraint&gt;  
  &lt;user-data-constraint&gt;  
  &lt;description&gt;no description&lt;/description&gt;  
  &lt;transport-guarantee&gt;NONE&lt;/transport-guarantee&gt;  
  &lt;/user-data-constraint&gt;  
  &lt;/security-constraint&gt;

  &lt;!-- 定义登录的页面和登录出错的页面 --&gt;  
  &lt;login-config&gt;  
  &lt;auth-method&gt;FORM&lt;/auth-method&gt;  
  &lt;form-login-config&gt;  
  &lt;form-login-page&gt;/login.jsp&lt;/form-login-page&gt;  
  &lt;form-error-page&gt;/login.jsp&lt;/form-error-page&gt;  
  &lt;/form-login-config&gt;  
  &lt;/login-config&gt;

  &lt;!-- 在此定义安全角色 --&gt;  
  &lt;security-role&gt;  
  &lt;description&gt;A user allowed to login&lt;/description&gt;  
  &lt;role-name&gt;SysUser&lt;/role-name&gt;  
  &lt;/security-role&gt;

 

WEB-INF/jboss-web.xml  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3//EN" "http://www.jboss.org/j2ee/dtd/jboss-web\_3\_2.dtd"&gt;

&lt;jboss-web&gt;  
 &lt;security-domain&gt;java:/jaas/myapp-security&lt;/security-domain&gt;  
   &lt;context-root&gt;myapp&lt;/context-root&gt;  
&lt;/jboss-web&gt;

JBOSS\_HOME/server/default/conf/login-config.xml中增加

  &lt;application-policy name = "myapp-security"&gt;  
  &lt;authentication&gt;  
  &lt;login-module code = "org.jboss.security.auth.spi.LdapLoginModule" flag = "required"&gt;  
 &lt;module-option name="java.naming.factory.initial"&gt;com.sun.jndi.ldap.LdapCtxFactory&lt;/module-option&gt;  
 &lt;module-option name="java.naming.provider.url"&gt;ldap://home.china.xxx.com:389/&lt;/module-option&gt;  
 &lt;module-option  name="java.naming.security.authentication"&gt;simple&lt;/module-option&gt;  
 &lt;module-option  name="principalDNPrefix"&gt;uid=&lt;/module-option&gt;  
 &lt;module-option  name="principaldnsuffix"&gt;,ou=People,dc=china,dc=xxx,dc=com&lt;/module-option&gt;  
 &lt;module-option  name="rolesCtxDN"&gt;ou=Roles,dc=china,dc=xxx,dc=com&lt;/module-option&gt;  
 &lt;module-option  name="uidAttributeID"&gt;uniqueMember&lt;/module-option&gt;  
 &lt;module-option  name="roleAttributeID"&gt;cn&lt;/module-option&gt;  
 &lt;module-option  name="matchOnUserDN"&gt;true&lt;/module-option&gt;  
  &lt;/login-module&gt;  
  &lt;/authentication&gt;  
  &lt;/application-policy&gt;

index.jsp内容：  
&lt;%  
out.write("Login Successful!  "+new java.util.Date());  
out.write("is SysUser="+httpServletRequest.isUserInRole("SysUser"));  
out.write("is admin="+httpServletRequest.isUserInRole("admin"));  
out.write("is Engineer="+httpServletRequest.isUserInRole("Engineer"));  
%&gt;

login.jsp中的FORM内容：  
   
&lt;FORM name="logonForm"  method="post" action="j\_security\_check"&gt;  
 &lt;input name="j\_username" type="text" /&gt;  
 &lt;input name="j\_password" type="password" /&gt;  
&lt;/FORM&gt;

*  帐号 j\_username  
*  密码 j\_password  
*  action j\_security\_check  
“\*”内容为固定写法，不能改变

所有准备完成之后，启动JBOSS，在浏览器中输入http://ip:8080/myapp 进入index.jsp，但权限不允许匿名进入，所以会被Server将请求转入login.jsp，输入JZhao:password，判断成功之后，将会自动转入index.jsp，在页面中你会看到下面的输入

Login Successfull!  Sat Nov 27 11:52:36 CST 2004  
is SysUser=true  
is admin=true  
is Engineer=true