JAAS授权中拒绝访问

时间:2014-03-05 05:30:43

标签: java authorization jaas securitymanager

我正在设计一个JAAS身份验证和授权应用程序。在身份验证部分,用户输入的用户名和密码与从数据库中检索的值匹配。每个用户有三个主体(用户名,密码,位置) 。成功的身份验证后,主体被添加到主题。这部分工作正常。

问题出在授权部分(当我设置SecurityManager时),根据位置(经理或员工)授予权限。

政策文件是

grant codebase "/home/esamsar/NetBeansProjects/JAAS/*"
{
permission javax.security.auth.AuthPermission "createLoginContext.Sample";
permission javax.security.auth.AuthPermission "doAsPrivileged";
}


grant   Principal PositionPrincipal "manager" 
{

   permission java.util.PropertyPermission "java.home", "read";
   permission java.util.PropertyPermission "user.home", "read,write";
   permission java.io.FilePermission "topsecurity.txt", "read";

};

grant   Principal PositionPrincipal "employee" 
{

   permission java.util.PropertyPermission "java.home", "read";
   permission java.util.PropertyPermission "user.home", "read,write";

};

添加以下代码行后,遇到"无法创建LoginContext。访问被拒绝(" java.util.PropertyPermission"" java.security.auth.login.config""写")"

 System.setSecurityManager(new SecurityManager());
 System.setProperty("java.security.auth.login.config", configFile);
 System.setProperty( "java.security.policy", policyFile );

configFile和policyFile分别是jaas配置文件和策略文件。 我可以将哪些内容添加到我的策略文件中以解决问题。请提前感谢。

1 个答案:

答案 0 :(得分:0)

你需要先写:

System.setProperty("java.security.auth.login.config", configFile);
System.setProperty( "java.security.policy", policyFile );

只有这样:

System.setSecurityManager(new SecurityManager());

但这可能不仅仅是问题。我有同样的问题,我修复了它。我的工作示例,但使用unix auth。这里是: 主要课程:

public class UserAuthLesson {
public static void main(String[] args) {
    try
    {
        System.setProperty("java.security.policy", "src/MyApp.policy");
        System.setProperty("java.security.auth.login.config", "src/jaas.config");
        System.setSecurityManager(new SecurityManager());
        LoginContext context = new LoginContext("Login1"); // defined in JAAS configuration file
        context.login();
        Subject subject = context.getSubject();
        context.logout();
    }
    catch (LoginException exception) // thrown if login was not successful
    {
        exception.printStackTrace();
    }
}
} 

政策文件:

grant{
permission javax.security.auth.AuthPermission "createLoginContext.Login1";
permission javax.security.auth.AuthPermission "doAsPrivileged";
};
grant principal com.sun.security.auth.UnixPrincipal "max" {
permission java.util.PropertyPermission "user.*", "read";
};

Jaas档案:

Login1 {
com.sun.security.auth.module.UnixLoginModule required;
};