当前位置: 首页 > 工具软件 > License3j > 使用案例 >

License3j做web项目部署授权认证功能

方河
2023-12-01

一、功能流程

  • 密钥生成(gpg  --full-gen-key)

生成密钥:gpg --full-gen-key

    创建时需要留意的地方:RAS加密、userId

导出公钥:gpg   --output public-key  --export [userId]

 导出私钥:gpg  --output public-key --export-secret-key [userId]

  • 项目中引入License3j

<dependency>
           <groupId>com.verhas</groupId>
           <artifactId>license3j</artifactId>
           <version>2.0.0-JVM8</version>
       </dependency>

  • 使用密钥对员文件进行加密
OutputStream out;
try {
	out = new FileOutputStream(licenseFile);
	out.write(new License().setLicense(new File(originFile))
			  .loadKey(new File(privateKeyFile), userId)
			  .encodeLicense(privateProtectedPassword).getBytes("utf-8"));		
	out.close();
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (UnsupportedEncodingException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (PGPException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (NoSuchProviderException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (SignatureException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
  • 在授权校验处(用户登录)使用公钥对加密后的文件进行解密读取
License license = new License();
if(license.loadKeyRing(this.getClass().getResource("/").getPath()+ "/license/public-key", null)
		   .setLicenseEncodedFromFile(this.getClass().getResource("/").getPath()+"/license/my.license")
		   .isVerified()) {
	String auth = license.getFeature("auth");
	if("true".equals(auth)) {
		String authMac = license.getFeature("auth-mac");
		String  expireTime = license.getFeature("expire-time");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-mm");
		if(new Date().after(sdf.parse(expireTime))) {
			resultDto.setMessage("Auth Fail(Expired)");
			resultDto.setFlag(false);
			return resultDto;
		}
		
		if(!authMac.equals(CommonUtil.getServerHardwareAddress())) {
			resultDto.setMessage("Auth Fail(Information check error)");
			resultDto.setFlag(false);
			return resultDto;
		}
	}
}
public static String getServerHardwareAddress() {
	try {
		byte[] hardwareAddress = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
		StringBuffer sb = new StringBuffer("");
		if(hardwareAddress!=null) {
			for(int i=0; i<hardwareAddress.length; i++) {
				int temp = hardwareAddress[i]&0xff;
				String tempStr = Integer.toHexString(temp);
				if(tempStr.length()==1) {
					sb.append("0"+tempStr);
				}else {
					sb.append(tempStr);
				}
			}
		}
		return sb.toString().toUpperCase();
	} catch (SocketException e) {
		e.printStackTrace();
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
	return null;
}

实践过程中遇到的问题:(org.bouncycastle.asn1.ASN1Integer"'s signer information does not match)

引入的包和另一个封装的Jar包出现冲突,调整版本以及依赖顺序后问题得到解决

参考文章:

http://ju.outofmemory.cn/entry/98116

 类似资料: