Slim modular Java 9 runtime Docker image with Alpine Linux

With the release of Java 9, and the introduction of Project Jigsaw (the Java Platform Module System), we no longer have the need for a full-blown JRE to run our Java applications. It is now possible to construct a stripped-down Java Runtime, containing the minimum set of required modules. This allows us to create slim Docker containers without excess baggage.

The source code belonging to this blog post can be found at: https://github.com/rlippolis/java9-runtime-image

Continue reading

Serverless Java with AWS Lambda: Introduction

Just as we are over the crest of the microservice hype and can finally see how this architectural tool might (or might not) solve our problems the next hype is already here: serverless programming! In this first blog post I’m going to explain what serverless is, what it isn’t, and how it can change the way we create software. In the next posts I’m going to show a few simple examples using a well known ‘serverless’ platform: AWS Lambda.

Originally posted here.

Continue reading

Run one or Exclude one test with Gradle

From time to time you only want to run one test, one test method, one class or one package from the command-line.

Or on the contrary: you want to exclude / ignore one specific test or group of tests during the build cycle.

Excluding tests from the build cycle by the command line usually occurs when the following scenarios meet:

  • A test requires significant amount of resources (time, memory, disk space, etc.)
  • The run needs to be independent from the IDE (to reenact the Continuous Integration / Continuous Delivery pipeline) as some IDEs load test-dependencies on the compile-time class-path.
  • You have no or limited ability to change the code-base

Continue reading

Run one or Exclude one test with Maven

From time to time you only want to run one test, one test method, one class or one package from the command-line.

Or on the contrary: you want to exclude / ignore one specific test or group of tests during the build cycle.

Excluding tests from the build cycle by the command line usually occurs when the following scenarios meet:

  • A test requires significant amount of resources (time, memory, disk space, etc.)
  • The run needs to be independent from the IDE (reenact the Continuous Integration / Continuous Delivery pipeline) as some IDEs load test-dependencies on the compile-time class-path.
  • You have no or limited ability to change the code-base
    Continue reading

Spicy Spring : Scheduler does not shutdown

On my current project we use Java 8, Spring 4.3 and Tomcat 7.0 as application server. After the scheduling functionality was added to the project the application server did not shut down any more, it hung till the end of time.

I like to use the default Java implementations when possible so the configured scheduler was the java.util.concurrent.ScheduledThreadPoolExecutor.
Continue reading

Correlate your services logging with Spring Boot

In a modern service landscape, especially when using containers, you are probably using something like the ELK stack (Elasticsearch, Logstash, Kibana) to flow all the logging into.
But how to find from all those loglines what caused the nasty bug after a innocent buttonpress?
One of the easy answers to this is what’s called a correlation id – basically a unique number assigned to that buttonpress which gets carried around between the services and added to every logline.
Sounds good you say? it is so let’s see how to do this.
Continue reading

Ratpacked: Add Ratpack To Spring Boot Application

In a previous post we saw how we can use Spring Boot in a Ratpack application. But the integration can also be the other way around: using Ratpack in a Spring Boot application. This way we can use Ratpack’s power to handle requests sent to our Spring Boot application and still use all Spring Boot features in our application. The easiest way to add Ratpack to a Spring Boot application is adding a Ratpack dependency and use the @EnableRatpack annotation. With this annotation a RatpackServer instance is created and started along with configuration options.

Continue reading

Handling YAML format in your REST with Spring Boot

In general a REST service serves JSON document formats. In Spring Boot it is even the standard without having to configure anything.
Over HTTP we have the ability to send a Content-Type as header to instruct the server to return a certain document format (Mime type).
Besides handling JSON, XML is another common document format. But what if we want to serve or read a YAML document format?

This tutorial provides the required steps to be able to handle objects in YAML format. To be able to send a YAML Content-Type with the header, and to be able to serialize and deserialize with the existing ObjectMappers of Jackson. This tutorial will use Spring Boot, Jackson and a YAML dataformat extension library for Jackson.

Continue reading

Securing your application landscape with Spring Cloud Security – Part 1

Securing an application is difficult. Securing an entire application landscape is even more difficult! In this modern era of blazing fast microservices we do not want the additional complexity of having to secure it all manually. This is where Spring Cloud Security comes in. By combining proven technologies, it helps us achieve performant, configurable end-to-end security across multiple applications.

So what technologies are being combined? Well, a lot… We will not mention them all here, but the foundation relies on Spring Boot and Spring Security OAuth.

