Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Creating applications with Grails, Angular JS and Spring Security - G3 Summit 2016

877 views

Published on

Grails 3.1 enhances and improves the profile system introduced in Grails 3.0. One of the new profiles ease the creation and development of applications where the the front-end is an Angular JS application, and the backend is a Grails REST API.

In this session, Álvaro (member of the Grails team at OCI, Grails committer and author of several plugins) will demonstrate how to use the Angular profile to create an application using Grails 3.1, Angular JS and Spring Security REST.

Published in: Software
  • Be the first to comment

Creating applications with Grails, Angular JS and Spring Security - G3 Summit 2016

  1. 1. Creating applications with Grails, Angular JS and Spring Security Álvaro Sánchez-Mariscal
  2. 2. Álvaro Sánchez-Mariscal Software Engineer Grails Development Team sanchezmariscala@ociweb.com
  3. 3. OCI is the new home of Grails More at ociweb.com/grails
  4. 4. The workshop bit.ly/grails-angular-workshop
  5. 5. Creating REST API’s with Grails
  6. 6. The REST Profile • Targeted at building REST applications. • REST Specific plugins and commands. • No GSP, asset pipeline, UI plugins. • JSON / Markup views instead.
  7. 7. The REST Profile • Profile specific commands: • create-domain-resource - creates an @Resource domain • create-restful-controller - creates a RestfulController
  8. 8. The REST Profile • Statically compiled, extensible JSON views: json.person { name "bob" } {"person":{"name":"bob"}}
  9. 9. Create the project $ grails create-app -profile rest-api -features hibernate5,json-views todo | Application created at /tmp/todo
  10. 10. Create a domain resource $ grails create-domain-resource todo | Created grails-app/domain/todo/Todo.groovy | Created src/test/groovy/todo/TodoSpec.groovy
  11. 11. REST Domain class package com.example
 
 import grails.rest.Resource
 
 @Resource(uri = '/todos')
 class Todo {
 
 String description
 boolean completed
 
 }
  12. 12. Create a restful controller $ grails create-restful-controller todo.Todo | Created grails-app/controllers/todo/TodoController.groovy
  13. 13. RESTful Controller package com.example
 
 import grails.rest.RestfulController
 
 class TodoController extends RestfulController {
 
 static responseFormats = ['json']
 
 TodoController() {
 super(Todo)
 }
 
 def pending() {
 respond Todo.findAllByCompleted(false), view: 'index'
 }
 }
  14. 14. JSON View import com.example.Todo
 
 model {
 Todo todo
 }
 
 json {
 hal.links(todo)
 id todo.id
 description todo.description
 completed todo.completed
 }
  15. 15. Demo - REST API
  16. 16. Working with the Angular JS profile
  17. 17. The Angular JS Profile • Extends the REST profile. • Adds project setup for AngularJS. • Code generation for AngularJS. • Scaffolding available via plugin.
  18. 18. The Angular JS Profile • Profile specific commands: • create-ng-controller • create-ng-service • create-ng-domain • create-ng-directive • create-ng-component • create-ng-module
  19. 19. Create the project $ grails create-app -profile angular -features hibernate5,json-views todo | Application created at /tmp/todo
  20. 20. Scaffold all the things! $ grails ng-generate-all todo.Todo
  21. 21. Demo - AngularJS scaffolding
  22. 22. The Angular 2 Profile • Available since Grails 3.2.1. • Multi-project build: • Client: a pure Angular 2 project, Angular CLI- based. • Server: a pure rest-api Grails project.
  23. 23. The Angular 2 Profile $ ng serve $ ./gradlew client:bootRun
  24. 24. CORS • Supported in Grails core since 3.2.2 • application.yml grails:
 cors:
 enabled: true
  25. 25. Demo - Angular 2 profile
  26. 26. Adding Security with Spring Security REST
  27. 27. Spring Security REST • Compatibility layer over Spring Security Core. • Login and logout REST endpoints. • Token validation filter. • Stateless by default, with JWT (signed and encrypted) • Memcached, Redis, GORM and Grails Cache token storages. • Implicit grant support through 3rd party providers. • RFC 6750 Bearer Token support.
  28. 28. Create the project $ grails create-app -profile angular -features hibernate,json-views,security todo | Application created at /tmp/todo
  29. 29. The interceptor angular
 .module("todo")
 .factory('authInterceptor', function ($rootScope, $window) {
 return {
 request: function (config) {
 config.headers = config.headers || {};
 if ($window.sessionStorage.token) {
 config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
 }
 return config;
 }
 };
 })
 .config(function ($httpProvider) {
 $httpProvider.interceptors.push('authInterceptor');
 })
 .controller("TodoController", TodoController);
  30. 30. Demo - All together
  31. 31. Thank you! Álvaro Sánchez-Mariscal

×