EJB 3 Security Domain with Open LDAP

阅读更多

项目中用到了EJB 3 web service, 生产环境上都是有安全认证的,常见的认证有2中,一种是数据库认证,另外一种是LDAP认证。这篇文章主要讲述的是LDAP认证,使用的LDAP是openLDAP windows版本。

 

准备条件:

1. 服务器: JBoss 6 (自行下载)

2. LDAP:  Open LDAP for windows (参考网络,很多文档)

3. Maven 依赖:jboss-ejb3-ext-api, jboss-ejb-api, jboss-as-system-jmx

 

LDAP 配置

1. 依照网络上的通用例子新建一个xx.ldif文件,密码就设置成admin或者其他的,

dn: dc=example,dc=com
objectclass: dcObject
objectclass: organization
o: Example Company
dc: example

dn: cn=Manager,dc=example,dc=com
objectclass: organizationalRole
cn: Manager

 

2. 添加2个ou(Users, Roles):

dn: ou=Users,dc=example,dc=com
objectclass: organizationalRole
objectclass: top

dn: ou=Roles,dc=example,dc=com
objectclass: organizationalRole
objectclass: top

 

3. 添加1个测试用户和1个测试角色

dn: uid=tester,ou=Users,dc=example,dc=com
objectclass: top
objectclass: inetOrgPerson
objectclass: person
uid: tester
cn: tester
sn: tester
userPassword:1

dn: cn=test,ou=Users,dc=example,dc=com
objectClass: top
objectClass: groupOfNames
cn: test
description: testgroup
member: uid=tester,ou=Users,dc=example,dc=com

 注意:objectClass一定要匹配,不要弄错了。

 

再添加一个普通用户,不要加入到test组中,

 

dn: uid=tester2,ou=Users,dc=example,dc=com
objectclass: top
objectclass: inetOrgPerson
objectclass: person
uid: tester
cn: tester
sn: tester
userPassword:1

 

配置JBoss login config xml

1. 打开jboss-6\server\default\conf\login-config.xml, 在 之前添加一段:


		
			
				com.sun.jndi.ldap.LdapCtxFactory
				simple
				ldap://example.com:389
				cn=Manager,dc=example,dc=com
				admin
				ou=Users,dc=example,dc=com
				(uid={0})
				ou=roles,dc=example,dc=com
				(member={1})
				cn
			

		
	

 

新建MAVEN项目

项目名:securityTest

 

pom.xml:

 


  4.0.0
  com.javaeye.ejb
  securityTest
  0.0.1-SNAPSHOT
  ejb
  
		UTF-8
	
		
		
			org.jboss.ejb3
			jboss-ejb3-ext-api
			1.1.1
		
		
			org.jboss.javaee
			jboss-ejb-api
			3.0.0.CR1
		
		
			org.jboss.jbossas
			jboss-as-system-jmx
			6.0.0.M1
		
	

	
		securityTest
		
			
				src/main/resources
				true
			
		
		
		
				maven-compiler-plugin
				2.3.2
				
					1.6
					1.6
					utf8
				
			
			
				org.apache.maven.plugins
				maven-ejb-plugin
				2.3
				
					3.0
				
						
		
	
 

 

Service Interface:

 

package com.javaeye.test;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(targetNamespace = "http://www.javaeye.com/test/", name = "test")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface Test {

	public String process(String parameters);
}
 

 

Service Impl

 

package com.javaeye.ejb;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.jws.WebService;

import org.jboss.ejb3.annotation.SecurityDomain;
import org.jboss.wsf.spi.annotation.WebContext;

import com.javaeye.test.Test;

@Stateless
@WebService(serviceName = "Test", endpointInterface = "com.javaeye.test.Test", targetNamespace = "http://www.javaeye.com/test/")
@WebContext(contextRoot = "securityTest", urlPattern = "/test", authMethod = "BASIC", secureWSDLAccess = true)
@SecurityDomain("sd")
public class SecurityTestService implements Test{

	@RolesAllowed({ "test" })
	public String process(String parameters) {
		return "Hello, " + parameters;
	}

}
 

 

Service 很简单,就是一个hello + input。酷

执行 mvn clean install, 在target会生成一个jar包,把架包丢到jboss deploy目录下,启动jboss, 可以看到service注册成功:

 

15:31:41,069 INFO  [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] Add Service
 id=SecurityTestService
 address=http://localhost:9000/securityTest/test
 implementor=com.javaeye.ejb.SecurityTestService
 invoker=org.jboss.wsf.stack.cxf.InvokerEJB3
 serviceName={http://www.javaeye.com/test/}Test
 portName={http://www.javaeye.com/test/}SecurityTestServicePort
 wsdlLocation=null
 mtomEnabled=false
 

 

通过soup ui新建一个project, wsdl:http://localhost:9000/securityTest/test?wsdl

回车后要求输入用户和密码,则填入:tester/1,回车后打开项目,双击process方法下面的Request 1

 

Input:

 


   
   
      aaa
   

 

 

 case 1: 在SOAP UI Reuest Property里面设置Username=tester, Password=1, (正确的用户,有正确的权限)然后执行方法,将返回

 


   
      Hello, aaa
   
 

 

case 2: 在SOAP UI Reuest Property里面设置Username=tester, Password=12, (错误的用户密码)然后执行方法,将返回

 

JBoss Web/3.0.0-CR1 - Error report 

HTTP Status 401 -


type Status report

message

description This request requires HTTP authentication ().


JBoss Web/3.0.0-CR1

 

 

case 3: 在SOAP UI Reuest Property里面设置Username=tester2, Password=1, (正确的用户,没有权限)然后执行方法,将返回

 


   
      
         soap:Server
         Caller unauthorized
      
   

 

至此,测试完毕。谢谢阅读!

你可能感兴趣的:(JBoss,Open,Ldap,EJB3,Security,Domain)