Blog

  • Need: Properties file may contain sensitive information like password, emailed etc. If such information is defined in the properties file, anyone who has access to our code can know server location, email, password etc. 


    Solution:
    Set the properties variable

    bashrc

    file as follows: (such as values of property variables will not visible directly, rather saved in bashrc and retrieved from the same).

    1- Open bashrc file:
    vim  ~/.bashrc

    2- export the variables that need to be set into bashrc 
    export MAIL_HOST="hostName"
    export MAIL_USERNAME="
    emailAddress"
    export MAIL_PASSWORD="Password"

    3- Save the file

    4- Reflect the changes done in bashrc   
    source  ~/.bashrc

    5- Check from terminal whether variable is set to bashrc or not
    if [ -z "$MAIL_HOST" ]
    then
        echo "not defined"
    else 
        echo "defined"
    fi
           (or)
     
    echo $MAIL_HOST

    If MAIL_HOST is properly set to bashrc then the output will be defined else not defined or in another case respective mail host will be printed.

    6- Access the configuration set into bashrc to properties file: 

    spring.mail.host = ${MAIL_HOST}
    spring.mail.username =${MAIL_USERNAME}
    spring.mail.password =${MAIL_PASSWORD}
    spring.mail.properties.mail.smtp.auth = true
    spring.mail.properties.mail.smtp.socketFactory.port = 587
    spring.mail.properties.mail.smtp.starttls.enable = true
    spring.mail.properties.mail.smtp.socketFactory.fallback = false
    
    

     

     

    Benefits of approach:
    When we set the information in
    bashrc we do not require to set multiple profiles. Since different profile may use different configuration hence we can simply set the properties required for development in development bashrc, production in production's bashrc and so on..
    This approach reduces the code for setting multiple profiles. Moreover, this is a secure way to access sensitive information. Now if anyone has access to our code is not eligible to access our security details such as email and password. 


    Note: To reflect the changes done in bashrc sometime one may require to source the bashrc from the project location. So before running the application, it is required to source the bashrc from project location as follows: 

    source ~/.bashrc

     

     

     

Tags: Java