How to mask JAVA Object confidential/personal information in logs while Printing


Here you will see all steps to mask confidential/ information like credit card, CVV, Exp date,  SSN, password etc. So that it will print in mask form as ****** so that unauthorize use will not misuse of others information.

Main concept to mask an Java for personal information by using reflection API and string override method by extending mask class.

Below are steps to masking Java Object :

  • Extends classes with MaskData class.
  • Override toString() method of object class and implement in MaskData Class.
  • Use JAVA reflection apis to get all fields objects and change SPI fields with *****.
  • Use replace digits methods method to replace digits with *.

Below are classes AccountDetail, Address and CredeitCard where only confidential information need to mask for Credit Card.

package com.model;

import com.mask.object.MaskData;

public class AccountDetail extends MaskData {
private String firstName;
private String lastName;
private AddressDetail address;
private CreditCardDetail creditCardDetail;
public AccountDetail(String firstName, String lastName, AddressDetail address, CreditCardDetail creditCardDetail) {
	super();
	this.firstName = firstName;
	this.lastName = lastName;
	this.address = address;
	this.creditCardDetail = creditCardDetail;
}
public String getFirstName() {
	return firstName;
}
public void setFirstName(String firstName) {
	this.firstName = firstName;
}
public String getLastName() {
	return lastName;
}
public void setLastName(String lastName) {
	this.lastName = lastName;
}
public AddressDetail getAddress() {
	return address;
}
public void setAddress(AddressDetail address) {
	this.address = address;
}
public CreditCardDetail getCreditCardDetail() {
	return creditCardDetail;
}
public void setCreditCardDetail(CreditCardDetail creditCardDetail) {
	this.creditCardDetail = creditCardDetail;
}

}

Address Class not having any confidential information.
package com.model;

import com.mask.object.MaskData;

public class AddressDetail extends MaskData{

private String addressLine1;
private String city;
private String state;
private String pincode;
private String country;

public AddressDetail(String addressLine1, String city, String state, String pincode, String country) {
	super();
	this.addressLine1 = addressLine1;
	this.city = city;
	this.state = state;
	this.pincode = pincode;
	this.country = country;
}
public String getAddressLine1() {
	return addressLine1;
}
public void setAddressLine1(String addressLine1) {
	this.addressLine1 = addressLine1;
}
public String getCity() {
	return city;
}
public void setCity(String city) {
	this.city = city;
}
public String getState() {
	return state;
}
public void setState(String state) {
	this.state = state;
}
public String getPincode() {
	return pincode;
}
public void setPincode(String pincode) {
	this.pincode = pincode;
}
public String getCountry() {
	return country;
}
public void setCountry(String country) {
	this.country = country;
}

}

CreditCardDetail class is having confidential fields cardNumber,cvv and expDate only these fields need to mask.

package com.model;

import com.mask.object.MaskData;

public class CreditCardDetail extends MaskData{
private String cardNumber;
private String cvv;
private String expDate;

public CreditCardDetail(String cardNumber, String cvv, String expDate) {
	super();
	this.cardNumber = cardNumber;
	this.cvv = cvv;
	this.expDate = expDate;
}

public String getCardNumber() {
	return cardNumber;
}
public void setCardNumber(String cardNumber) {
	this.cardNumber = cardNumber;
}
public String getCvv() {
	return cvv;
}
public void setCvv(String cvv) {
	this.cvv = cvv;
}
public String getExpDate() {
	return expDate;
}
public void setExpDate(String expDate) {
	this.expDate = expDate;
}

}

This is main class where masking these confidential fields by using JAVA reflection apis and override toString() method of Object Class.


package com.mask.object;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MaskData implements Cloneable {
	Set fieldSet=new HashSet();
	String[] spiData = { "cardNumber", "cvv", "expDate" };

	public Object clone() {
		return this;
	}

	@Override
	public String toString() {
		StringBuffer buffer=new StringBuffer();
		try
		{
		Object object=super.clone();
		printObjectStream(buffer,object);
		}
		 catch (CloneNotSupportedException ex)
		{
			 ex.printStackTrace();
		}
		return buffer.toString();
	}

	private void printObjectStream(StringBuffer buffer, Object object) {
		try {
			buffer.append(object.getClass().getCanonicalName()).append("[\n");
			Object value=null;
			for (Field field : object.getClass().getDeclaredFields()) {
				if(fieldSet.add(field.getName()))
				{
					field.setAccessible(true);
					//System.out.println(field.getName());
					buffer.append(field.getName() + "=");
					value = field.get(object);
					if(value!=null)
					{
						if(field.getType().isArray() |field.getType().getCanonicalName().startsWith("com.model"))
						{
							printObjectStream(buffer,value);
						}
						else if (Arrays.asList(spiData).contains(field.getName())) {
							//field.set(object, replaceDigits((String) field.get(object)));
							buffer.append(replaceDigits((String) value) );
						}
						else
						{
							buffer.append( value );
						}
					}
				}

				buffer.append("\n");
			}
			buffer.append("]");
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		}

	}

	private String replaceDigits(String text) {
		StringBuffer buffer = new StringBuffer(text.length());
		Pattern pattern = Pattern.compile("\\d");
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()) {
			matcher.appendReplacement(buffer, "X");
		}
		return buffer.toString();
	}

}

Below is Test program to above code .


package com.mask.object;
import com.model.AccountDetail;
import com.model.AddressDetail;
import com.model.CreditCardDetail;

public class MaskJavaObject {

	public static void main(String[] args) {
		AddressDetail addressDetail = new AddressDetail("Noida City Center", "Noida", "UP", "India", "20310");
		CreditCardDetail creditCardDetail = new CreditCardDetail("1234567890123456", "123", "12/90");
		AccountDetail accountDetail = new AccountDetail("Saurabh", "Gupta", addressDetail, creditCardDetail);
		System.out.println(accountDetail);
	}

}

Console Output

com.model.AccountDetail[
firstName=Saurabh
lastName=Gupta
address=com.model.AddressDetail[
addressLine1=Noida City Center
city=Noida
state=UP
pincode=India
country=20310
]
creditCardDetail=com.model.CreditCardDetail[
cardNumber=XXXXXXXXXXXXXXXX
cvv=XXX
expDate=XX/XX
]
]

Related Posts

Below are some more masking ways for different type of data like XML, JSON and printing objects before logging , sending to page or transferring over network.

Log4j2: How to Mask Logs Personal/Confidential/SPI Information

How to Mask JSON Confidential/Personal Information in logs :JAVA

How to MASK XML Confidential/Personal Data : JAVA

Advertisements

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 11 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Java, Mask, Security and tagged , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to How to mask JAVA Object confidential/personal information in logs while Printing

  1. Pingback: How to Mask JSON Confidential/Personal Information in logs :JAVA | Facing Issues On IT

  2. Pingback: Log4j2: How to Mask Logs Personal/Confidential/SPI Information | Facing Issues On IT

  3. Pingback: How to MASK XML Confidential/Personal Data : JAVA | Facing Issues On IT

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s