Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyEncryptedPasswordFactory extends BasicDataSourceFactory {
- @Override
- public Object getObjectInstance(Object obj, Name name, Context context, Hashtable<?, ?> environment)
- throws Exception {
- if (obj instanceof Reference) {
- Reference ref = (Reference) obj;
- DecryptPasswordUtil.replacePasswordWithDecrypted(ref, "password");
- return super.getObjectInstance(obj, name, context, environment);
- } else {
- throw new IllegalArgumentException(
- "Expecting javax.naming.Reference as object type not " + obj.getClass().getName());
- }
- }
- }
- public class MyEncryptedAtomikosPasswordFactory extends EnhancedTomcatAtomikosBeanFactory {
- @Override
- public Object getObjectInstance(Object obj, Name name, Context context, Hashtable<?, ?> environment)
- throws NamingException {
- if (obj instanceof Reference) {
- Reference ref = (Reference) obj;
- DecryptPasswordUtil.replacePasswordWithDecrypted(ref, "xaProperties.password");
- return super.getObjectInstance(obj, name, context, environment);
- } else {
- throw new IllegalArgumentException(
- "Expecting javax.naming.Reference as object type not " + obj.getClass().getName());
- }
- }
- }
- public class DecryptPasswordUtil {
- public static void replacePasswordWithDecrypted(Reference reference, String passwordKey) {
- if(reference == null) {
- throw new IllegalArgumentException("Reference object must not be null");
- }
- // Search for password addr and replace with decrypted
- for (int i = 0; i < reference.size(); i++) {
- RefAddr addr = reference.get(i);
- if (passwordKey.equals(addr.getType())) {
- if (addr.getContent() == null) {
- throw new IllegalArgumentException("Password must not be null for key " + passwordKey);
- }
- String decrypted = yourDecryptionMethod(addr.getContent().toString());
- reference.remove(i);
- reference.add(i, new StringRefAddr(passwordKey, decrypted));
- break;
- }
- }
- }
- }
- <Resource factory="com.mycompany.MyEncryptedPasswordFactory" username="user" password="encryptedPassword" ...other options... />
- <Resource factory="com.mycompany.MyEncryptedAtomikosPasswordFactory" type="com.atomikos.jdbc.AtomikosDataSourceBean" xaProperties.user="user" xaProperties.password="encryptedPassword" ...other options... />
- <Resource
- name="jdbc/myDataSource"
- auth="Container"
- type="javax.sql.DataSource"
- username="user"
- password="encryptedpassword"
- driverClassName="driverClass"
- factory="mypackage.MyCustomBasicDataSourceFactory"
- url="jdbc:blabla://..."/>
- package mypackage;
- public class MyCustomBasicDataSourceFactory extends org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory {
- @Override
- public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
- Object o = super.getObjectInstance(obj, name, nameCtx, environment);
- if (o != null) {
- BasicDataSource ds = (BasicDataSource) o;
- if (ds.getPassword() != null && ds.getPassword().length() > 0) {
- String pwd = MyPasswordUtilClass.unscramblePassword(ds.getPassword());
- ds.setPassword(pwd);
- }
- return ds;
- } else {
- return null;
- }
- }
- }
- @Bean
- public DataSource dataSource() {
- DataSource ds = null;
- JndiTemplate jndi = new JndiTemplate();
- try {
- ds = jndi.lookup("java:comp/env/jdbc/myDataSource", DataSource.class);
- } catch (NamingException e) {
- log.error("NamingException for java:comp/env/jdbc/myDataSource", e);
- }
- return ds;
- }
- print(SHA1CryptoServiceProvider sHA1Hasher = new SHA1CryptoServiceProvider();
- ASCIIEncoding enc = new ASCIIEncoding();
- byte[] arrbytHashValue = sHA1Hasher.ComputeHash(enc.GetBytes(clearTextPW));
- string HashData = System.BitConverter.ToString(arrbytHashValue);
- HashData = HashData.Replace("-", "");
- if (HashData == databaseHashedPassWO)
- {
- return true;
- }
- else
- {
- return false;
- });
Add Comment
Please, Sign In to add comment