creating restful api’s with grails and spring security

33
Creating RESTful API’s with Grails and Spring Security Álvaro Sánchez-Mariscal Web Architect odobo @alvaro_sanchez

Upload: alvaro-sanchez-mariscal

Post on 27-Aug-2014

3.873 views

Category:

Software


4 download

DESCRIPTION

In this talk I will cover how to create a REST API using Grails 2.3 to support single-page applications, exploring all the possible alternatives. Code is available at https://github.com/alvarosanchez/restful-grails-springsecurity-greach2014 I will also explain how to integrate Spring Security using the spring-security-rest plugin I recently created, to implement a stateless, token-based, RESTful authentication.

TRANSCRIPT

Page 1: Creating RESTful API’s with Grails and Spring Security

Creating RESTful API’s with Grails and Spring

SecurityÁlvaro Sánchez-Mariscal

Web Architect – odobo !

@alvaro_sanchez

Page 2: Creating RESTful API’s with Grails and Spring Security

About me

• Passionate software developer.

• Founded Salenda in 2005.

• Co-founded Escuela de Groovy in 2009.

• Groovy/Grails lover since 2007.

• Working now at Odobo as Web Architect.

Page 3: Creating RESTful API’s with Grails and Spring Security

• HTML5 games platform for:

• Game developers.

• Casinos.

• Check out https://play.odobo.com and try for free!

Page 4: Creating RESTful API’s with Grails and Spring Security

Different approaches

• Using just @Resource.

• With uri attribute.

• With explicit UrlMappings.

Page 5: Creating RESTful API’s with Grails and Spring Security

Demostep1 … step2

Page 6: Creating RESTful API’s with Grails and Spring Security

Different approaches

• Creating explicitly a controller and extending RestfulController.

• Defining just the constructor.

• Implementing actions based on the URL mappings report.

Page 7: Creating RESTful API’s with Grails and Spring Security

Demostep3 … step4

Page 8: Creating RESTful API’s with Grails and Spring Security

Different approaches

• Scaffolding (but don’t tell your mother).

Page 9: Creating RESTful API’s with Grails and Spring Security

Customizing response

• Customize default renderers.

• Register custom marshallers.

• Use Hypermedia (and fasten your seat belts!).

• Use Dan Wood’s rest-renderers plugin.

Page 10: Creating RESTful API’s with Grails and Spring Security

Demostep5 … step7

Page 11: Creating RESTful API’s with Grails and Spring Security

Adding Spring Security

Motivation: we need to break down the traditional, monolithic Grails applications, in 2 different apps:

1. A pure HTML5/Javascript frontend.

2. A mere RESTful Grails backend.

Page 12: Creating RESTful API’s with Grails and Spring Security

Adding Spring Security

Issue: The existing Spring Security plugins would not work with a RESTful, browser-

based client.

Page 13: Creating RESTful API’s with Grails and Spring Security

REST is much more than just

returning JSON.

Page 14: Creating RESTful API’s with Grails and Spring Security

RESTful is about*

Client / server.

Stateless.

Cacheable.

Layered.

* Source: Wikipedia.

Page 15: Creating RESTful API’s with Grails and Spring Security

Meet Spring Security REST

A stateless, token-based authentication for your

RESTful API’s

Page 16: Creating RESTful API’s with Grails and Spring Security

Authentication

Page 17: Creating RESTful API’s with Grails and Spring Security

Demo

Page 18: Creating RESTful API’s with Grails and Spring Security

Invoking a protected resource

Page 19: Creating RESTful API’s with Grails and Spring Security

Demo

Page 20: Creating RESTful API’s with Grails and Spring Security

Authentication Endpoint

• Uses the default authenticationManager bean, which in turn uses all the registered authentication providers.

• Receives username and password, and generates a customizable JSON response.

Page 21: Creating RESTful API’s with Grails and Spring Security

Authentication Endpoint

• Credentials can be extracted from:

1. Request parameters.

2. A JSON payload.

3. Any custom implementation

Page 22: Creating RESTful API’s with Grails and Spring Security

Token Generation

• 2 strategies out-of-the-box:

1. Using java.security.SecureRandom (default).

2. Using java.util.UUID.

• A custom implementation can be plugged.

Page 23: Creating RESTful API’s with Grails and Spring Security

Token Storage

• In Memcached (default).

• Using GORM.

• Write your own.

Page 24: Creating RESTful API’s with Grails and Spring Security

Token Storage

Page 25: Creating RESTful API’s with Grails and Spring Security

Token Validation

• If the token header (X-Auth-Token by default) is present, the request will be validated.

• Otherwise, the plugin won’t participate in the filter chain.

Page 26: Creating RESTful API’s with Grails and Spring Security

Token Validation

• If the passed token exists on the token storage, the principal will be stored on the security context.

• It can be retrieved using springSecurityService.principal

Page 27: Creating RESTful API’s with Grails and Spring Security

CORS support

• Grails doesn’t support CORS (vote for GRAILS-10914).

• This plugin comes prepackaged with cors plugin.

Page 28: Creating RESTful API’s with Grails and Spring Security

Demo

Page 29: Creating RESTful API’s with Grails and Spring Security

OAuth support

Page 30: Creating RESTful API’s with Grails and Spring Security

OAuth support

Page 31: Creating RESTful API’s with Grails and Spring Security

Demo

Page 32: Creating RESTful API’s with Grails and Spring Security

DevQA: make your testers happier with

Groovy, Spock and Geb

Tomorrow, 17:15

Page 33: Creating RESTful API’s with Grails and Spring Security

Thanks!Álvaro Sánchez-Mariscal

Web Architect – odobooo !

@alvaro_sanchez alvarosanchez