Hands-On Spring Security 5 for Reactive Applications
eBook - ePub

Hands-On Spring Security 5 for Reactive Applications

Learn effective ways to secure your applications with Spring and Spring WebFlux

  1. 268 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Spring Security 5 for Reactive Applications

Learn effective ways to secure your applications with Spring and Spring WebFlux

About this book

Secure your Java applications by integrating the Spring Security framework in your code

Key Features

  • Provide authentication, authorization and other security features for Java applications.
  • Learn how to secure microservices, cloud, and serverless applications easily
  • Understand the code behind the implementation of various security features

Book Description

Security is one of the most vital concerns for any organization. The complexity of an application is compounded when you need to integrate security with existing code, new technology, and other frameworks. This book will show you how to effectively write Java code that is robust and easy to maintain.

Hands-On Spring Security 5 for Reactive Applications starts with the essential concepts of reactive programming, Spring Framework, and Spring Security. You will then learn about a variety of authentication mechanisms and how to integrate them easily with the Spring MVC application. You will also understand how to achieve authorization in a Spring WebFlux application using Spring Security.You will be able to explore the security confgurations required to achieve OAuth2 for securing REST APIs and integrate security in microservices and serverless applications. This book will guide you in integrating add-ons that will add value to any Spring Security module.

By the end of the book, you will be proficient at integrating Spring Security in your Java applications

What you will learn

  • Understand how Spring Framework and Reactive application programming are connected
  • Implement easy security confgurations with Spring Security expressions
  • Discover the relationship between OAuth2 and OpenID Connect
  • Secure microservices and serverless applications with Spring
  • Integrate add-ons, such as HDIV, Crypto Module, and CORS support
  • Apply Spring Security 5 features to enhance your Java reactive applications

Who this book is for

If you are a Java developer who wants to improve application security, then this book is for you. A basic understanding of Spring, Spring Security framework, and reactive applications is required to make the most of the book.

Trusted by 375,005 students

Access to over 1.5 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Year
2018
Print ISBN
9781788995979
Edition
1
eBook ISBN
9781788990073

REST API Security

Spring Security can be used to secure REST APIs. This chapter begins with the introduction of some of the important concepts in regard to REST and the JWT.
The chapter then introduces OAuth concepts and by using hands-on coding examples, explains simple and advanced REST API security utilizing the Spring Security and Spring Boot modules in the Spring Framework.
We will be using the OAuth protocol in our examples to secure exposed REST API's utilizing Spring Security features to the fullest. We will be using the JWT to exchange claims between the server and client.
In this chapter, we will cover the following concepts:
  • Modern application architecture
  • Reactive REST API
  • Simple REST API security
  • Advanced REST API security
  • Spring Security OAuth project
  • OAuth2 and Spring WebFlux
  • Spring Boot and OAuth2

Important concepts

Before getting into coding, we need to be conversant with some important concepts. This section is aimed at introducing you to some of these concepts in detail.

REST

Representational State Transfer (REST) is an architectural style presented by Roy Fielding in the year 2000 for developing web services. It is built on top of the well-known Hypertext Transfer Protocol (HTTP) and can transfer data in multiple formats, the most common being JavaScript Object Notation (JSON) and eXtensible Markup Language (XML). The status of a request in REST is indicated using standard HTTP status code (200: OK, 404: Page not found!, and so on). Being based on HTTP, security is taken care of using the already familiar Secure Sockets Layer (SSL) and Transport Layer Security (TLS).
While writing such web services, you are free to use any programming language (Java, .NET, and so on) that is capable of making web requests based on HTTP (which is a de facto standard that every language supports). You have a number of well-known frameworks, using which developing RESTful APIs on the server side is quite easy and simple. Also, on the client side, there are a number of frameworks that make invoking RESTful APIs and handling responses straightforward and easy.
Since REST works on internet protocol, the caching of a web service response can be achieved quite easily by supplying appropriate HTTP headers (Cache-Control, Expires, and so on). The HTTP methods PUT and DELETE are not cacheable in any scenario. The following table summarizes the use of HTTP methods:
HTTP method
Description
GET
Retrieves a resource
POST
Creates a new resource
PUT
Updates an existing resource
DELETE
Deletes an existing resource
PATCH
Makes a partial update to a resource
Table 1: HTTP method usage
A REST API request/response (data sent over the wire) can be compressed by specifying appropriate HTTP headers, similar to caching. The HTTP header, Accept-Encoding, is sent by the client to the server, to let the server know the compression algorithms it can understand. The server successfully compresses a response and puts out another HTTP header, Content-Encoding, letting the client know which algorithm has to be used to decompress.

JSON Web Token (JWT)

"JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties."
- https://jwt.io/
In the past, the stateless nature of HTTP was circumvented in a web application (most of them are stateful in nature) by associating each request with a session ID created on the server and then stored by the client using cookies. Each request sends the cookie (session ID) in the form of an HTTP header, which gets validated by the server, and a state (a user session) is associated with each request. In modern applications (we will cover this in a bit more detail in the next section), a server-side session ID is replaced with the JWT. The following diagram shows the workings of the JWT:
Figure 1: Workings of the JWT in modern applications
The web server, in this case, doesn't create a user session and the user session management capability needed for a stateful application is offloaded to other mechanisms.
In the world of the Spring Framework, the Spring Session module can be employed to externalize the session from the web server to a central persistence store (Redis, Couchbase, and so on). Every request containing a valid token (JWT) is validated against this external store of authenticity and validity. After successful authentication, applications can generate a valid token and send it as a response to the client. The client can then store this token in any client storage mechanism it uses (sessionStorage, localStorage, cookies, and so on, in a browser). Using Spring Security, we can validate this token to ascertain the authenticity and validity of the user and then do whatever is required. We have a dedicated example in a subsequent section (Simple REST API security) of this chapter, which uses a basic authentication mechanism and, if successful, creates the JWT. Subsequent requests use the token in the HTTP header, which gets validated on the server to give access to other secured resources.
The following points highlight some of the advantages of using the JWT:
  • Better performance: Each request, when reaching the server, has to check the authenticity of the token send. The authenticity of the JWT can be checked locally and doesn't require an external call (say, to a database). This local validation is performant and reduces the overall response time for a request.
  • Simplicity: JWT is easy and simple to implement. Also, it is a well established format in the industry for tokens. There are a number of well-known libraries which can be used to easily work with the JWT.

Structure of a token

Unlike common security mechanisms, such as encryption, obscuring, and hiding, the JWT doesn't encrypt or hide the data contained within. But, it does the destination system to check whether the token is from an authentic source. The structure of the JWT consists of a header, payload, and a signature. As mentioned, rather than encryption, the data contained within the JWT is encoded and then signed. Encoding does the job of transforming the data in a way that is acceptable by a variety of parties and signing allows us to check for its authenticity and, in fact, its origin:
JWT = header.payload.signature
Let's go into more detail about each of the components constituting the token.

Header

This is a JSON object and takes the following format. It gives information on how the signature should be computed:
{
"alg": "HS256",
"typ": "JWT"...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Overview of Spring 5 and Spring Security 5
  8. Deep Diving into Spring Security
  9. Authentication Using SAML, LDAP, and OAuth/OIDC
  10. Authentication Using CAS and JAAS
  11. Integrating with Spring WebFlux
  12. REST API Security
  13. Spring Security Add-Ons
  14. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.5M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1.5 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Spring Security 5 for Reactive Applications by Tomcy John in PDF and/or ePUB format, as well as other popular books in Computer Science & Cyber Security. We have over 1.5 million books available in our catalogue for you to explore.