Friday, December 20, 2013

Create a KeyStore to save Secret Key

Secret key is used to encrypt and decrypt the strings like "password" and other secret information. KeyStore is the most secure place to keep secret key. 

Here are the main steps to create new key store and saving a secret key:
  • Get instance of KeyStore using KeyStore.getInstance method. It takes the name of the keystore
  • Once instance of KeyStore is available, load it as empty keyStore
  • Generate the secretKey and save it to keystore for future purposes.
    private final String KEYSTORE_TYPE = "JCEKS";
    private final String KEYSTORE_NAME = "CareKeyStore";
    private final String SECRET_KEY_NAME = "secretKeyAlias";
    private final String KEY_ALGO = "AES";

    private String final pwd ="mypass";

    KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
    ks.load (null,pwd.toCharArray());
    KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGO);
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();
    KeyStore.ProtectionParameter protParam =
                            new KeyStore.PasswordProtection(pwd.toCharArray());
    KeyStore.SecretKeyEntry skEntry =
                            new KeyStore.SecretKeyEntry(secretKey);
    ks.setEntry(SECRET_KEY_NAME, skEntry, protParam);
    java.io.FileOutputStream fos = null;
    try {
           fos = new java.io.FileOutputStream(KEYSTORE_NAME);
           ks.store(fos, pwd.toCharArray());
                       
     }catch(Exception ex){
           logger.error(null,ex);
                       
      }finally {
            if (fos != null) {
                  fos.close();
            }
      }






          

Thursday, July 4, 2013

JPA Reference Card

Collection of basic types



Tuesday, April 9, 2013

JSF 2 Tips & Techniques - JSF Messages, Resource Bundle, and Validation Messages

Overriding default JSF error / warning Messages

Message bundle is used to override jsf's default error / warning messages. Declare message-bundle in your faces-config.xml and write new messages for default keys. You can define list of JSF defined messages from JSF specification under "Localized Application Messages"

Resource Bundle

Localized Resource bundle is defined with resource-bundle tag inside faces-config at global level or it is defined at the page level with loadBundle tag. Define resource bundle properties file in src/main/resources folder (You use any pacakge hierarchy e.g. if you want to define your resource bundle something like com.abc.view.ResourceBundle.properties then you needed to define corresponding folder hierarchy in your resource folder which will be src/main/resources/com/abc/view/ResourceBundle.properties. 

After defining your resource bundle, register it either on the page through f:loadBundle tag or globally in faces-config file using resource-bundle tag. 

Then Use it in your page like #{rb['app.title']}

Validation Messages using Bean Validation API

If you want to override default messages of Bean validation api or want to introduce new messages, then create ValidationMessages.properties file at root level i.e. at src/main/resources folder.