JBoss EJB3.0 Security

xiaoxiao2021-03-05  47

Calculator.java

Package org.jboss.tutorial.security.bean;

Import javax.ejb.remote;

@Remote

Public Interface Calculator

{

INT Add (int X, int y);

Int subtract (int X, int y);

Int Divide (int X, int y);

}

CalculatorBean.java

Package org.jboss.tutorial.security.bean;

Import org.jboss.ejb3.security.securitydomain

Import javax.ejb.methodpermissions;

Import javax.ejb.stateless;

Import javax.ejb.transactionattribute;

Import javax.ejb.transactionattributetype;

Import javax.ejb.unchecked;

@StateLess

@Securitydomain ("other")

Public Class CalculatorBean Implements Calculator

{

@Unchecked // it's ok to delete this line, it means to use this method without checked

@TransactionAttribute (TransactionAttributeType.Requires_New)

Public Int Add (int X, int y)

{

Return X Y;

}

@MethodPermissions ({"Student", "Teacher"}) // Note That We can more role here

Public int subtract (int X, int y)

{

Return X - Y;

}

@MethodperMissions ({"Teacher"})

Public int Divide (int X, int y)

{

Return X / Y;

}

}

Here Substract methods define access objects: Student and Teacher; Divide methods define that the access object is: Teacher. You can see role.properties, of course, you can also add some custom objects.

Client.java

Package org.jboss.tutorial.security.client;

Import org.jboss.security.securityassociation;

Import org.jboss.security.simpleprincipal;

Import org.jboss.tutorial.security.bean.calculator;

Import javax.naming.initialcontext;

Public Class Client

{

Public static void main (string [] args) Throws Exception

{

InitialContext CTX = New InitialContext ();

Calculator Calculator = (Calculator) ctx.lookup (Calculator.class.getName ());

System.out.Println ("Everybody CAN Add");

System.out.Println ("1 1 =" Calculator.Add (1, 1)); System.out.Println ("Change Role: Kabir Is A Student);

Securityassociation.SetPrincipal (New SimplePrincipal ("kabir"));

Securityassociation.Setcredential ("ValidPassword" .tochararray ());

System.out.println ("Students Are Allowed to Do Subtraction But Division");

System.out.println ("1 - 1 =" Calculator.Subtract (1, 1));

Try

{

System.out.println ("16/4 =" Calculator.divide (16, 4));

}

Catch (SecurityException EX)

{

System.out.println ("Kabir try to do division:" ex.getMessage ());

}

System.out.println ("Change Role: Roson IS A Teacher);

Securityassociation.SetPrincipal (New SimplePrincipal ("Roson");

Securityassociation.Setcredential ("Sandy" .tochararray ());

System.out.Println ("Teacher Are Allowed Do Substraction and Division);

System.out.Println ("2 - 1 =" Calculator.Subtract (2, 1));

System.out.println ("16/4 =" Calculator.divide (16, 4));

}

}

There are two characters here: kabir is the Student password for validpassword; roson is a teacher password for Sandy.

These two people calls minus, except that both method programs will be processed according to access.

Users.properties

Kabir = validpassword

Roson = Sandy

Inside is a format such as UserName = Password, one line of one user.

ROLES.PROPERTIES

Kabir = student

Roson = teacher

Inside is username = role1, role2, role3, all roles to the user and the user belong.

There is no log4j.properties in jboss-ejb-3.0_preview_5.zip, there is no such thing as the lack of appender. With this will generate a replard.log log file in this directory

Log4j.properties

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = record.log

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.rapnder.r.Layout.conversionPattern =% p% d {hh: mm: ss}% T% c {1} -% m% nlog4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.maxfilesize = 100kb

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.rootlogger = stdout, r

Run: Refer to Installing.html

Under Windows

Open the command prompt cmd to jboss_home / bin

Run.bat -c all

Use ANT

After Build, run.

discuss:

Because there are not many contacts in JaaS, I can only try my own ideas and changed places.

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

New Post(0)