Java program using Truelicense license control expands to verify the license Start end date, verifying that the given MAC address is bound

Source: Internet
Author: User
Tags nets

Java Program License control using Truelicense

After expansion you can verify the license Start end Date and verify that the given MAC address is bound.

Truelicense is an open source Java License Validation project.

Using the Truelicense implementation for the Java Engineering License mechanism (including license generation and validation), refer to Http://www.it165.net/pro/html/201404/11540.html

These include the principle of license authorization mechanism and the concrete steps of making license

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

In Createparam.properties

Increase the configuration of the IP address and MAC address

######### #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


Because the MAC address is added, the Validate function in LicenseManager needs to be changed to increase the verify of create_validate for Create, 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 increase 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);}}


To create a class listnets to read the client server's IP address and MAC address for verification, the author uses a function to verify the MAC address, after all, the customer may change the machine's IP address

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 read and write license certificate, using Content.setextra (Licensecheckmodel);

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

Changed all the file read methods in the Truelicense, using absolute path reading.

   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 profiles and my generated key Stroe under the Java-license-jar/src/main/resources folder

Project Code Address:

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

Java program using Truelicense license control expands to verify the license Start end date, verifying that the given MAC address is bound

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.