Java RMI security problems.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
I keep getting this error message whenever I try to run the applet inside a web browser.
Code:
java.sexurity.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
Code:
import java.rmi.*;

public class ChatServer {
    public ChatServer() {
        try {
            ChatImplementation ci = new ChatImplementation();
            Naming.rebind("rmi://localhost/ChatService", ci);
		} catch (Exception e) {
            System.out.println(e);
		}
    }

    public static void main(String args[]) {
        new ChatServer();
    }
}

Code:
import java.rmi.*;
import javax.swing.*;
import java.util.*;

public interface ChatInterface extends Remote {
    
    public void logOn(String name) throws RemoteException;

    public void sendText(String text) throws RemoteException;

    public JTextArea updateChat() throws RemoteException;

    public Vector getUsers() throws RemoteException;
}

Code:
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
import javax.swing.*;

public class ChatImplementation extends UnicastRemoteObject implements ChatInterface {
    private Vector userList = new Vector();
    private JTextArea chatText = new JTextArea();

    public ChatImplementation() throws RemoteException {
        super();
    }
    
    public void logOn(String name) throws RemoteException {
        userList.add(name);
    }

    public void sendText(String text) throws RemoteException {
        chatText.append(text + "\n");
    }

    public JTextArea updateChat() throws RemoteException {
        return chatText;
    }

    public Vector getUsers() throws RemoteException {
        return userList;
    }
}

Code:
package program;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

public class ChatClient extends JApplet implements ActionListener {
        JTextArea userText;
        JTextArea chatText;
        JButton send;
        JList userList;
        ChatInterface c;
        String nick;

     public void init() {
        try {
            c = (ChatInterface) Naming.lookup("rmi://localhost/ChatService");
            JPanel contentPanel = new JPanel();
            setContentPane(contentPanel);
            contentPanel.setLayout(new BorderLayout());
            JPanel textEntry = new JPanel();
            userList = new JList(c.getUsers());
            JScrollPane usersList = new JScrollPane(userList);
            chatText = c.updateChat();
            JScrollPane chatScrollPane = new JScrollPane(chatText);
            contentPanel.add(chatScrollPane, BorderLayout.CENTER);
            contentPanel.add(textEntry, BorderLayout.SOUTH);
            contentPanel.add(usersList, BorderLayout.EAST);
            userText = new JTextArea (3, 30);
            JScrollPane userScrollPane = new JScrollPane(userText);
            textEntry.add(userScrollPane);
            send = new JButton("Send");
            send.addActionListener(this);
            textEntry.add(send);
            userList.setPrototypeCellValue("------------------------");
            chatText.setLineWrap(true);
            userText.setLineWrap(true);
            chatText.setEditable(false);
            c.logOn(JOptionPane.showInputDialog(null, "Please enter a user name"));
            updateUsers();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

     public void updateChat() {
        try {
            chatText.setText(c.updateChat().getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
     }

     public void updateUsers() {
         try {
            userList.setListData(c.getUsers());
         } catch (Exception e) {
             JOptionPane.showMessageDialog(null, e);
         }
     }

     public void actionPerformed(ActionEvent ae) {
         if (! userText.getText().equals("")) {
            try {
                c.sendText(nick + ": " + userText.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
            userText.setText(null);
            updateChat();
        }
     }
}

However if I run the client as a swing application like this, it runs fine.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

public class ChatClient extends JFrame implements ActionListener {
        JTextArea userText;
        JTextArea chatText;
        JButton send;
        JList userList;
        ChatInterface c;
        String nick;

     public ChatClient() {
	super();
        try {
            c = (ChatInterface) Naming.lookup("rmi://localhost/ChatService");
            JPanel contentPanel = new JPanel();
            setContentPane(contentPanel);
            contentPanel.setLayout(new BorderLayout());
            JPanel textEntry = new JPanel();
            userList = new JList(c.getUsers());
            JScrollPane usersList = new JScrollPane(userList);
            chatText = c.updateChat();
            JScrollPane chatScrollPane = new JScrollPane(chatText);
            contentPanel.add(chatScrollPane, BorderLayout.CENTER);
            contentPanel.add(textEntry, BorderLayout.SOUTH);
            contentPanel.add(usersList, BorderLayout.EAST);
            userText = new JTextArea (3, 30);
            JScrollPane userScrollPane = new JScrollPane(userText);
            textEntry.add(userScrollPane);
            send = new JButton("Send");
            send.addActionListener(this);
            textEntry.add(send);
            userList.setPrototypeCellValue("------------------------");
            chatText.setLineWrap(true);
            userText.setLineWrap(true);
            chatText.setEditable(false);
        } catch (Exception e) {
            JOptionPane.showInputDialog(null, e);
        }
        nick = JOptionPane.showInputDialog(null, "Please enter a user name");
        logOn(nick);
	setVisible(true);
	pack();
	setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

     public void logOn(String name) {
         try {
            c.logOn(name);
            updateUsers();
         } catch (Exception e) {
            JOptionPane.showInputDialog(null, e);
         }
     }

     public void updateChat() {
        try {
            chatText.setText(c.updateChat().getText());
        } catch (Exception e) {
            JOptionPane.showInputDialog(null, e);
        }
     }

     public void updateUsers() {
         try {
            userList.setListData(c.getUsers());
         } catch (Exception e) {
             JOptionPane.showInputDialog(null, e);
         }
     }

     public void actionPerformed(ActionEvent ae) {
         if (! userText.getText().equals("")) {
            try {
                c.sendText(nick + ": " + userText.getText());
            } catch (Exception e) {
                JOptionPane.showInputDialog(null, e);
            }
            userText.setText(null);
            updateChat();
        }
     }

    public static void main(String[] args) {
	new ChatClient();
	}
}

This is my html code.

Code:
<html>
<applet code = 'program.ChatClient' 
    archive = 'Program.jar', 
    width = 600, 
    height = 600 />
</html>

The jar file contains the ChatClient and the ChatInterface classes and was created by netbeans, all the files are running from the same directory.
 
Last edited:
Yeah I'm trying to override the permissions but I'm not getting very far. Adding this to the client and server
Code:
System.setProperty("java.security.policy", "C:\\Program Files\\Java\\jdk1.6.0_18\\bin\\server.policy");
	System.setSecurityManager(new RMISecurityManager());
with this policy
Code:
grant {
permission java.security.AllPermission;
};
gets me past the first error onto this one
Code:
Java Plug-in 1.6.0_18
Using JRE version 1.6.0_18-b07 Java HotSpot(TM) Client VM
User home directory = C:\Users\Ash
----------------------------------------------------
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>
----------------------------------------------------


java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.System.setProperty(Unknown Source)
	at program.ChatClient.init(ChatClient.java:16)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
:(
 
If I try to run the applet in netbeans I get this error,
error777.png


could this be related?
 
I set everything up in netbeans and now everything works correctly using the netbeans program without any security modifications or anything.
 
Back
Top Bottom