Skip to content

Instantly share code, notes, and snippets.

@IVedmak
Last active August 29, 2015 14:01
Show Gist options
  • Save IVedmak/7ec70d6744743a3c30eb to your computer and use it in GitHub Desktop.
Save IVedmak/7ec70d6744743a3c30eb to your computer and use it in GitHub Desktop.
Solution for saving encrypted password attribute in mongodb
public class Attribute<T> extends BaseModel {
private String parentObjectName;
private AttributeType attributeType;
private String name;
private String displayName;
private String fullName;
private T value;
private T defaultValue;
// add getters and setters
}
public enum AttributeType {
STRING,LONG,INTEGER,DATE,DATE_TIME,ENUM,PASSWORD,BOOLEAN,TEXT_AREA;
}
import com.mongodb.DBObject;
import com.vsapphire.manager.plugins.base.AttributeType;
import com.vsapphire.utils.EncryptionUtils;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
@Component
public class AttributeEncryptionListener extends AbstractMongoEventListener {
@Override
public void onBeforeConvert(Object source) {
super.onBeforeConvert(source);
passwordsPersistenceCare(source,true);
}
private void passwordsPersistenceCare(Object source, boolean shouldEncrypt) {
for (Field field : source.getClass().getDeclaredFields()) {
if (Collection.class.isAssignableFrom(field.getType())) {
if (((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0].toString().endsWith(Attribute.class.getName())) {
try {
field.setAccessible(true);
Collection<Attribute> attrs = (Collection<Attribute>) field.get(source);
for (Attribute attr : attrs) {
if (AttributeType.PASSWORD.equals(attr.getAttributeType())) {
if (shouldEncrypt) {
attr.setValue(EncryptionUtils.AESEncrypt(attr.getValue().toString()));
} else {
attr.setValue(EncryptionUtils.AESDecrypt(attr.getValue().toString()));
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
@Override
public void onAfterConvert(DBObject dbo, Object source) {
super.onAfterConvert(dbo, source);
passwordsPersistenceCare(source,false);
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.util.Base64;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptionUtils {
protected static final Log logger = LogFactory.getLog(EncryptionUtils.class);
private static final String KEY = "0e127edb-908c-44";
private static final String SECRET = "36648650-e1b5-42";
public static String getMD5Encrypted(String password) {
Md5PasswordEncoder encoder = new Md5PasswordEncoder();
return encoder.encodePassword(password, null);
}
public static String AESEncrypt(String value) {
String key = KEY;
String secret = SECRET;
try {
IvParameterSpec iv = new IvParameterSpec(secret.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
public static String AESDecrypt(String encrypted) {
String key = KEY;
String secret = SECRET;
try {
IvParameterSpec iv = new IvParameterSpec(secret.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
logger.error(ex);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment