Results 1 to 4 of 4
  1. #1
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Hi, I want to give Clipboard access to an applet.
    I edited <JAVA_HOME>\jre6\lib\security\java.policy
    file to add:

    grant codeBase "http://www.blahblah.net/*" {
    permission java.awt.AWTPermission "accessClipboard";
    };

    It still throws java.security.AccessControlException.

    when I add the permission to the global

    grant {
    }

    area, it works. But for obvious security reasons
    I want to limit the access to some domains.

    Am I missing something? I also tried
    grant codeBase "http://www.blahblah.net/"
    and
    grant codeBase "http://www.blahblah.net/-"
    but nothing works... Any Ideas?

    Thanks

    BTW the applet is NOT signed.


    Here a sample code for the problem:

    Clippy.java
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.StringSelection;
    import java.awt.datatransfer.ClipboardOwner;
    import java.awt.datatransfer.Transferable;
    import java.awt.Toolkit;
    public class Clippy extends Applet implements ClipboardOwner
    {
    		public void init() 
    		{
    
    		}
    
    		public void stop() 
    		{
    
    		}
    		
    		public void paint(Graphics g) 
    		{
    
    		}
    	
    		public static String getCustomStackTrace(Throwable aThrowable) {
    			//add the class name and any message passed to constructor
    			final StringBuilder result = new StringBuilder( "EXCEPTION: " );
    			result.append(aThrowable.toString());
    			final String NEW_LINE = System.getProperty("line.separator");
    			result.append(NEW_LINE);
    
    			//add each element of the stack trace
    			for (StackTraceElement element : aThrowable.getStackTrace() ){
    			result.append( element );
    			result.append( NEW_LINE );
    			}
    			return result.toString();
    		}
    
    		public void copyToClipBoard(String content) {
    			Graphics g = getGraphics();	
    			System.out.println("Copying!!"); 
    			try{		  
    				Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    				StringSelection stringSelection = new StringSelection(content);
    				clipboard.setContents(stringSelection,this);
    				g.drawString("Copied!!!", 10, 10);
    			}
    			catch (Exception e)
    			{
    				g.drawString(getCustomStackTrace(e), 10, 10);
    				//e.printStackTrace();
    				System.out.println(getCustomStackTrace(e));
    				
    			}
    			
    		}
    
    		public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
    		//do nothing
    		}
    
    
    }
    Test.html
    Java Code:
    <html>
    <body>
    <SCRIPT>
    function getScreenDimension() {
    
      }
    </SCRIPT>
    <FORM>
    <INPUT type="button" value="call Java Applet method" 
       onClick = "document.clip.copyToClipBoard('dadada');">
    </FORM>
    <applet codebase="http://www.blahblah.net/"  code=Clippy name="clip" id="Clippy" width=400 height=200 mayscript="true" scriptable="true">
    <param name="scriptable" value="true" />
    </applet>
    </body>
    </html>
    Last edited by cultclassic; 05-11-2011 at 09:31 PM.

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default Permission on policy file

    For some reason, the first post appeared after the second post... So I combined them both above...
    Last edited by cultclassic; 05-11-2011 at 10:03 PM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    I ran a test and was able to have the applet code copy to the clipboard when the copy... method was called in the start method. It fails when called on the button push.
    I added a param to the html for the applet: <param name="DoCopy" value="true" />
    The in start:
    Java Code:
          public void start() {
             // Fake a call if requested
             String doit = getParameter("DoCopy");
             if(doit != null && doit.equals("true"))
                copyToClipBoard("Copied from Applet");
          }
    My .java.policy file entry:

    grant codeBase "file:/D:/JavaDevelopment/Testing/ForumQuestions5/-" {
    permission java.awt.AWTPermission "accessClipboard";
    };

    The java console shows:

    Java Plug-in 1.6.0_20
    Using JRE version 1.6.0_20-b02 Java HotSpot(TM) Client VM
    User home directory = C:\Documents and Settings\Owner

    ----------------------------------------------------
    c: clear console window
    f: finalize objects on finalization queue
    g: garbage collect
    h: display this help message
    l: dump classloader list
    m: print memory usage
    o: trigger logging
    q: hide console
    r: reload policy configuration
    s: dump system and deployment properties
    t: dump thread list
    v: dump thread stack
    x: clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------

    Copying!! content=Copied from Applet<
    copied!


    I added the text of content and the println for "copied!" so the output shows on the log

    I'm using Firefox.

  4. #4
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Whoa! Interesting! Thanks Norm...

    I did some more experimentation. On a hunch, I made the clipboard object a class variable and initialized it on init();
    Now it works, even from the Javascript call.

    Have no clue why, have to look in to it.

Similar Threads

  1. File Permission Problem
    By ProgrammingPup in forum Java Applets
    Replies: 2
    Last Post: 12-29-2009, 11:48 PM
  2. File Permission Problem
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 12-29-2009, 05:05 PM
  3. Replies: 1
    Last Post: 10-16-2008, 09:56 PM
  4. Djava.security.policy=applet.policy
    By willemjav in forum Java Applets
    Replies: 1
    Last Post: 03-09-2008, 01:57 AM
  5. Djava.security.policy=applet.policy
    By willemjav in forum NetBeans
    Replies: 0
    Last Post: 03-09-2008, 01:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •