前言:
网上很多文章都在讲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中增加下面内容:
<welcome-file-list>
<welcome-file>index.JSP</welcome-file>
</welcome-file-list>
<!-- 添加入口角色检查,本段内容规定SysUser可以进入系统 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted</web-resource-name>
<description>Declarative security tests</description>
<url-pattern>*.do</url-pattern>
<url-pattern>/servlet/ComandLineServlet</url-pattern>
<http-method>HEAD</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>SysUser</role-name>
</auth-constraint>
<user-data-constraint>
<description>no description</description>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- 定义登录的页面和登录出错的页面 -->
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- 在此定义安全角色 -->
<security-role>
<description>A user allowed to login</description>
<role-name>SysUser</role-name>
</security-role>
WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>
<security-domain>java:/jaas/myapp-security</security-domain>
<context-root>myapp</context-root>
</jboss-web>
JBOSS_HOME/server/default/conf/login-config.xml中增加
<application-policy name = "myapp-security">
<authentication>
<login-module code = "org.jboss.security.auth.spi.LdapLoginModule" flag = "required">
<module-option name="java.naming.factory.initial">com.sun.jndi.ldap.LdapCtxFactory</module-option>
<module-option name="java.naming.provider.url">ldap://home.china.xxx.com:389/</module-option>
<module-option name="java.naming.security.authentication">simple</module-option>
<module-option name="principalDNPrefix">uid=</module-option>
<module-option name="principaldnsuffix">,ou=People,dc=china,dc=xxx,dc=com</module-option>
<module-option name="rolesCtxDN">ou=Roles,dc=china,dc=xxx,dc=com</module-option>
<module-option name="uidAttributeID">uniqueMember</module-option>
<module-option name="roleAttributeID">cn</module-option>
<module-option name="matchOnUserDN">true</module-option>
</login-module>
</authentication>
</application-policy>
index.jsp内容:
<%
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"));
%>
login.jsp中的FORM内容:
<FORM name="logonForm" method="post" action="j_security_check">
<input name="j_username" type="text" />
<input name="j_password" type="password" />
</FORM>
* 帐号 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
分享到:
相关推荐
Java 2 SDK 1.3引入了JAAS( Java Authentication and Authorization Service),增加了基于用户的访问控制能力,即根据谁在运行代码来进行授权。JAAS已经整合进了Java 2 SDK 1.4,作为标准的用户认证与授权模型。
通过JAAS框架针对LDAP数据存储库对用户进行授权和认证的模块。 已经创建了两个测试类,可以用作创建新连接器的模板 安装 编译项目: mvn clean package 将生成的jar文件复制到项目类路径 在配置目录(CONF_DIR)中...
Java Authentication Authorization Service(JAAS,Java验证和授权API)提供了灵活和可伸缩的机制来保证客户端或服务器端的Java程序。Java早期的安全框架强调的是通过验证代码的来源和作者,保护用户避免受到下载...
以完整的代码讲解JAAS( JAVA验证与授权服务)
jaas的详细资料 基于JAAS和J2EE Web容器的验证与授权
Java Authentication Authorization Service(JAAS,Java 验证和授权API)提供了灵活和可伸缩的机制来保证客户端或服务器端的Java 程序。Java 早期的安全框架强调的是通过验证代码的来源和作者,保护用户避免受到...
Java Authentication Authorization Service(JAAS,Java验证和授权API)提供了灵活和可伸缩的机制来保证客户端或服务器端的Java程序
java JAAS登陆验证
JAAS 身份验证技术入门 作者 马林
Java认证和授权服务(JAAS)供应器JAAs验证机制
JAAS, the Java Authentication and Authorization Service, has been a standard part of the Java security framework since version 1.4 version and was available as an optional package in J2SE 1.3. Before ...
JAAS认证与授权教程,包含本教程的源代码
JAAS(Java Authentication and Authentication Service Jay,认证和授权服务)是Java安全编程的一个重要补 充。它提供了根据验证主题身份来验证主题和授权资源访问的标准方法。本文较全面地介绍了JAAS的特点、组成 ...
#在JAAS上运行VAAS与JAAS(JAVAEE身份验证和授权规范)具有良好的集成。 让我们来看看。 首先,您必须在Web应用程序的META-INF文件夹中创建一个Context.xml文件。 在这里,我们使用的是来自Tomcat的MemoryRealm
JAAS登录验证文档,里面有详细步骤及例子;亲测使用中,希望用jaas的朋友能帮助到大家;
此示例演示了使用JAAS身份验证然后获得对基础安全上下文的访问权限的问题。 只需将其安装到已安装CXF 3.0.2的Karaf容器中即可。 # install cxf feature:install -v cxf-jaxrs # install the bundle install -s mvn:...
jaas.jar jaas.jar jaas.jar
基于JAAS的用户验证和控制模型的一般设计.doc