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.

Spring Developer's Guide to Social Integration

1,317 views

Published on

Recorded at SpringOneGX 2015
Presenter: Craig Walls
Data / Integration Track
There's an internet full of services ready to be plugged into your Spring applications. These services offer a wealth of information about the users of your application, including information about their interests, places they travel to and visit, their friends, family, and colleagues, and even what temperature they like to keep in their home. All you need to do is tap into it.

In this session, you'll see how to use Spring Social, Spring Boot, and Spring Integration to expand the reach of your application to those services, effectively injecting your app into their social graph.

Published in: Technology
  • Be the first to comment

Spring Developer's Guide to Social Integration

  1. 1. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Spring Developer’s Guide to Social Integration By Craig Walls @habuma / @SpringSocial
  2. 2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Safe Harbor Statement The following is intended to outline the general direction of Pivotal's offerings. It is intended for information purposes only and may not be incorporated into any contract. Any information regarding pre-release of Pivotal offerings, future updates or other planned modifications is subject to ongoing evaluation by Pivotal and is subject to change. This information is provided without warranty or any kind, express or implied, and is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions regarding Pivotal's offerings. These purchasing decisions should only be based on features currently available. The development, release, and timing of any features or functionality described for Pivotal's offerings in this presentation remain at the sole discretion of Pivotal. Pivotal has no obligation to update forward looking information in this presentation. 2
  3. 3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ John Lennon Statement Life is what happens while you are busy making other plans. 3
  4. 4. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What’s in a tweet? 4
  5. 5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Be a part of the session demo! Tweet about this session! Use #SpringSocial and/or #s2gx and/or mention @springone2gx 5
  6. 6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reaching out • Microservices are typically inside some defined application boundary • But there are hundreds (thousands?) of APIs chock full of data outside of your application. • Most are secured with OAuth 6
  7. 7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ OAuth in 1 slide • Authorization protocol • Your app needs to access a user’s data on some other API • Your app must get permission to access that data • Many ways to obtain an access token • OAuth 1.0(a) • OAuth 2 authorization code grant • OAuth 2 implicit grant • OAuth 2 resource owner credentials (aka, “password”) grant • OAuth 2 client credentials grant • Challenge: Where do you keep the authorization/token? 7
  8. 8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Social 8
  9. 9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Intro to Spring Social • Handles the OAuth dance • Securely stores authorization details (e.g., the token) • Provides API bindings for several APIs, including • Facebook, Twitter, LinkedIn, TripIt, GitHub • plus lots of community projects • NOT JUST SOCIAL!!! • Ecobee - Control and access thermostats • Wunderlist - Integrate with your users’ TODO lists, reminders, and errands • GitHub and BitBucket - Integrate with source code control 9
  10. 10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Social Project Family 10 Spring Social Core Security Web Config Provider Bindings Facebook Twitter LinkedIn GitHub TripIt (?) Community-Led Projects 500px BitBucket DropBox Foursquare Instagram last.fm Mixcloud SoundCloud Vkontakte Yammer Alfresco Daum Flattr Geeklist Intuit/QBO Win. Live Nk Tumblr Weibo App.net Digg Flickr Google Khan Acad. Miso Salesforce Viadeo XingWunderlist Alfresco Ecobee Strava Slideshare
  11. 11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The moving parts 11 creates 11 Performs OAuth “dance”. Handles requests for “/connect/{providerId} Redirects the browser to the API’s authorization page. Exchanges the authorization code for an access token. Looks up a ConnectionFactory for a given API provider and uses it to create a Connection. Stores the Connection in the database via a ConnectionRepository. Looks up a ConnectionFactory by the API provider ID. Creates Connections. Also knows specifics regarding an API provider’s OAuth implementation. Stores Connections in a database for future use. Makes requests to an API on behalf of the application, ensuring that the Authorization header is on each request. Connect Controller Connection Factory Locator Connection Repository Connection Factory Connection API Binding stored via
  12. 12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ConnectController’s connection flow 12 GET /connect
  13. 13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ConnectController’s connection flow 13 GET /connect/twitterPOST /connect/twitter redirectredirect GET /connect/twitter?oauth_token={t}&verifier={v}GET /connect/twitter
  14. 14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Sign-in-with… • ProviderSignInController • Spring MVC controller • Security framework agnostic • Best suited for apps not using Spring Security • SocialAuthenticationFilter • Implementation of Spring Security’s AuthenticationFilter • Plugs into Spring Security’s filter chain • Best suited for apps secured with Spring Security 14
  15. 15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Miscellaneous • ReconnectFilter • Detects stale/revoked connections, redirects to ConnectController to establish a new connection • DisconnectController (Facebook only) • Handles disconnect callbacks from Facebook, removes connection • RealTimeUpdateController (Facebook only) • Handles real-time update callbacks from Facebook • GenericOAuth1ConnectionFactory and GenericOAuth2ConnectionFactory • Enables connectivity for otherwise unsupported APIs 15
  16. 16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ You might also want to know about… • OAuth1Template / OAuth2Template • Lower level component for working with OAuth • Used by higher-level Spring Social components • Best used for client tokens and password grant 16
  17. 17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Demos 17 Spring Social Showcase & The simplest Spring Social app ever!
  18. 18. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Integrating Social 18
  19. 19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Integration and Twitter 19 @Bean public MessageSource<Tweet> twitterSource(Twitter twitter) { SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(twitter, "twsearch"); messageSource.setQuery("@springone2gx OR #s2gx"); return messageSource; } @Bean public IntegrationFlow myFlow(MessageSource<Tweet> tweetSource, TweetAnalyzer tweetAnalyzer) { return IntegrationFlows.from(tweetSource, e-> e.poller(Pollers.fixedDelay(10000) .maxMessagesPerPoll(10))) .transform("payload") .transform(tweetAnalyzer) .handle(handler) .get(); }
  20. 20. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Demo 20 Creating a tweet pipeline with Spring Integration
  21. 21. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Social.NEXT 21
  22. 22. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Social is a social project 22 From comment on reddit by “BadMoonRosin”
  23. 23. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Be a part of the Spring Social community Report issues / suggest improvements http://jira.spring.io/browse/SOCIAL (append “FB”, “TW”, “LI”, “GH” for provider projects) 23
  24. 24. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Be a part of the Spring Social community Ask questions / Answer questions http://stackoverflow.com/questions/tagged/spring-social (append “-facebook”, “-twitter”, “-linkedin”, “-github” for provider projects) 24
  25. 25. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Be a part of the Spring Social community Fix issues / submit improvements Fork http://github.com/spring-projects/spring-social (append “-facebook”, “-twitter”, “-linkedin”, “-github” for provider projects) Make changes (and write tests!) Submit Individual Contributor Agreement (https://support.springsource.com/spring_committer_signup) Submit pull request / Test someone else’s pull request 25
  26. 26. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Expand the Spring Social Project Family 26 Spring Social Core Security Web Config Provider Bindings Facebook Twitter LinkedIn GitHub TripIt (?) Community-Led Projects 500px BitBucket DropBox Foursquare Instagram last.fm Mixcloud SoundCloud Vkontakte Yammer Alfresco Daum Flattr Geeklist Intuit/QBO Win. Live Nk Tumblr Weibo App.net Digg Flickr Google Khan Acad. Miso Salesforce Viadeo XingWunderlist Alfresco Ecobee Strava Slideshare Your Spring Social Extension Here!
  27. 27. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Step 1: Create a service provider 27 public class MyPeepsServiceProvider extends AbstractOAuth2ServiceProvider<MyPeeps> { public MyPeepsServiceProvider(String clientId, String clientSecret) { super(getOAuth2Template(clientId, clientSecret)); } private static OAuth2Template getOAuth2Template(String clientId, String clientSecret) { OAuth2Template oAuth2Template = new OAuth2Template(clientId, clientSecret, “https://www.mypeeps.com/oauth2/authorization”, “https://www.mypeeps.com/oauth2/accessToken”); oAuth2Template.setUseParametersForClientAuthentication(true); return oAuth2Template; } public MyPeeps getApi(String accessToken) { return new MyPeepsTemplate(accessToken); } }
  28. 28. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Step 2: Create a connection factory 28 public class MyPeepsConnectionFactory extends OAuth2ConnectionFactory<MyPeeps>{ public MyPeepsConnectionFactory(String consumerKey, String consumerSecret) { super("mypeeps", new MyPeepsServiceProvider(consumerKey, consumerSecret), new MyPeepsAdapter()); } }
  29. 29. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Step 3: Create an API binding • Will vary from API to API • Binding template • Accepts access token as constructor parameter • Offers methods for consuming API • Domain types • Java types mapped to API request forms and responses 29
  30. 30. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Step 4: Create an API adapter 30 public class LinkedInAdapter implements ApiAdapter<MyPeeps> { public boolean test(MyPeeps myPeeps) { try { myPeeps.profileOperations().getUserProfile(); return true; } catch (HttpClientErrorException e) { return false; } } public void setConnectionValues(MyPeeps myPeeps, ConnectionValues values) { MyPeepsProfile profile = myPeeps.profileOperations().getUserProfile(); values.setProviderUserId(profile.getId()); … } public UserProfile fetchUserProfile(MyPeeps myPeeps) { MyPeepsProfile profile = myPeeps.profileOperations().getUserProfile(); return new UserProfileBuilder().setName(profile.getFirstName() + " " + profile.getLastName()) .setEmail(profile.getEmailAddress()).build(); } public void updateStatus(MyPeeps myPeeps, String message) { myPeeps.statusOperations().updateStatus(message); } }
  31. 31. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Hold on… Why so much boilerplate? 31
  32. 32. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ A simpler extension model 32 spring: social: providers: mypeeps: authorization-url: https://mypeeps.com/oauth/auth token-url: https://mypeeps.com/oauth/token api-binding-class: com.mypeeps.api.MyPeepsTemplate
  33. 33. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Current work in progress/research…* • A simpler extension model (specifically SocialAuthenticationFilter support) • More complete Spring Boot support • Tighter alignment with Spring Security OAuth • OpenID Connect support • Stateless connections / JWT support 33 * See “Safe Harbor Statement” at beginning of session
  34. 34. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 34 http://projects.spring.io/spring-social/ Spring Integration Java DSL - Artem Bilan Applied Spring Integration, Spring AMQP and RabbitMQ in Spring XD - Gary Russell Learn More. Stay Connected. @springcentral Spring.io/video

×