Sunday, 7 December 2014

Security for Microservices with Spring and OAuth2

To use Spring the easiest way is to use "Spring-Boot".

Simple Service with "Groovy". lets call it "app.groovy":


@Grab('spring-boot-starter-security')
@RestController
class Application {
 @RequestMapping("/")
 def home() {
 [status: 'OK']
 }
}

You can run the service by:

spring run app.groovy

and call the service with:

curl -v localhost:8080

Well, we got an HTTP response status of "401 Unauthorized" and a header of "WWW-Authenticate: Basic realm="Spring""

We also got a JSON response that say we are unauthrized.

So what are the user/password we need (remember the header response of Basic authentication?)?

The default user is "user" and the default password is in the log file when we invoked spring boot.

So we will now call the service like this:

curl user:@localhost:8080

and we got the following response:

{status: 'OK'}

You can use Spring Boot with a certificate



No comments:

Post a Comment