任何人都可以告诉我如何将java命令"-Djava.security.policy=mysecurity.policy"
添加到gradle
应用程序的java rmi
版本中。感谢。
这是我运行gradle run时得到的错误
gradle run
任务:运行 线程“main”中的异常java.security.AccessControlException:访问被拒绝(“java.net.SocketPermission”“127.0.0.1:1099”“connect,resolve”) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472) 在java.security.AccessController.checkPermission(AccessController.java:884) 在java.lang.SecurityManager.checkPermission(SecurityManager.java:549) 在java.lang.SecurityManager.checkConnect(SecurityManager.java:1051) 在java.net.Socket.connect(Socket.java:584) 在java.net.Socket.connect(Socket.java:538) 在java.net.Socket。(Socket.java:434) 在java.net.Socket。(Socket.java:211) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338) at sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147) at identityserver.server.IdServer.setupServer(IdServer.java:73) at identityserver.server.IdServer.main(IdServer.java:32) <<< ====== ----> 75%执行[13s]
:运行
// Gradle构建文件
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'identityserver.server.IdServer'
repositories{
mavenCentral()
}
dependencies{
compile "commons-cli:commons-cli:1.4"
}
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
sourceSets{
main.java.srcDirs = ["src/identityserver/server", "src/identityserver/client"]
}
// Java rmi服务器类,安全管理器设置
package identityserver.server;
import org.apache.commons.cli.*;
import java.awt.*;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class IdServer extends UnicastRemoteObject implements ServerOperations{
private static final long serialVersionUID = -5057719935941913677L;
private static int registryPort = 1099;
private Options options;
private String[] args;
private String name;
public static void main(String[] args){
IdServer idServer = null;
try {
idServer = new IdServer(args,"//IdServer");
} catch (RemoteException e) {
System.out.println("IdServer err: " + e.getMessage());
e.printStackTrace();
}
if (args.length > 3){
idServer.usage();
System.exit(1);
}
idServer.parse();
idServer.setupServer(idServer);
}
public IdServer(String[] args, String name) throws RemoteException{
super();
this.args = args;
this.name = name;
options = new Options();
options.addOption("p","port",true, "port number");
options.addOption("v", "verbose",false, "print detailed messages");
}
public void parse(){
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (cmd.hasOption("p")){
System.out.println("Have port number " + cmd.getOptionValue("p"));
}else if (cmd.hasOption("v")){
System.out.println("Verbose!!!!");
}
} catch (ParseException e) {
// e.printStackTrace();
usage();
}
}
private void usage(){
System.err.println("Usage: java IdServer [--port <port#>][--verbose]");
}
public void setupServer(IdServer idServer){
try {
System.setSecurityManager(new SecurityManager());
Registry registry = LocateRegistry.getRegistry(registryPort);
registry.rebind("IdServer", idServer);
System.out.println("IdServer bound in registry");
} catch (RemoteException e) {
e.printStackTrace();
System.exit(1);
}
}
@Override
public String createAccount(String loginName) throws RemoteException {
return "Account created without password";
}
@Override
public String createAccount(String loginName, String realName, String password) throws RemoteException {
return "Account created with password";
}
}
答案 0 :(得分:0)
您可以将jvm属性添加到测试配置
test {
systemProperties 'java.security.policy': file("$projectDir/mysecurity.policy").absolutePath
}