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

一、功能流程

  • 密钥生成(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


           com.verhas
           license3j
           2.0.0-JVM8
       

  • 使用密钥对员文件进行加密
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

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

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

参考文章:

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

你可能感兴趣的:(Java,Web)