Question: is my implementation secure (by API Key standards) OR as secure as using Spring Boot Security
?
I have produced a Spring Boot
API, but rather than using Spring Boot
Security to implement Api Key security, I have written my own API key implementation. The API Key is passed as a @RequestHeader
in each 'secured' request (see /booking/cancel
below).
Controller:
@RequestMapping(value = "/booking/cancel",
consumes = { "application/json" },
method = RequestMethod.POST)
public ResponseEntity<Void> cancelOrder(@RequestBody Cancellation cancellation,
@RequestHeader String apiKey) {
if(apiKey == null) {
return new ResponseEntity<Void>(HttpStatus.NOT_ACCEPTABLE);
}
long bookingProviderId;
try {
bookingProviderId = bookingService.getIdFromApiKey(apiKey);
if (bookingProviderId < 0) {
return new ResponseEntity<Void>(HttpStatus.NOT_ACCEPTABLE);
}
} catch (ApplicationException e) {
e.printStackTrace();
return new ResponseEntity<Void>(HttpStatus.INTERNAL_SERVER_ERROR);
}
//More code here...
}
Service layer:
The getIdFromApiKey
function exists in my service layer and calls the Dao object. It returns a long (Id) which I can subsequently use to manage access in the controller (e.g. prevent a user from cancelling someone else's order).
public long getIdFromApiKey(String apiKey) throws ApplicationException {
return apiKeyDao.selectId(apiKey);
}
Dao Layer:
public long getApiKey (String apiKey) throws DataAccessException {
BookingProvider bp = jdbcTemplate.queryForObject("SELECT * FROM BookingProvider WHERE apiKey = ?", BeanPropertyRowMapper.newInstance(BookingProvider.class), apiKey);
if(bp == null)
return -1;
else
return bp.getId();
}