Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As said before encrypting passwords is just moving the problem somewhere else.</p> <p>Anyway, it's quite simple. Just write a class with static fields for your secret key and so on, and static methods to encrypt, decrypt your passwords. Encrypt your password in Tomcat's configuration file (<code>server.xml</code> or <code>yourapp.xml</code>...) using this class.</p> <p>And to decrypt the password "on the fly" in Tomcat, extend the DBCP's <code>BasicDataSourceFactory</code> and use this factory in your resource.</p> <p>It will look like:</p> <pre class="lang-xml prettyprint-override"><code> &lt;Resource name="jdbc/myDataSource" auth="Container" type="javax.sql.DataSource" username="user" password="encryptedpassword" driverClassName="driverClass" factory="mypackage.MyCustomBasicDataSourceFactory" url="jdbc:blabla://..."/&gt; </code></pre> <p>And for the custom factory:</p> <pre class="lang-java prettyprint-override"><code>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 &amp;&amp; ds.getPassword().length() &gt; 0) { String pwd = MyPasswordUtilClass.unscramblePassword(ds.getPassword()); ds.setPassword(pwd); } return ds; } else { return null; } } </code></pre> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload