简体   繁体   English

如何解决java.security.AccessControlException

[英]How to solve java.security.AccessControlException

I need suggestions concerning the java.security.AccessControlException , I get when executing the following code. 我需要有关java.security.AccessControlException建议,我在执行以下代码时会得到建议。 (I have consulted similar questions here but didn't success to make it work) (我在这里曾咨询过类似的问题,但未能成功实现)

Here is my server code: 这是我的服务器代码:

 public class GetPageInfos extends UnicastRemoteObject  implements RemoteGetInfo{

    private static final String url="http://www.lemonde.fr/";
public class GetPageInfos extends UnicastRemoteObject  implements RemoteGetInfo{
    private static final String url="http://www.lemonde.fr/";

    public GetPageInfos() throws RemoteException{           
    }

    public String getSiteInfos() throws RemoteException {
         Document doc;
            try {                   
                 doc = Jsoup.connect(url).get();
                 String title = doc.title();  
                 return "title is "+title;                                       
            } catch (IOException e) {
                System.out.println("Faild! "+e.getMessage());
                return "not found";
            }  

    }

    public static void main(String[] args){
         try {

            GetPageInfos infos= new GetPageInfos();
            //System.setProperty("java.rmi.server.hostname","5lq04x1.gemalto.com");
            Naming.rebind("RemoteGetInfo", infos);
             /*GetPageInfos obj=new GetPageInfos(); 
             RemoteGetInfo stub = (RemoteGetInfo) UnicastRemoteObject.exportObject(obj, 0);
             Registry registry = LocateRegistry.getRegistry();
             registry.bind("RemoteGetInfo", stub);
             */
             System.out.println("server ready");

        } catch (RemoteException e) {
              System.out.println("GetPageInfos "+e.getMessage());
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

And here is my client code: 这是我的客户代码:

//RMI Client
public class PrintSiteInfos {

    public static void main(String arg[]) 
    { 

        System.setSecurityManager(new RMISecurityManager());

             try 
                { 

                   /*String host=null;
                   Registry registry = LocateRegistry.getRegistry(host);
                   RemoteGetInfo stub = (RemoteGetInfo) registry.lookup("RemoteGetInfo");
                   String response = stub.getSiteInfos();
                   System.out.println(response); */
                 RemoteGetInfo obj = (RemoteGetInfo) Naming.lookup( "RemoteGetInfo");        
                 System.out.println(obj.getSiteInfos()); 
                } 
                catch (Exception e) 
                { 
                   System.out.println("PrintSiteInfos exception: " + e.getMessage()); 
                   e.printStackTrace(); 
                }  
    }   
}

So I got 所以我得到了

exception: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

I found that I have to pass a policy file which I have like: 我发现我必须传递一个我喜欢的策略文件:

grant { 
  permission java.security.AllPermission;}; 

But how? 但是如何? Anyother suggestions? 还有其他建议吗?

You may grant only the socket permission not all permissions (which might be a security risk). 您可以只授予套接字权限,而不能授予所有权限(这可能会带来安全风险)。 Thus something like: 因此类似:

grant {
    permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
};

Two ways to do it: 有两种方法:

1) As an argument at the command line 1)作为命令行的参数

java -Djava.security.policy=mypolicyfile PrintSiteInfo

2) within the JRE environment: 2)在JRE环境中:

Add the permission in the JRE_HOME/lib/security/java.policy file 在JRE_HOME / lib / security / java.policy文件中添加权限

Get rid of the security manager. 摆脱安全管理器。 You don't need it unless you're using the RMI codebase feature. 除非您正在使用RMI代码库功能,否则不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM