如何将安全策略文件添加到 rmi 应用程序的 gradle 构建中?



谁能告诉我如何将java命令"-Djava.security.policy=mysecurity.policy"添加到java rmi应用程序的gradle构建中。谢谢。

这是我运行 gradle run 时遇到的错误

格拉德尔跑

任务:运行 线程"main"中的异常 java.security.AccessControlException: 访问被拒绝("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472) at java.security.AccessController.checkPermission(AccessController.java:884) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051) at java.net.Socket.connect(Socket.java:584) at java.net.Socket.connect(Socket.java:538) 在java.net.Socket。(插座.java:434) 在java.net.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) 在 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";
}
}

您可以将 jvm 属性添加到测试配置中

test {
systemProperties 'java.security.policy': file("$projectDir/mysecurity.policy").absolutePath
}

相关内容

  • 没有找到相关文章

最新更新