java - 在java小程序中检查我们是否有打印权限而不提示用户

标签 java printing applet certificate

基本上,我有一个自签名(目前)的 Java 小程序可以打印内容。尽管我可以在不签署小程序的情况下进行打印,但我不想在用户每次访问我的网站时都提示他们。最糟糕的是,对于您在 PrinterJob 对象上执行的每个操作,它们都会收到提示。现在,如果他们接受证书,则不会收到任何打印提示,这正是我想要的行为。不幸的是,如果他们拒绝证书,则必须再次接受打印提示。我想做的是,如果他们拒绝证书,就停止小程序。为此,我尝试了以下方法:

public void init(){ 
    doPrivileged(new PrivilegedAction<Void>() {
        @Override
        public Void run() {
            _appsm = System.getSecurityManager();
            if (!hasPrintPermissions()) return null;

            printer = new MarketplaceLabelPrinter();
            LOG.info("Initialized");
            return null;
        }
    });
}

/**
 *  Returns true if the applet has enough permissions to print
 */
public boolean hasPrintPermissions(){
    try{
        _appsm.checkPrintJobAccess();
    } catch (SecurityException e) {
        LOG.severe("Not enough priviledges to print.");
        return false;
    }
    return true;
}

这确实有点作用,但它会提示用户,这是我不想要的。更糟糕的是,这个安全检查完全没有用,因为如果他们按“确定”但没有检查“始终允许这个小程序访问打印机”,安全检查会认为它可以访问打印机,但实际上却没有。 (参见:/image/ZkwFe.png)

总之,如果用户拒绝证书,我希望小程序停止运行。

谢谢大家

最佳答案

尝试/捕获不受信任的小程序中不允许的内容。伪代码例如

public static boolean isTrusted() {
  boolean trusted = false;
  try {
    SecurityManager sm = System.getSecurityManager();
    // not permitted in a sand-boxed app.
    System.setSecurityManager(null);
    // restore the trusted security manager.
    System.setSecurityManager(sm);
    // This code must be trusted, to reach here.
    trusted = true;
  catch(Throwable ignore) {}
  return trusted;
}

关于java - 在java小程序中检查我们是否有打印权限而不提示用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11588692/

相关文章:

java - Java读取USB打印机状态

java - Jpanel.add(Jpanel) 没有响应?

java - 读取动态生成的微调器的值 - 如何执行此操作?

java - 如何使用 Spring Batch 发送多封电子邮件

java - 队列中的Java线程一旦完成任务,就需要 hibernate

python - 拆分或替换 - 去掉两个字符串之间和周围的撇号和逗号

python - 如何使用pygtk打印图像

java - 非自签名证书是否会消除 Applet 的安全警告?

java - 大小程序 大用户群 低带宽

java - 如何从后台删除 Activity ?启动 Activity 结果