0

I'm trying to run the JAAS code sample.

lc = new LoginContext("JaasSample", new TextCallbackHandler());

To register the configuration file I try the command:

java -Djava.security.auth.login.config=jaas.config JaasAcn

And gets the error:

Error: Could not find or load main class .security.auth.login.config=jaas.config

Caused by: java.lang.ClassNotFoundException: /security/auth/login/config=jaas/config

Community
  • 1
  • 1
baruchiro
  • 5,088
  • 5
  • 44
  • 66
  • Please let me to write answer. There are many mistakes in [doc](https://docs.oracle.com/javase/7/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html) and I have two points: Today java use "javax" library. And prefer to use the programmicly way to set Configuration. – baruchiro Jan 08 '18 at 18:38
  • `java.security.auth.login.config` is not a package name. It is the name of a system property, and it is documented correctly. Your problem is due to a trivial typo: nothing else. – user207421 Jan 15 '18 at 00:47
  • @EJP so which typo? And why the error is *class* not found? – baruchiro Jan 15 '18 at 04:42
  • This is already covered completely in my answer. You can't seriously expect me to state it all twice. – user207421 Jan 15 '18 at 09:53

2 Answers2

2

Firstly write javax instead of java, so:

java -Djavax.security.auth.login.config=jaas.config JaasAcn

And second, consider to config it programmicly:

  1. Inherit the javax.security.auth.login.Configuration class.
  2. Override the function AppConfigurationEntry[] getAppConfigurationEntry(String name). In this func you can return an AppConfigurationEntry object that represent a row in config file.

    new AppConfigurationEntry(NTLoginModule.class.getName(),
    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, new HashMap<>())
    

    With name parameter you can responde for the name parameter in LoginContext constructor.

  3. Create object from your Configuration class and put him in Configuration:

    MyConfiguration config = new MyConfiguration();
    Configuration.setConfiguration(config);
    

The shortened code can look like this:

Configuration config = new Configuration() {
    @Override
    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
        return new AppConfigurationEntry[]{
                new AppConfigurationEntry(NTLoginModule.class.getName(),
                    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
                    new HashMap<>())
        };
    }
};
Configuration.setConfiguration(config);
baruchiro
  • 5,088
  • 5
  • 44
  • 66
  • 1
    The name of the system property is `java.security.auth.login.config`. See the [documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/jaas/JAASRefGuide.html#Policy). No `javax` in there. Nothing here that identifies the actual problem. – user207421 Jan 09 '18 at 09:55
  • @EJP if the question is "how to config" so the programmicly way is not answer? – baruchiro Jan 09 '18 at 10:09
  • And with javax I'm not getting a "class not found" error – baruchiro Jan 09 '18 at 10:29
  • 1
    The question is why the class not found error, and `javax` is still wrong, and you won't get a 'class not found error' with `java` if you type it properly. – user207421 Jan 09 '18 at 21:44
0

Clearly there is a typo in the command line: a space after -Djava, so the .security.auth.login.config=jaas.config part is taken as the class name (and hence also mangled accordingly).

user207421
  • 305,947
  • 44
  • 307
  • 483
  • i get the same error ```Error: Could not find or load main class .security.auth.login.config=``` and i have no typo there – ulkas Sep 20 '21 at 18:15
  • Of course you do. Something is separating `-Djava` from `.security.auth.login.config`. – user207421 Sep 21 '21 at 03:38
  • well it turned out not. simply the command line config arguments were like this. no matter which one i picked (or copy pasted), it always wanted to find the class ```.someclass...```. i ended up setting these preferences directly in the code via: ```System.setProperty("-Djavax.security.auth.login.config", "value");``` – ulkas Sep 21 '21 at 12:06