Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricston-git/aafc713400a5c8ffa997 to your computer and use it in GitHub Desktop.
Save ricston-git/aafc713400a5c8ffa997 to your computer and use it in GitHub Desktop.
Encrypting properties in Spring Boot with jasypt-spring-boot
@SpringBootApplication
public class App {
private static final Logger LOG = LoggerFactory.getLogger(App.class);
@Value("${db.driverclassname}")
private String dbDriverClassName;
@Value("${db.url}")
private String dbUrl;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(dbDriverClassName);
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
LOG.info("dataSource url: " + dataSource.getUrl());
return dataSource;
}
@Bean
public ContactRepository msgRepo() {
return new MySQLContactRepository();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
db.driverclassname=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/contactsdb
db.username=contactsuser
db.password=ENC(XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx+hNPrJyQT88=
public interface ContactRepository {
public List<Contact> find(Integer offset, Integer rowCount);
}
@RestController
@RequestMapping(value = "/contacts")
public class ContactsController {
@Autowired
ContactRepository customerRepo;
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public List<Contact> contacts(@RequestParam(value = "offset", required = false) Integer offset,
@RequestParam(value = "rowCount", required = false) Integer rowCount) {
return customerRepo.find(offset, rowCount);
}
}
create database contactsdb;
mysql -u root -p contactsdb < db/schema.sql
mysql -u root -p contactsdb < db/data.sql
create user 'contactsuser'@'localhost' identified by 'contactspassword';
grant all privileges on contactsdb.* to 'contactsuser'@'localhost';
encrypted-pwd$ java -cp ~/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="contactspassword" password=supersecretz algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 24.45-b08
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: contactspassword
password: supersecretz
----OUTPUT----------------------
XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx+hNPrJyQT88=
public class MySQLContactRepository implements ContactRepository {
protected NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
@Override
public List<Contact> find(Integer offset, Integer rowCount) {
offset = offset == null ? 0 : offset;
rowCount = rowCount == null ? 10 : rowCount;
final String SQL = "SELECT * FROM Contacts LIMIT :offset,:rowCount";
return jdbcTemplate.query(SQL, new MapSqlParameterSource("offset", offset).addValue("rowCount", rowCount), (rs, n) -> {
Contact contact = new Contact();
contact.setId(rs.getLong("id"));
contact.setName(rs.getString("name"));
contact.setEmail(rs.getString("email"));
contact.setCompany(rs.getString("company"));
return contact;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment