Java planning with Truelicense license control extension can be verified, license start end date, verify binding a given MAC address

Source: Internet
Author: User
Tags nets

Using Truelicense for Java Programming license control

The extension can be validated after the license start end date, verifying that a given MAC address is bound.

Truelicense It is an open source Java License Inspection project.

Use the Truelicense implementation for the javaprojectlicense mechanism (including license generation and validation), see http://www.it165.net/pro/html/201404/11540.html

Contains the principles of the license authorization mechanism and the detailed steps to make the license

This article is mainly based on the addition of MAC address verification:

In Createparam.properties

Add IP address and MAC address configuration in the

######### #common parameters########### #aliasPRIVATEALIAS =privatekeys#key (important!) keypwd=iptv1234#storepwdstorepwd=iptv1234#subjectsubject=bigdata#licpathlicpath=c:/9exce/license/license.lic# pripathpripath=c:/9exce/license/privatekeys.keystore######### #license content########### #issuedTimeissuedTime = 2015-03-09#notbeforetimenotbefore=2015-03-09#notaftertimenotafter=2016-03-20#ip addressipAddress= 150.236.220.200#mac addressmacaddress=80-00-0b-56-3b-32#consumertypeconsumertype=user# Consumeramountconsumeramount=1#infoinfo=this is a license


Due to the addition of MAC address, it is necessary to change the Validate function in LicenseManager to add create_validate for create verify, because the MAC address cannot be verified when the certificate is created.

protected synchronized void Create_validate (Licensecontent paramlicensecontent) throws Licensecontentexception { Licenseparam Locallicenseparam = Getlicenseparam (); if (!locallicenseparam.getsubject (). Equals ( Paramlicensecontent.getsubject ())) {throw new licensecontentexception (Exc_invalid_subject);} if (paramlicensecontent.getholder () = = null) {throw new licensecontentexception (Exc_holder_is_null);} if (paramlicensecontent.getissuer () = = null) {throw new licensecontentexception (Exc_issuer_is_null);} if (paramlicensecontent.getissued () = = null) {throw new licensecontentexception (Exc_issued_is_null);} Date localDate1 = new Date ();D ate localDate2 = Paramlicensecontent.getnotbefore (); if (localDate2! = null) && (loc Aldate1.before (LocalDate2)) {throw new licensecontentexception (Exc_license_is_not_yet_valid);} Date localDate3 = Paramlicensecontent.getnotafter (); if (localDate3! = null) && (Localdate1.after (localDate3)) {throw new licensecontentexception (exc_license_has_expired);} StRing str = Paramlicensecontent.getconsumertype (), if (str = = null) {throw new Licensecontentexception (exc_consumer_type_ Is_null);} Preferences localpreferences = Locallicenseparam.getpreferences (); if (localpreferences! = null) && ( Localpreferences.isusernode ())) {if (! User.equalsignorecase (str)) {throw new licensecontentexception (Exc_consumer_type_is_not_user);} if (Paramlicensecontent.getconsumeramount ()! = 1) {throw new Licensecontentexception (Exc_consumer_amount_is_not_one) ;}} else if (Paramlicensecontent.getconsumeramount () <= 0) {throw new Licensecontentexception (Exc_consumer_amount_is_ not_positive);}}

Update validate Add the MAC address of the authentication client server.

