EJB3.0 Development Guide: Transaction and Security

xiaoxiao2021-03-06  49

Not like in EJB2.x, you need to deploy transactions in the deployment file. EJB3.0 provides transaction support for the specified class or method by comment. In the preview specification of EJB3.0, specifying the use of TransactionAttribute as a comment, the statement of TransactionAttribute is as follows

@Target ({Method, Type}) @Retime (runtime) public @interface TransactionAttribute {TransactionAttributeType value () Default Required;

TransactionAttributeType is an enumeration type:

Public Enum TransactionAtTributeType {Mandatory, Required, Requiresnew, Supports, NotSupported, Never}

It can be seen that TransactionAttribute can be annotated for classes and can be noted for methods.

Several enumeration values ​​of TransactionttributeType representative:

Mandatory This property requires your transaction to be running, otherwise it will return an exception. Required If you want your EJB to always run in the transaction, you should use this mode. If the transaction is running, you will add your EJB components to your transaction. If there is no transaction, you will add a transaction. Requiresnew always gives your EJB component a new transaction. Supports If the transaction exists, add your EJB to your transaction, if there is no transaction, you will not run in the transaction. NotSupported Your EJB component will not use transactions. Suppose your component B sets this property, component A uses a transaction, when A calls B, a transaction will be suspended, and the transaction operation will not perform transaction operation. When B is completed, a transaction is activated. NEVER Your EJB component will not use transactions. Suppose your component B sets this property, when the client calls B in the transaction, the container will throw an exception.

In JBOSS, this note is simplified, and TransactionAttribute is simplified to TX, TransactionAttributeType is simplified to TXTYPE.

MethodPermissions and Unchecked are secure comments. JSR-181 is simplified. These two comments can be used in classes and methods.

MethodPermissions is used to specify a list of roles that can be allowed to use such or this method:

@MethodPermissions

(

{"Role1", "role2", "role3"}

)

Unchecked specified method and class will not do permission check.

In Eclipse, the example provided this article is imported.

This example is similar to the example of the stateless session bean, but transaction or permissions are added to the business method.

This example mainly has 7 files:

Counter.java: Business Interface.

CounterBean.java: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Roles.properties: Role settings

Users.properties: User Settings

Here is a description of the content of each file.

Counter.java

Package com.kuaff.ejb3.security; import javax.ejb.remote; @Remote Public interface counter {public int Add (INT i); public int getNumber (); public int getValue ();} This interface is simple, and no The status session bean is the same, and JBoss defaults to use the full name of the interface as its JNDI name. In the above example, its full name can be obtained by counter.class.getname ().

Counterbean.java

package com.kuaff.ejb3.security; import org.jboss.ejb3.security.SecurityDomain; import javax.ejb.MethodPermissions; import javax.ejb.Stateless; import javax.ejb.Tx; import javax.ejb.TxType; import javax .ejb.Unchecked; @Stateless @SecurityDomain ( "kuaff") public class CounterBean implements Counter {private int number = 0; @Unchecked @Tx (TxType.REQUIRESNEW) public int add (int i) {number = i; return number @MethodperMissions ({"Manager"}) public int getNumber () {return number;} @MethodPermissions ({"User"}) public int getValue () {return number;}}

This is the implementation class of the counter. In this class, SecurityDomain is a comment added by JBoss to specify a JaAs's warehouse. Set to Other, verify the properties file. In our example, you can see two new files.prperties and roles.properties, store users and role information separately. Add methods support transactions and do not do permissions. GetNumber needs to have a Manager role to access, GetValue needs to have users who have user roles to access.

Client.java

package com.kuaff.ejb3.security; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.security.SecurityAssociation; import org.jboss.security.SimplePrincipal; public class Client {public static void main ( String [] args) {InitialContext ctx; try {ctx = new InitialContext (); counter counter = (counter) ctx.lookup (Counter.class.getName ()); SecurityAssociation.setPrincipal (new SimplePrincipal ( "smallnest")); System.out.println ("Use Manager Role Access"); Securityassociation.Setcredential ("ASD # $ 45GAS5" .tochararray ()); try {system.out.println ("Current Number:" counter.getNumber () );} Catch (securityException ex) {system.out.println ("Security Abnormal 001");} System.out.Println ("Accessing other roles to access method"); try { System.out.println ("Current Number:" Counter.getValue ());} catch (securityException ex) {system.out.println ("security exception 002");}} catch (namingexception e) {E This class is used to test the counter EJB we publish. First pass

CTX = new initialContext (); get the context, then use the lookup counter, then set the user and password, the counter method is executed, and the exception will be thrown once it is not accessed.

Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.

转载请注明原文地址:https://www.9cbs.com/read-81473.html

New Post(0)