OAuth, or, in our case, OAuth2 is basically an authorization delegation protocol. To quote Wikipedia, OAuth:

[…] specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials.

In our context, the resource owners are the users of our applications, server resources are the data we store about our users (e.g. the list of shop orders), and third-parties are the applications (or services) in our landscape which perform actions on the user data.

A common mistake people make is thinking that OAuth also provides authentication. It doesn’t, but that’s where Spring Security kicks in. For more information about the difference between authentication and authorization in an OAuth context, we refer you to this article.

Basically, the picture below visualizes the type of application landscape we want to achieve with this series of articles.

Overview

An overview of the type of application landscape we want to achieve

In the following series of articles, we will build a concrete implementation of the above displayed application landscape from the ground up, with as little code and configuration as possible. We will make some concessions regarding complexity and functionality, mainly to enable us to focus on the basic principles.

The project source code is available on Bitbucket, so feel free to clone the Git repository:

The master branch contains the complete solution. Each article in this series will represent a separate step in the development of the solution. Several Git tags have been created in the repository to be able to follow along with each step.

How to run it

To be able to run the applications, the following software is required:

  • Gradle (tested with version 2.12)
  • Java 8 Development Kit

All three applications in the Git repository can be run from the command line, by executing the following command from the root directory of the application to run:

Alternatively, it is possible to run the main method in the relevant Spring Boot Application class (MyAuthServerApplication / MyWebsiteApplication / MyRestApiApplication) from an IDE or command line.

It does not matter in which order the applications are started.

Securing a basic website with a separate Identity Provider

As a starting point, we have created a Spring Boot application, called My Website. The application is bootstrapped using Spring Initializr, with the Web and Thymeleaf dependencies.

The application is pretty straightforward, containing a single Spring MVC Controller, to render a web page which displays the current time (like mentioned before, the functionality of the application is irrelevant, so we keep this simple). For now, the application is not secured in any way.

Tip To see the application in its initial state (not secured), checkout the Git tag insecure-website from the repository. To see the end result of this step (the website is secured using an Identity Provider), checkout the tag secure-website.

To view the current time, run the My Website application (see How to run it), and navigate to: http://localhost:8080/time

Creating the Identity Provider

The first step in securing My Website involves creating My Auth Server. This application will function as an Identity Provider for My Website. To achieve this, we will be using Spring Cloud Security, in combination with Spring Security OAuth2.

In OAuth2, the role of Identity Provider is split in two parts. The first part, the Authorization Server, verifies the identity of a resource owner (the user) and provides some kind of access token to applications wanting to act as the user. The second part, the Resource Server, provides resources (e.g. information about the user), based on the given token.

Important The OAuth2 specification doesn’t state anything about authentication. It basically defines how to delegate authorizations to access protected resources to third parties. In our context: the Authorization Server tells our application(s) that it may act as some user (resource owner), and thus access their data (resources). So how does the Authorization Server know which user it concerns? That’s where Spring Security enters the picture. Spring Security will handle the authentication part, and Spring Security OAuth2 will do the rest. For more general information about user authentication with OAuth2, see here.

With Spring Security OAuth2 it is possible to create two separate applications, one acting as Authorization Server and one as Resource Server. But it is also possible to combine the two parts into one application, which is what we will be doing.

Again, we will use Spring Initializr to bootstrap the application, with the Web and Thymeleaf dependencies, but this time we also add the Cloud OAuth2 dependency. We configure the application to run at http://localhost:9090/auth (see the properties in my-auth-server/src/main/resources/application.yml).

My Auth Server application is configured as both an Authorization Server and Resource Server, and is secured with Spring Security, with the help of a few Spring configurer adapters. These are explained below.

Authorization Server configuration

The AuthorizationServerConfigurer allows you to modify the default configuration (enabled by the annotation @EnableAuthorizationServer) to be able to act as an OAuth2 Authorization Server. Furthermore, we configure the client details service. This service specifies details about the way ‘clients’ (== other applications) interact with the Authorization Server.

  1. We configure everything in-memory for simplicity, but it is also possible to get the details from e.g. a database
  2. The client authenticates with the credentials myauthserver / verysecretpassword
  3. Specify the allowed redirect URI patterns, this prevents random parties from gaining an access token (e.g. XSRF attacks)
  4. OAuth2 supports different grant types, of which the Authorization Code type is the most known. For more information about grant types, see here
  5. When a client (application) authorizes with the Authorization Server, the server can assign one or more scopes to the client. In simple terms, a client scope can be seen as a specific permission for this client. With client scopes, it is possible to allow some applications to have more access rights to user resources than other applications. This is out of scope (pun intended) for this article.
  6. It is possible to make the Authorization Server ask the user permission for the client to obtain rights to access their resources. In our case we automatically approve the request.

