Guest User

Untitled

a guest
Jan 9th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. public class MyEncryptedPasswordFactory extends BasicDataSourceFactory {
  2.  
  3. @Override
  4. public Object getObjectInstance(Object obj, Name name, Context context, Hashtable<?, ?> environment)
  5. throws Exception {
  6. if (obj instanceof Reference) {
  7. Reference ref = (Reference) obj;
  8. DecryptPasswordUtil.replacePasswordWithDecrypted(ref, "password");
  9. return super.getObjectInstance(obj, name, context, environment);
  10. } else {
  11. throw new IllegalArgumentException(
  12. "Expecting javax.naming.Reference as object type not " + obj.getClass().getName());
  13. }
  14. }
  15. }
  16.  
  17. public class MyEncryptedAtomikosPasswordFactory extends EnhancedTomcatAtomikosBeanFactory {
  18. @Override
  19. public Object getObjectInstance(Object obj, Name name, Context context, Hashtable<?, ?> environment)
  20. throws NamingException {
  21. if (obj instanceof Reference) {
  22. Reference ref = (Reference) obj;
  23. DecryptPasswordUtil.replacePasswordWithDecrypted(ref, "xaProperties.password");
  24. return super.getObjectInstance(obj, name, context, environment);
  25. } else {
  26. throw new IllegalArgumentException(
  27. "Expecting javax.naming.Reference as object type not " + obj.getClass().getName());
  28. }
  29. }
  30. }
  31.  
  32. public class DecryptPasswordUtil {
  33.  
  34. public static void replacePasswordWithDecrypted(Reference reference, String passwordKey) {
  35. if(reference == null) {
  36. throw new IllegalArgumentException("Reference object must not be null");
  37. }
  38.  
  39. // Search for password addr and replace with decrypted
  40. for (int i = 0; i < reference.size(); i++) {
  41. RefAddr addr = reference.get(i);
  42. if (passwordKey.equals(addr.getType())) {
  43. if (addr.getContent() == null) {
  44. throw new IllegalArgumentException("Password must not be null for key " + passwordKey);
  45. }
  46. String decrypted = yourDecryptionMethod(addr.getContent().toString());
  47. reference.remove(i);
  48. reference.add(i, new StringRefAddr(passwordKey, decrypted));
  49. break;
  50. }
  51. }
  52. }
  53. }
  54.  
  55. <Resource factory="com.mycompany.MyEncryptedPasswordFactory" username="user" password="encryptedPassword" ...other options... />
  56.  
  57. <Resource factory="com.mycompany.MyEncryptedAtomikosPasswordFactory" type="com.atomikos.jdbc.AtomikosDataSourceBean" xaProperties.user="user" xaProperties.password="encryptedPassword" ...other options... />
  58.  
  59. <Resource
  60. name="jdbc/myDataSource"
  61. auth="Container"
  62. type="javax.sql.DataSource"
  63. username="user"
  64. password="encryptedpassword"
  65. driverClassName="driverClass"
  66. factory="mypackage.MyCustomBasicDataSourceFactory"
  67. url="jdbc:blabla://..."/>
  68.  
  69. package mypackage;
  70.  
  71. public class MyCustomBasicDataSourceFactory extends org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory {
  72. @Override
  73. public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
  74. Object o = super.getObjectInstance(obj, name, nameCtx, environment);
  75. if (o != null) {
  76. BasicDataSource ds = (BasicDataSource) o;
  77. if (ds.getPassword() != null && ds.getPassword().length() > 0) {
  78. String pwd = MyPasswordUtilClass.unscramblePassword(ds.getPassword());
  79. ds.setPassword(pwd);
  80. }
  81. return ds;
  82. } else {
  83. return null;
  84. }
  85. }
  86. }
  87.  
  88. @Bean
  89. public DataSource dataSource() {
  90. DataSource ds = null;
  91. JndiTemplate jndi = new JndiTemplate();
  92. try {
  93. ds = jndi.lookup("java:comp/env/jdbc/myDataSource", DataSource.class);
  94. } catch (NamingException e) {
  95. log.error("NamingException for java:comp/env/jdbc/myDataSource", e);
  96. }
  97. return ds;
  98. }
  99.  
  100. print(SHA1CryptoServiceProvider sHA1Hasher = new SHA1CryptoServiceProvider();
  101. ASCIIEncoding enc = new ASCIIEncoding();
  102.  
  103. byte[] arrbytHashValue = sHA1Hasher.ComputeHash(enc.GetBytes(clearTextPW));
  104. string HashData = System.BitConverter.ToString(arrbytHashValue);
  105. HashData = HashData.Replace("-", "");
  106. if (HashData == databaseHashedPassWO)
  107. {
  108. return true;
  109. }
  110. else
  111. {
  112. return false;
  113. });
Add Comment
Please, Sign In to add comment