Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login a user programmatically via JAAS

I'm trying to run code within JBoss Container under a different authentication by programatically logging in a user like that (stripped exception handling):

LoginContext ctx = ctx = 
    new LoginContext("MyLoginSchema", 
        new UsernamePasswordCallbackHandler("newuser", "")
    );
ctx.login();

Subject.doAs(ctx.getSubject(), new PrivilegedAction<T>() {
    @Override
    public T run() {
        Subject.getSubject(AccessController.getContext());
        InitialContext ic = new InitialContext();
        EJBContext sctxLookup = (EJBContext) ic.lookup("java:comp/EJBContext");
        Principal principal = sctxLookup.getCallerPrincipal();
    }           
}); 

Login of newuser works (Call of LoginModule was successful) but Subject.doAs() doesn't associate the new Subject with the EJBContext. The code in the run()-Method still fetches the old user's principal from EJBContext.

I tested another method of retrieving the logged in user but same behavior here:

Subject caller = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");

Any ideas?

like image 866
roehrijn Avatar asked Oct 08 '12 10:10

roehrijn


People also ask

What is required to authenticate a user with JAAS?

All you have to know is how to write your application and specify configuration information (such as in a login configuration file) such that the application will be able to utilize the LoginModule specified by the configuration to authenticate the user.

What is the full form of JAAS in Java?

The Java Authentication and Authorization Service (JAAS) was introduced as an optional package (extension) to the Java 2 SDK, Standard Edition (J2SDK), v 1.3.

What is JAAS server?

The Java Authentication and Authorization Service (JAAS) is a set of application program interfaces (APIs) that can determine the identity of a user or computer attempting to run Java code and ensure that the entity has the right to execute the functions requested.

What is JAAS module?

JAAS provides subject-based authorization on authenticated identities. This document focuses on the authentication aspect of JAAS, specifically the LoginModule interface.


1 Answers

Which LoginModule do you use now? In JBoss 6.1 you had to use ClientLoginModule to authenticate in container.

like image 129
Roman Besolov Avatar answered Oct 25 '22 21:10

Roman Besolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!