protected synchronized void Validate (Licensecontent paramlicensecontent) throws Licensecontentexception {Licenseparam Locallicenseparam = Getlicenseparam (); if (!locallicenseparam.getsubject (). Equals (Paramlicensecontent.getsubject () ) {throw new licensecontentexception (Exc_invalid_subject);} if (paramlicensecontent.getholder () = = null) {throw new licensecontentexception (Exc_holder_is_null);} if (paramlicensecontent.getissuer () = = null) {throw new licensecontentexception (Exc_issuer_is_null);} if (paramlicensecontent.getissued () = = null) {throw new licensecontentexception (Exc_issued_is_null);} Date localDate1 = new Date ();D ate localDate2 = Paramlicensecontent.getnotbefore (); if (localDate2! = null) && (loc Aldate1.before (LocalDate2)) {throw new licensecontentexception (Exc_license_is_not_yet_valid);} Date localDate3 = Paramlicensecontent.getnotafter (); if (localDate3! = null) && (Localdate1.after (localDate3)) {throw new licensecontentexception (exc_license_has_expired);} LicensechEckmodel Licensecheckmodel = (Licensecheckmodel) Paramlicensecontent.getextra (); String macAddress = licensecheckmodel.getipmacaddress (); try {if (! Listnets.validatemacaddress (macAddress)) {throw new licensecontentexception (exc_license_has_expired);}} catch (SocketException e) {//TODO auto-generated catch Blockthrow new Licensecontentexception (exc_license_has_expired) ;} String str = Paramlicensecontent.getconsumertype (), if (str = = null) {throw new Licensecontentexception (exc_consumer_ Type_is_null);} Preferences localpreferences = Locallicenseparam.getpreferences (); if (localpreferences! = null) && ( Localpreferences.isusernode ())) {if (! User.equalsignorecase (str)) {throw new licensecontentexception (Exc_consumer_type_is_not_user);} if (Paramlicensecontent.getconsumeramount ()! = 1) {throw new Licensecontentexception (Exc_consumer_amount_is_not_one) ;}} else if (Paramlicensecontent.getconsumeramount () <= 0) {throw new Licensecontentexception (Exc_consumer_amount_is_ not_positive);}}


The Create class listnets is used to read the client server's IP address and MAC address for validation. I used the function to verify the MAC address. After all, the customer may change the IP address of the machine

Package Zlicense.util;import Java.net.*;import java.util.*;import static Java.lang.system.out;public class ListNets { public static void Main (string args[]) throws socketexception {string ip = "150.236.220.200"; String mac = "80-00-0b-56-3b-32"; Boolean flag = Validatoipandmacaddress (IP, Mac); Boolean macflag = Validatemacaddress (ma c); out.printf ("Validatomacaddress flag=%s\n", Macflag); out.printf ("Validatoipandmacaddress flag=%s\n", flag); static void Displayinterfaceinformation (NetworkInterface netint) throws SocketException {out.printf ("Display Name:%s\ N ", Netint.getdisplayname ()); out.printf (" Name:%s\n ", Netint.getname ()); byte[] mac = Netint.gethardwareaddress (); if ( Mac! = null) {StringBuilder SB = new StringBuilder (); for (int i = 0; i < mac.length; i++) {Sb.append (String.Format ("%02 x%s ", Mac[i], (i < mac.length-1)?

"-": ""));} System.out.println ("mac=" + sb.tostring ());} enumeration<inetaddress> inetaddresses = netint.getinetaddresses (); for (inetaddress InetAddress: Collections.list (inetaddresses)) {out.printf ("inetaddress:%s\n", inetaddress); System.out.println ("inetaddress ip=" + inetaddress.gethostaddress ());} out.printf ("\ n");} public static Boolean validatemacaddress (String macAddress) throws SocketException {Boolean returnflag = false; Enumeration<networkinterface> nets = networkinterface.getnetworkinterfaces (); for (NetworkInterface Netint: Collections.list (Nets)) {byte[] Mac = Netint.gethardwareaddress (); StringBuilder sb = new StringBuilder (), if (Mac = null) {for (int i = 0; i < mac.length; i++) {Sb.append (String.Format ( "%02x%s", Mac[i], (i < mac.length-1)?

"-": ""));} System.out.println ("mac=" + sb.tostring ());} if (sb.tostring (). Equals (macAddress)) {Returnflag = true;}} return Returnflag;} public static Boolean validatoipandmacaddress (String ipaddress,string macAddress) throws SocketException {Boolean Returnflag = false; Enumeration<networkinterface> nets = networkinterface.getnetworkinterfaces (); for (NetworkInterface Netint: Collections.list (Nets)) {byte[] Mac = Netint.gethardwareaddress (); StringBuilder sb = new StringBuilder (), if (Mac = null) {for (int i = 0; i < mac.length; i++) {Sb.append (String.Format ( "%02x%s", Mac[i], (i < mac.length-1)?

"-" : ""));} System.out.println ("mac=" + sb.tostring ());} if (sb.tostring (). Equals (macAddress)) {enumeration<inetaddress> inetaddresses = Netint.getinetaddresses (); String IP = ""; for (inetaddress inetAddress:Collections.list (inetaddresses)) {IP = inetaddress.gethostaddress (); System.out.println ("inetaddress ip=" + inetaddress.gethostaddress ()); if (ipaddress.tostring (). Equals (IP)) { Returnflag = True;}}} return returnflag;}}



Creating a Licensecheckmodel is a model class that stores IP and MAC addresses

Package Zlicense.util;public class Licensecheckmodel {private String ipAddress;    private string Ipmacaddress;private string Cpuserial;private string Motherboardsn;private string harddisksn;public String getipaddress () {return ipAddress;} public void setipaddress (String ipAddress) {this.ipaddress = ipAddress;} Public String getipmacaddress () {return ipmacaddress;} public void setipmacaddress (String ipmacaddress) {this.ipmacaddress = ipmacaddress;} Public String getcpuserial () {return cpuserial;} public void Setcpuserial (String cpuserial) {cpuserial = cpuserial;} Public String Getmotherboardsn () {return motherboardsn;} public void Setmotherboardsn (String motherboardsn) {this.motherboardsn = Motherboardsn;} Public String GETHARDDISKSN () {return HARDDISKSN;} public void Setharddisksn (String harddisksn) {this.harddisksn = HARDDISKSN;}}


Update Createlicense add mac and IP configuration to read and write the license certificate. The use of Content.setextra (Licensecheckmodel);

Write the information that needs to be validated into the Licensecheckmodel and set into the content.

Changed all of the file read methods in the Truelicense. is read with an absolute path.

   Properties prop= new Properties ();                   InputStream in= getclass (). getResourceAsStream (Propertiespath);                                     try {                            inputstreamin = new FileInputStream (propertiespath);                            Prop.load (in);                   } catch (IOException e) {                            //todoauto-generated catch block                            e.printstacktrace ();                   }

All the configuration files and my generated key Stroe in the Java-license-jar/src/main/resources directory

Project Code Address:

Https://github.com/jingshauizh/JavaSpringSurmmary/tree/master/java-license-jar

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Java planning with Truelicense license control extension can be verified, license start end date, verify binding a given MAC address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.