0

ライセンス モデルに基づいて Web アプリケーションに制限ルールを組み込んでいます。ライセンス ファイルに存在する情報を使用して、実行時に Java セキュリティ ポリシーを更新したいと考えています。これにより、war ファイル内にパッケージ化された特定の jar にアクセスしたり、Java パッケージへのアクセスを制限したりできます。

アプリケーション内のjarまたはパッケージへのアクセスを動的に制限する方法は? ポリシー ファイルに相対パスまたはパッケージ パスを指定できますか?

次のサンプルコードを見ています:

/**
 *  policy
 * grant {
 *    permission java.util.PropertyPermission "my.user.pwd", "read";
 *    permission java.lang.RuntimePermission "setSecurityManager";
 *    permission java.lang.RuntimePermission "createSecurityManager";
 *    permission java.lang.RuntimePermission "usePolicy";
 *  };
 * 
 */

class PasswordSecurityManager extends SecurityManager {
    public static void main(String[] args) {
        try {
            System.setProperty("my.user.pwd", "mysecret");
            // set the policy file as the system security policy
            System.setProperty("java.security.policy", "file:/C:/temp/policy/java-2.policy");
            System.setSecurityManager(new PasswordSecurityManager("mysecret"));

        } catch (SecurityException se) {
            System.out.println("SecurityManager already set!");
        }
        try {
            System.setProperty("my.user.pwd", "mysecret");
            DataInputStream fis = new DataInputStream(new FileInputStream("C:\\TEMP\\policy\\test.txt"));
            DataOutputStream fos = new DataOutputStream(new FileOutputStream("C:\\TEMP\\policy\\outputtext.txt"));
            String inputString;
            while ((inputString = fis.readLine()) != null) {
                fos.writeBytes(inputString);
                fos.writeByte('\n');
            }
            fis.close();
            fos.close();
        } catch (IOException ioe) {
            System.err.println("I/O failed for SecurityManagerTest.");
        }
    }

    String password;

    PasswordSecurityManager(String password) {
        super();
        this.password = password;
    }

    private boolean accessOK() {
        System.out.println("Reading from System... the secret password?");
        String response = System.getProperty("my.user.pwd");
        if (response.equals(password))
            return true;
        else
            return false;
    }

    @Override
    public void checkRead(FileDescriptor filedescriptor) {
        if (!accessOK())
            throw new SecurityException("Not a Chance!");
    }

    @Override
    public void checkRead(String filename) {
        if (!accessOK())
            throw new SecurityException("No Way!");
    }

    @Override
    public void checkRead(String filename, Object executionContext) {
        if (!accessOK())
            throw new SecurityException("Forget It!");
    }

    @Override
    public void checkWrite(FileDescriptor filedescriptor) {
        if (!accessOK())
            throw new SecurityException("Not!");
    }

    @Override
    public void checkWrite(String filename) {
        if (!accessOK())
            throw new SecurityException("Not Even!");
    }
}
4

1 に答える 1

0

上記のコードの次の方法は、私の問題を解決します....

    @Override
public void checkPackageAccess(String pkg) {
    super.checkPackageAccess(pkg);
    System.out.println("checkPackageAccess.." + pkg);
    if (!accessOK()) {
        if (pkg.startsWith("my.specialpackage.path")) {
            throw new SecurityException("No access to " + pkg);
        }
    }
}
于 2013-08-15T05:18:09.907 に答える