Search code examples
javaeclipsejaas

Working with "codebases" in the security policy during development in Eclipse


I am working with Java security policies and it's still rather new for me. I will like to be able to run the code directly as a launch configuration, so I have a debugger and all the other nice IDE stuff.

I can get it to work until I use the codebase attribute in the policy.

I have a class in the package application that creates a LoginContext. The policy file is located in the root of my project. With this content it works fine:

grant {
   permission javax.security.auth.AuthPermission "createLoginContext.Sample";
   permission javax.security.auth.AuthPermission "doAsPrivileged";
};

But when I add the codebase it fails with Cannot create LoginContext. access denied ("javax.security.auth.AuthPermission" "createLoginContext.Sample")

grant codebase "file:./bin/application/-" {
   permission javax.security.auth.AuthPermission "createLoginContext.Sample";
   permission javax.security.auth.AuthPermission "doAsPrivileged";
};

I have tried these values for codebase

  1. file:application/-
  2. file:./application/-
  3. file:bin/application/-
  4. file:./bin/application/-
  5. file:C:/Full Path/to/project/bin/application/-
  6. file://C:/Full Path/to/project/bin/application/-

(In case it matters: The full path includes spaces)


Solution

  • I managed to find the problem using JAAS's debug output, which I can really recommend.

    In short the codebase is always the binary root folder (or jar) not the package folder containing the class file. In my case it meant that the codebase of all classes was path/to/project/bin which didn't match path/to/project/bin/application/- specified in the policy.

    To solve this one needs to have multiple bin-folders or "output folder" as Eclipse name them. To get this:

    1. Go into Project Properties > Java Build Path and select the Source tab
    2. Activate Allow output folders for source folders
    3. Add as many source folders you like, by clicking Add Folder
    4. For each source folder, specify the output folder by first selecting the output folder item and then click edit.

    Mark of important buttons in Project Properties

    After this you can have the policy point to each folder like this

    grant codebase "file:binLogin/-" {
        permission javax.security.auth.AuthPermission "modifyPrincipals";
    };
    
    grant codebase "file:binApp/-" {
       permission javax.security.auth.AuthPermission "createLoginContext.Sample";
       permission javax.security.auth.AuthPermission "doAsPrivileged";
    };
    

    I hope this might help others in the future.