Resource Server configuration

An application acting as an OAuth2 client to My Auth Server may need to be able to fetch information about a user, given a valid OAuth2 access token. To serve this information, we add a REST Controller, and secure this resource by configuring the My Auth Server as an OAuth2 Resource Server.

The REST Controller is a standard Spring @RestController, with one request mapping:

  1. The currently logged in user is injected by Spring as an OAuth2Authentication object. This is basically a standard Java Principal, with some added OAuth2-specific details.
  2. We return a map containing the current username,
  3. and a set of user roles.

This is the minimal set of details required for a Spring-enabled OAuth2 client to reconstruct its own OAuth2 Principal object. It is possible to add more details here, if needed. Instead of a Map, some examples on the internet just return the Principal object directly. Although this saves you the overhead of creating a map, we feel it exposes too much unnecessary information. With our approach, we are in control of what information is shared about the user.

The next step is to secure this resource. Of course, we can only provide valid user information, if we have a logged in user. Therefore, we configure My Auth Server as an OAuth2 Resource Server, which basically is a server hosting protected resources, capable of serving these resources when presented with a valid access token. In this case, the protected resource is information about the logged in user. The Resource Server security configuration is defined in ResourceServerConfigurer, using the @EnableResourceServer annotation.

  1. We match our security configuration on the URL /user, matching the request mapping in our ResourceController.
  2. Only authenticated access is allowed.

Spring Security configuration

The last step for My Auth Server is to configure the application security, to define how we want our users to authenticate themselves. We use a pretty standard Spring Security setup, but for the sake of completeness, we describe the configuration below.

The WebSecurityConfigurer modifies the default Spring Security configuration (enabled by the annotation @EnableWebSecurity annotation). We configure the URLs to secure, enable Cross-Site Request Forgery protection, setup a login form, and add some user credentials.

  1. The login page is located at /login, and should be publicly available.
  2. All other requests should be authenticated.
  3. Enable Cross-Site Request Forgery (CSRF) protection.
  4. Configure the form login page at /login.
  5. For simplicity, we use an in-memory user database with two users: a normal user and an administrator.

The login form is a simple html form, generated using a Thymeleaf template. The template is located at my-auth-server/src/main/resources/templates/login.html, and made available using a Spring MVC View Controller, defined in my-auth-server/src/main/java/com/jdriven/example/cloudsecurity/config/WebMvcConfigurer.java.

Using the Identity Provider to secure My Website

Now that we have set up our Identity Provider (My Auth Server), we need to configure My Website to use it. This is a very easy task, consisting of three parts.

Spring Cloud Starter OAuth2

We add a dependency in the My Website project to Spring Cloud Starter OAuth2.

Spring Security Configuration

We configure the security in WebSecurityConfigurer (which is a Spring Web Security Configurer Adapter), using the @EnableOAuth2Sso annotation to enable OAuth2. The security configuration itself is pretty straightforward.

  1. Allow navigating to the index page at /.
  2. Secure all the other URLs.

Application configuration

The last step is to add some properties to our application configuration in application.yml. These properties configure My Website to use the URLs provided by My Auth Server.

  1. The URL used by an OAuth2 client to acquire an OAuth2 access token.
  2. The URL to which the user will be redirected to authorize access to a resource.
  3. The client ID specified earlier in the Authorization Server configuration.
  4. The client password specified earlier in the Authorization Server configuration.
  5. The user info resource URL, specified earlier in the Resource Server configuration.
Tip To see the resulting source code at this point, checkout the Git tag secure-website.

That’s it! If we run both the My Auth Server application and the My Website application (see How to run it), and navigate to http://localhost:8080/time, we will see that the URL is now secured, and we need to login first. Congratulations, your website is now secure!

Up next

In the next articles in this series, we will discuss the following topics:

  • After having secured a single application, we will add a new application, a REST service, which we will also secure through My Auth Server in a Single Sign On (SSO) configuration.
  • We will call the REST service from the browser using client side Javascript, proxying the service calls through My Website using Netflix Zuul.
  • Then, we will make the REST service stateless (no sessions), remaining secure by using JSON Web Tokens (JWT).
  • Finally, we will add the ability to login using a Google account.