2012-07-21 44 views
2

基本上,我有一个自签名(现在)的Java applet,用于打印内容。尽管我可以在不登录小程序的情况下进行打印,但我不想在每次访问我的网站时提示用户。最糟糕的部分是,他们会在PrinterJob对象上执行每个操作的提示。现在,如果他们接受证书,则不会得到任何打印提示,这正是我想要的行为。不幸的是,如果他们拒绝证书,他们必须再次接受打印提示。我想要做的是停止小程序,如果他们拒绝证书。要做到这一点,我已经尝试了以下几种:检查我们是否在没有提示用户的情况下在java applet中拥有打印许可

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; 
} 

这有点用,但它提示用户,我不想要。更糟糕的是,这种安全检查完全没用,因为如果他们按下“确定”但不勾选“始终允许该小程序访问打印机”,安全检查认为它可以访问打印机,但事实上并非如此。 (请参阅:http://i.imgur.com/541YW.png

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

谢谢大家

回答

2

做的东西,不会在不受信任的小程序允许一个try/catch。伪码例如

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; 
} 
+0

够公平的,谢谢! – Nepoxx 2012-07-23 16:35:06

相关问题