Today's browsers can let the users enable/disable Java 2 security restrictions like file or clipboard access. How can an Applet (JApplet) check if it can access to the file system or clipboard?

Jon Wingfield

The SecurityManager can be used to check whether access to a resource is allowed.
Code for checking SystemClipboard access in JDK1.1

SecurityManager secMan = System.getSecurityManager();
if (secMan!=null) {
    try {
        secMan.checkSystemClipboardAccess();
    } catch (SecurityException se) {
        // can't access clipboard
    }
    try {
        secMan.checkWrite("c:	emp");
    } catch (SecurityException se) {
        // can't write to c:	emp
    }
    try {
        secMan.checkRead("c:	emp");
    } catch (SecurityException se) {
        // can't read from c:	emp
    }
}
For Java 2 the previous code still works but can be replaced by the general checkPermission(Permission permission) mechanism:
SecurityManager secMan = System.getSecurityManager();
if (secMan!=null) {
    try {
        secMan.checkPermission(java.awt.AWTPermission("accessClipboard"));
    } catch (SecurityException se) {
        // can't access clipboard
    }
    try {
        secMan.checkPermission(java.io.FilePermission("c:	emp" ,"write"));
    } catch (SecurityException se) {
        // can't write to c:	emp
    }
    try {
        secMan.checkPermission(java.io.FilePermission("c:	emp" ,"read"));
    } catch (SecurityException se) {
        // can't read from c:	emp
    }
}
For more info check out these URLs:
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact