Skip to content

Commit

Permalink
Update customer api and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
czetsuya committed Jun 23, 2019
1 parent 41d9b6e commit 2d9fc3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Spring Project Secured with Keycloak

A demo project created to demonstrate how a Spring project can be secured using a Keycloak server via bearer token.

Requirements:
- Keycloak server 6.0.1
- https://github.com/czetsuya/keycloak-react

Note:
- Make sure that you have the same Keycloak client credentials value for the 2 project ands Keycloak server.

If keycloak.json file is to be used instead of application.yml, set the following system variable and make sure that you have the file keycloak.json in src/main/resources.

keycloak.configurationFile = classpath:keycloak.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.broodcamp.web.assembler.CustomerResourceAssembler;

@RestController
@RequestMapping(value = "/v1/customers")
//@RequestMapping("/v1/customers")
public class CustomerController {

@Autowired
Expand All @@ -41,9 +41,10 @@ public class CustomerController {
*
* @return list of customers
*/
@GetMapping
@GetMapping("/v1/customers")
public Resources<Resource<Customer>> all() {
List<Resource<Customer>> entities = repository.findAll().stream().map(assembler::toResource).collect(Collectors.toList());
List<Resource<Customer>> entities = repository.findAll().stream().map(assembler::toResource)
.collect(Collectors.toList());

return new Resources<>(entities, linkTo(methodOn(CustomerController.class).all()).withSelfRel());
}
Expand All @@ -57,15 +58,16 @@ public Resources<Resource<Customer>> all() {
* @return newly created customer
* @throws URISyntaxException
*/
@PostMapping(value = "/")
@PostMapping(value = "/v1/customers")
public ResponseEntity<Resource<Customer>> newCustomer(@RequestBody Customer newCustomer) throws URISyntaxException {

Resource<Customer> resource = assembler.toResource(repository.save(newCustomer));
return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
}

@PutMapping("/{id}")
public ResponseEntity<?> replaceCustomer(@RequestBody Customer newCustomer, @PathVariable Long id) throws URISyntaxException {
@PutMapping("/v1/customers/{id}")
public ResponseEntity<?> replaceCustomer(@RequestBody Customer newCustomer, @PathVariable Long id)
throws URISyntaxException {

Customer updatedEmployee = repository.findById(id).map(employee -> {
employee.setName(newCustomer.getName());
Expand All @@ -89,7 +91,7 @@ public ResponseEntity<?> replaceCustomer(@RequestBody Customer newCustomer, @Pat
* @param id
* @return
*/
@DeleteMapping(value = "/{id}")
@DeleteMapping(value = "/v1/customers/{id}")
public ResponseEntity<String> deleteCustomer(@PathVariable Long id) {
repository.deleteById(id);
return ResponseEntity.noContent().build();
Expand All @@ -101,7 +103,7 @@ public ResponseEntity<String> deleteCustomer(@PathVariable Long id) {
* @param id
* @return customer detail
*/
@GetMapping(value = "/{id}")
@GetMapping(value = "/v1/customers/{id}")
public Resource<Customer> one(@PathVariable Long id) {

Customer entity = repository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/keycloak.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"resource": "springboot-rest-api",
"verify-token-audience": true,
"credentials": {
"secret": "79573dbe-9c53-40ee-8612-5c431c8586e7"
"secret": "0c13ffa2-4499-4084-a275-c497653c9aa4"
},
"confidential-port": 0,
"principal-attribute": "preferred_username",
Expand Down

0 comments on commit 2d9fc3a

Please sign in to comment.