springboot项目,现在用混淆加密代码,
同时想要实现在指定服务器上运行,
我想的是在程序启动的时候去获取本地ip,
然后比对ip地址,
相同的话运行,不相同的话程序结束,
但是试了好多种方法获取的都是127.0.0.1,在本地运行的时候获取的是局域网的IP,
想问一下各位大神有没有什么合适的解决方法,
用什么东西做限制无所谓,
只要实现只能在指定服务器运行这个要求就好
用云服务器的MAC地址做绑定可以吗,云服务器的MAC地址会不会更改?
感谢
按照你的思路,可以用公网ip,而不是本机ip,用httpclient去访问公网上一个能报告ip的网址(比如ip138),用返回的你服务器的公网ip代替本机ip
还可以用本机的硬盘卷标、主板序列号、mac等。不过这个需要jni去调用原生的代码,并且操作系统平台相关(windows、linux获取的方式不同)
对ip限制,一般将ip段配置到数据库,程序执行时到库中进行查找匹配
一般可以用网卡地址,即MAC地址。
InetAddress localInetAddress=null;
Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.isLoopbackAddress()) {//回路地址,如127.0.0.1
//xxxxx
} else if (inetAddress.isLinkLocalAddress()) {//169.254.x.x
//
} else {
// Use a non-link local, non-loop back address by default
localInetAddress=inetAddress;
}
}
}
byte[] mac = NetworkInterface.getByInetAddress(localInetAddress).getHardwareAddress();
StringBuffer sb = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1) {
sb.append("0"+str);
}else {
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
这里有一个借助ip138返回你的出口ip的代码
public class Test {
public static void main(String[] args) {
try {
String ip1 = getMyIP();
System.out.println("myIP:" + ip1);
String ip2 = getMyIPLocal();
System.out.println("myLocalIP:" + ip2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
private static String getMyIP() throws IOException {
InputStream ins = null;
try {
URL url = new URL("http://iframe.ip138.com/ic.asp");
URLConnection con = url.openConnection();
ins = con.getInputStream();
InputStreamReader isReader = new InputStreamReader(ins, "GB2312");
BufferedReader bReader = new BufferedReader(isReader);
StringBuffer webContent = new StringBuffer();
String str = null;
while ((str = bReader.readLine()) != null) {
webContent.append(str);
}
int start = webContent.indexOf("[") + 1;
int end = webContent.indexOf("]");
return webContent.substring(start, end);
} finally {
if (ins != null) {
ins.close();
}
}
}
private static String getMyIPLocal() throws IOException {
InetAddress ia = InetAddress.getLocalHost();
return ia.getHostAddress();
}
}
https://wenwen.sogou.com/z/q824838357.htm
看了你的问题,我觉得你可以换一种思路,你的目的是为了配置固定的服务使用程序,那么你可以在数据库做个配置,比如一个md5加密后的密钥,在程序调用的时候查找比对密钥。类似于这种的。
你需要了解的是如何获取ip地址:
Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.isLoopbackAddress()) {//回路地址,如127.0.0.1
System.out.println("loop addr:" + inetAddress);
} else if (inetAddress.isLinkLocalAddress()) {//169.254.x.x
System.out.println("link addr:" + inetAddress);
} else {
//非链接和回路真实ip
System.out.println("ip:" + inetAddress);
}
}
}
[点击并拖拽以移动]
结果:
loop addr:/127.0.0.1
loop addr:/0:0:0:0:0:0:0:1
ip:/192.168.10.89