使用glassfish 5,JEE 8和独立应用程序进行JAAS登录

时间:2018-07-27 07:58:49

标签: ejb jaas jdbcrealm glassfish-5 java-ee-8

我正在使用glassfish 5,将JEE 8与EJB 3一起使用

我正在使用posgres使用jdbc领域。我已经在glassfish管理界面中设置了我的领域。

我有一个耳朵,里面装有一个ejb罐子和一个战争。该领域与网络应用程序配合良好

但是现在我必须设置一个独立的应用程序才能与服务器连接。我创建了一个简单的测试独立客户端,以尝试使用LoginContext进行连接,但无法使其与我的独立应用程序一起使用。我不断收到此错误。

Jul 27, 2018 11:50:54 AM com.sun.enterprise.security.BasePasswordLoginModule extractCredentials
    SEVERE: A PasswordCredential was required but not provided.
    javax.security.auth.login.LoginException: No credentials.
        at com.sun.enterprise.security.BasePasswordLoginModule.extractCredentials(BasePasswordLoginModule.java:342)
        at com.sun.enterprise.security.BasePasswordLoginModule.login(BasePasswordLoginModule.java:142)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at javax.security.auth.login.LoginContext.invoke(LoginContext.java:755)
        at javax.security.auth.login.LoginContext.access$000(LoginContext.java:195)
        at javax.security.auth.login.LoginContext$4.run(LoginContext.java:682)
        at javax.security.auth.login.LoginContext$4.run(LoginContext.java:680)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680)
        at javax.security.auth.login.LoginContext.login(LoginContext.java:587)
        at com.test.test.TestLogin.jaasLogin(TestLogin.java:33)
        at com.test.test.TestLogin.main(TestLogin.java:22

我缺少什么?

这是源代码

jaas.config

my_realm {
com.sun.enterprise.security.ee.auth.login.JDBCLoginModule 
        required
        debug="true"
        ;
};

独立客户端代码

package com.test.test;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;

public class TestLogin
{
    static LoginContext loginContext;

    public static void main(String[] args)
    {
        System.setProperty("java.security.auth.login.config", "/tmp/jaas.config");
        System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        System.out.println(jaasLogin("Test", "Test", "localhost"));

    }

    static public boolean jaasLogin(String login, String password, String server)
    {
        System.out.println("Server = " + server);
        CallbackHandler handler = new MyCallbackHandler(login, password);
        try
        {
            loginContext = new LoginContext("my_realm", handler);
            loginContext.login();
            System.out.println("JAAS : login succeeded");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    static class MyCallbackHandler implements CallbackHandler
    {

        /**
         * Username which will be set in the NameCallback, when NameCallback is
         * handled
         */
        private String username;

        /**
         * Password which will be set in the PasswordCallback, when PasswordCallback
         * is handled
         */
        private String password;

        /**
         * Constructor
         * 
         * @param username
         *          The username
         * @param password
         *          The password
         */
        public MyCallbackHandler(String username, String password)
        {
            this.username = username;
            this.password = password;
        }

        /**
         * @param callbacks
         *          Instances of Callbacks
         * @throws IOException
         *           IOException
         * @throws UnsupportedCallbackException
         *           If Callback is other than NameCallback or PasswordCallback
         */
        public void handle(Callback callbacks[])
                throws IOException, UnsupportedCallbackException
        {

            for (int i = 0; i < callbacks.length; i++)
            {
                if (callbacks[i] instanceof NameCallback)
                {
                    NameCallback nc = (NameCallback) callbacks[i];
                    nc.setName(username);
                }
                else if (callbacks[i] instanceof PasswordCallback)
                {
                    PasswordCallback pc = (PasswordCallback) callbacks[i];
                    pc.setPassword(password.toCharArray());
                }
                else
                {
                    throw new UnsupportedCallbackException(callbacks[i],
                            "Unrecognized Callback");
                }

            }
        }
    }
}

0 个答案:

没有答案