zoukankan      html  css  js  c++  java
  • 需要安全认证的远程EJB调用示例(Jboss EAP 6.2环境)

    一,Remote EJB

    服务接口定义:

    1 package yjmyzz.ejb.server.helloworld;
    2 
    3 public interface HelloWorldService {
    4     
    5     public String sayHello(String name);
    6 
    7 }

    实现:

     1 package yjmyzz.ejb.server.helloworld;
     2 
     3 import javax.annotation.security.RolesAllowed;
     4 import javax.ejb.Remote;
     5 import javax.ejb.Stateless;
     6 
     7 @Stateless
     8 @Remote(HelloWorldService.class)
     9 @RolesAllowed({ "guest" })
    10 public class HelloWorldBean implements HelloWorldService {
    11 
    12     public String sayHello(String name) {
    13         return "hello , " + name + " , welcome to EJB's world!";
    14     }
    15 
    16 }

    注意: @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

     1 package yjmyzz.ejb.client.helloworld;
     2 
     3 import java.util.Hashtable;
     4 
     5 import javax.naming.Context;
     6 import javax.naming.InitialContext;
     7 import javax.naming.NamingException;
     8 
     9 import yjmyzz.ejb.server.helloworld.HelloWorldService;
    10 
    11 
    12 public class EjbClientApp {
    13     public static void main(String[] args) throws NamingException {
    14         System.out.println(lookupRemoteBean().sayHello("jimmy"));
    15     }
    16 
    17     @SuppressWarnings("unchecked")
    18     private static HelloWorldService lookupRemoteBean() throws NamingException {
    19         @SuppressWarnings("rawtypes")
    20         final Hashtable jndiProperties = new Hashtable();
    21         jndiProperties.put(Context.URL_PKG_PREFIXES,
    22                 "org.jboss.ejb.client.naming");
    23         final Context context = new InitialContext(jndiProperties);
    24         return (HelloWorldService) context
    25                 .lookup("ejb:/ejb-server-helloworld/HelloWorldBean!"
    26                         + HelloWorldService.class.getName());
    27     }
    28 }

     注: lookupRemoteBean用于查找远程EJB.

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

    1 remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
    2 remote.connections=default
    3 remote.connection.default.host=localhost
    4 remote.connection.default.port = 4447
    5 remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
    6 remote.connection.default.username=msgUser
    7 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

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    【手绘漫画】图解LeetCode之最长上升子序列(LeetCode300题),贪心算法 + 二分查找
    C 语言编程 — GDB 调试工具
    【debug】Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
    tf.expand_dims()函数解析(最清晰的解释)
    C 语言编程 — 堆栈与内存管理
    C 语言编程 — 输入/输出与文件操作
    C 语言编程 — 头文件
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/remote-ejb3-with-security-sample-on-jboss-eap.html
Copyright © 2011-2022 走看看