License 3 jはwebプロジェクトの配置許可認証機能を行う

2963 ワード

一、機能フロー
  • 鍵生成(gpg--full-gen-key)
  • 鍵の生成:gpg--full-gen-key
    作成時に注意すべき点:RAS暗号化、userId
    公開鍵のエクスポート:gpg--output public-key--export[userId]
    秘密鍵のエクスポート:gpg--output public-key--export-secret-key[userId]
  • プロジェクトにLicense 3 j
  • を導入
               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.asn 1.ASN 1 Integer's signer information does not match)
    導入されたパッケージと別のパッケージのJarパッケージが競合し、バージョンを調整し、依存順位を調整した後、問題が解決されました.
    参考記事:
    http://ju.outofmemory.cn/entry/98116