需要安全认证的远程EJB调用示例(Jboss EAP 6.2环境)

时间:2022-05-04 11:12:59

一,Remote EJB

服务接口定义:

 package yjmyzz.ejb.server.helloworld;

 public interface HelloWorldService {

     public String sayHello(String name);

 }

实现:

 package yjmyzz.ejb.server.helloworld;

 import javax.annotation.security.RolesAllowed;
import javax.ejb.Remote;
import javax.ejb.Stateless; @Stateless
@Remote(HelloWorldService.class)
@RolesAllowed({ "guest" })
public class HelloWorldBean implements HelloWorldService { public String sayHello(String name) {
return "hello , " + name + " , welcome to EJB's world!";
} }

注意: @RoleAllowed({"guest"}) 该注解表示只有guest这个角色的用户才能调用HelloWorldBean

对应maven的pom.xml内容如下:

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>yjmyzz</groupId>
<artifactId>ejb-server-helloworld</artifactId>
<version>1.0</version>
<packaging>ejb</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>
<version.jboss.spec.javaee.6.0>3.0.2.Final</version.jboss.spec.javaee.6.0>
<version.ejb.plugin>2.3</version.ejb.plugin>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>${version.jboss.spec.javaee.6.0}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies> <dependency>
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName>
<plugins> <plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.maven.plugin}</version>
<configuration>
<filename>${project.build.finalName}.jar</filename>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${version.ejb.plugin}</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
</configuration>
</plugin> </plugins>
</build> </project>

二,EJB Client

 package yjmyzz.ejb.client.helloworld;

 import java.util.Hashtable;

 import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; import yjmyzz.ejb.server.helloworld.HelloWorldService; public class EjbClientApp {
public static void main(String[] args) throws NamingException {
System.out.println(lookupRemoteBean().sayHello("jimmy"));
} @SuppressWarnings("unchecked")
private static HelloWorldService lookupRemoteBean() throws NamingException {
@SuppressWarnings("rawtypes")
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,
"org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
return (HelloWorldService) context
.lookup("ejb:/ejb-server-helloworld/HelloWorldBean!"
+ HelloWorldService.class.getName());
}
}

注: lookupRemoteBean用于查找远程EJB.

jboss环境中,需要在classpath路径下放置jboss-ejb-client.properties文件,内容参考下面:

 remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=msgUser
remote.connection.default.password=Password1!

注: msgUser为jboss中创建的一个application user,而且属于guest组(不熟悉jboss下创建用户的朋友,可参考JMS + jboss EAP 6.2 示例 中的相关内容)

对应maven项目的pom.xml为:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>yjmyzz</groupId>
<artifactId>ejb-client-helloworld</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>ejb-client-helloworld</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>yjmyzz</groupId>
<artifactId>ejb-server-helloworld</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
<version>1.0.1.Final-redhat-2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<version>1.0.2.Final-redhat-2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-ejb-client</artifactId>
<version>1.0.21.Final-redhat-1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jboss.xnio</groupId>
<artifactId>xnio-nio</artifactId>
<version>3.0.7.GA-redhat-1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jboss.sasl</groupId>
<artifactId>jboss-sasl</artifactId>
<version>1.0.3.Final-redhat-1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-river</artifactId>
<version>1.3.16.GA-redhat-1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

示例源码: remote-ejb-with-security-sample.zip