SpringSocial
개발가이
드 작성
류지만
ryu.jiman@gmail.com
010-7373-1732
1. 형상관리
gradle
https://github.com/spring-projects/spring-social.git
maven
https://github.com/spring-projects/spring-social...
/spring-social-facebook/src/main/resources/org/springframework/social/facebook/config/xml/spring-social-acebook-1.1.xsd
<?...
spring-social-facebook/spring-social-facebook-web/src/main/resources/META-INF/facebook.tld
<?xml version="1.0" encoding="U...
/spring-social-showcase/src/main/webapp/WEB-INF/views/connect/facebookConnect.html
<!DOCTYPE html>
<html xmlns:th="http://...
spring-social-web/src/main/java/org/springframework/social/connect/web/ConnectController.java
/**
* Generic UI controller ...
ConnectController
Connection Factories /
Connection Factory Locator Connection Repository
API Bindings
ProviderSignInContr...
A real Spring Security Authentication Filter
Follows roughly the same flow as ProviderSignInController Plugs into
Spring S...
POST /signin/{provider}
Initiate signin flow for the specified provider
GET /signin/{provider}?oauth_token={t}[&verifier={...
Similar flow as ConnectController/Different Goal
Compares connections (by user ID)
If there’s a match, user is signed in
I...
GET /connect
Display connection status for all providers
GET /connect/{provider}
Display connection status for a specific ...
@Controller
public class TwitterTimelineController {
private final Twitter twitter;
@Inject
public TwitterTimelineControll...
<context:property-placeholder location=
"classpath:/org/springframework/social/showcase/config/application.properties" />
...
@Configuration
@EnableJdbcConnectionRepository
@EnableFacebook(appId="${facebook.clientId}",
appSecret="${facebook.clientS...
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" p...
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" p...
package org.springframework.social.showcase.facebook;
import org.springframework.social.ApiException;
import org.springfra...
package org.springframework.social.showcase.twitter;
import org.springframework.social.DuplicateStatusException;
import or...
오픈강의 2014 springsocial
오픈강의 2014 springsocial
Upcoming SlideShare
Loading in...5
×

오픈강의 2014 springsocial

315

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
315
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

오픈강의 2014 springsocial

  1. 1. SpringSocial 개발가이 드 작성 류지만 ryu.jiman@gmail.com 010-7373-1732
  2. 2. 1. 형상관리 gradle https://github.com/spring-projects/spring-social.git maven https://github.com/spring-projects/spring-social-twitter.git maven https://github.com/spring-projects/spring-social-facebook.git sample https://github.com/spring-projects/spring-social-samples.git Github
  3. 3. /spring-social-facebook/src/main/resources/org/springframework/social/facebook/config/xml/spring-social-acebook-1.1.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:facebook="http://www.springframework.org/schema/social/facebook" targetNamespace="http://www.springframework.org/schema/social/facebook"> <xs:element name="config"> <xs:complexType> <xs:attribute name="app-id" use="required" type="xs:string" /> <xs:attribute name="app-secret" use="required" type="xs:string" /> <xs:attribute name="app-namespace" use="optional" type="xs:string" /> </xs:complexType> </xs:element> </xs:schema>
  4. 4. spring-social-facebook/spring-social-facebook-web/src/main/resources/META-INF/facebook.tld <?xml version="1.0" encoding="UTF-8"?> <taglib xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd version="2.0"> <description>Spring Social Facebook JSP Tag Library</description> <tlib-version>1.0</tlib-version> <short-name>facebook</short-name> <uri>http://www.springframework.org/spring-social/facebook/tags</uri> <tag> <description>Initializes Facebook's JavaScript API</description> <name>init</name> <tag-class>org.springframework.social.facebook.web.FacebookInitTag</tag-class> <body-content>empty</body-content> <attribute> <description>The application's Facebook ID</description> <name>appId</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
  5. 5. /spring-social-showcase/src/main/webapp/WEB-INF/views/connect/facebookConnect.html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:social="http://spring.io/springsocial" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout"> <head> <title>Spring Social Showcase</title> </head> <body> <div id="content" layout:fragment="content"> <h3>Connect to Facebook</h3> <form th:action="@{/connect/facebook}" method="POST"> <input type="hidden" name="_csrf" th:value="${_csrf.token}" /> <input type="hidden" name="scope" value="publish_stream,user_photos,offline_access" /> <div class="formInfo"> <p>You aren't connected to Facebook </p> </div> <p> <button type="submit"> <img th:src="@{/resources/social/facebook/connect_light_medium_short.gif}"/></button></p> <label for="postToWall"> <input id="postToWall" type="checkbox" name="postToWall" /> Facebook wall</label> </form> </div> </body> </html>
  6. 6. spring-social-web/src/main/java/org/springframework/social/connect/web/ConnectController.java /** * Generic UI controller for managing the account-to-service-provider connection flow. * <ul> * <li>GET /connect/{providerId} - Get a web page showing connection status to {providerId}.</li> * <li>POST /connect/{providerId} - Initiate an connection with {providerId}.</li> * <li>GET /connect/{providerId}?oauth_verifier||code - Receive {providerId} authorization callback and establish the connection.< * <li>DELETE /connect/{providerId} - Disconnect from {providerId}.</li> * </ul> */ @Controller @RequestMapping("/connect") public class ConnectController implements InitializingBean {
  7. 7. ConnectController Connection Factories / Connection Factory Locator Connection Repository API Bindings ProviderSignInController SocialAuthenticationFilter CanvasSignInController Obtain application credentials from API provider Configure Provider Configure ConnectController Configure UserIdSource
  8. 8. A real Spring Security Authentication Filter Follows roughly the same flow as ProviderSignInController Plugs into Spring Security filter chain like any other authentication filter Comes with a configurer for Spring Security 3.2 configuration protected void configure(HttpSecurity http) throws Exception { http .formLogin() .loginPage("/signin") .loginProcessingUrl("/signin/authenticate") .failureUrl("/signin?param.error=bad_credentials") ... // more Spring security config goes here .and() .apply(new SpringSocialConfigurer()) .and().setSharedObject(ApplicationContext.class, context); } SocialAuthenticationFilter
  9. 9. POST /signin/{provider} Initiate signin flow for the specified provider GET /signin/{provider}?oauth_token={t}[&verifier={v}] Handle callback for OAuth 1.0/1.0a provider. Compare GET /signin/{provider}?code={c} Handle callback from OAuth 2 provider GET /signin/{provider}?error={e} Handle callback from OAuth 2 provider indicating an authorization error ProviderSignInController Endpoints
  10. 10. Similar flow as ConnectController/Different Goal Compares connections (by user ID) If there’s a match, user is signed in If no match, then user is sent to signup page @Bean public ProviderSignInController providerSignInController( ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository usersConnectionRepository, RequestCache requestCache) { return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, } ConnectController
  11. 11. GET /connect Display connection status for all providers GET /connect/{provider} Display connection status for a specific provider POST /connect/{provider} Initiate connection flow for the specified provider GET /connect/{provider}?oauth_token={t}[&verifier={v}] Handle callback for OAuth 1.0/1.0a provider GET /connect/{provider}?code={c} Handle callback for OAuth 2 provider DELETE /connect/{provider} Remove all connections for a given provider DELETE /connect/{provider}/{provider user ID} Remove a single connection for a given provider, identified by the user’s user ID at the provider ConnectController Endpoints
  12. 12. @Controller public class TwitterTimelineController { private final Twitter twitter; @Inject public TwitterTimelineController(Twitter twitter) { this.twitter = twitter; } @RequestMapping(value="/twitter/tweet", method=RequestMethod.POST) public String postTweet(String message) { twitter.timelineOperations().updateStatus(message); return "redirect:/twitter"; } } Injecting and Using API Bindings
  13. 13. <context:property-placeholder location= "classpath:/org/springframework/social/showcase/config/application.properties" /> <facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" app-namespace="socialshowcase" /> <social:jdbc-connection-repository/> <bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" /> <bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor" /> Configuring Spring Social with XML
  14. 14. @Configuration @EnableJdbcConnectionRepository @EnableFacebook(appId="${facebook.clientId}", appSecret="${facebook.clientSecret}") public class SocialConfig { @Bean public ConnectController connectController( ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { return ConnectController(connectionFactoryLocator, connectionRepository); } @Bean public UserIdSource userIdSource() { return new AuthenticationNameUserIdSource(); } } Configuring Spring Social
  15. 15. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %> <%@ page session="false" %> <h3>Connect to Twitter</h3> <form action="<c:url value="/connect/twitter" />" method="POST"> <div class="formInfo"> <p> You haven't created any connections with Twitter yet. </p> </div> <p><button type="submit"> <img src="<c:url value="/resources/social/twitter/connect-with-twitter.png" />"/></button></p> <label for="postTweet"> <input id="postTweet" type="checkbox" name="postTweet" /> Post a tweet </label> </form> Tiles /WEB-INF/views/connect/tiles.xml <definition name="connect/twitterConnected" extends="page"> <put-attribute name="content" value="/WEB-INF/views/connect/twitterConnected.jsp" /> </definition> WEB-INF/views/connect/twitterConnect.jsp
  16. 16. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %> <%@ taglib uri="http://www.springframework.org/spring-social/facebook/tags" prefix="facebook" %> <%@ page session="false" %> <h3>Connect to Facebook</h3> <form action="<c:url value="/connect/facebook" />" method="POST"> <input type="hidden" name="scope" value="publish_stream,user_photos,offline_access" /> <div class="formInfo"> <p>You aren't connected to Facebook yet.</p> </div> <p><button type="submit"> <img src="<c:url value="/resources/social/facebook/connect_light_medium_short.gif" />"/></button></p> <label for="postToWall"> <input id="postToWall" type="checkbox" name="postToWall" /> Tell your friends about on your Facebook wall</label> </form> Tiles /WEB-INF/views/connect/tiles.xml <definition name="connect/facebookConnect" extends="page"> <put-attribute name="content" value="/WEB-INF/views/connect/facebookConnect.jsp" /> </definition> /WEB-INF/views/connect/facebookConnect.jsp
  17. 17. package org.springframework.social.showcase.facebook; import org.springframework.social.ApiException; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.web.ConnectInterceptor; import org.springframework.social.facebook.api.Facebook; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import org.springframework.web.context.request.WebRequest; public class PostToWallAfterConnectInterceptor implements ConnectInterceptor<Facebook> { public void preConnect(ConnectionFactory<Facebook> connectionFactory, MultiValueMap<String, String> parameters, WebRequ if (StringUtils.hasText(request.getParameter(POST_TO_WALL_PARAMETER))) { request.setAttribute(POST_TO_WALL_ATTRIBUTE, Boolean.TRUE, WebRequest.SCOPE_SESSION); } } public void postConnect(Connection<Facebook> connection, WebRequest request) { if (request.getAttribute(POST_TO_WALL_ATTRIBUTE, WebRequest.SCOPE_SESSION) != null) { try { connection.updateStatus("I've connected with the Spring Social Showcase!"); } catch (ApiException e) { // Do nothing: No need to break down if the post-connect post can't be made. } request.removeAttribute(POST_TO_WALL_ATTRIBUTE, WebRequest.SCOPE_SESSION); } } private static final String POST_TO_WALL_PARAMETER = "postToWall"; private static final String POST_TO_WALL_ATTRIBUTE = "facebookConnect." + POST_TO_WALL_PARAMETER; } PostToWallAfterConnectInterceptor
  18. 18. package org.springframework.social.showcase.twitter; import org.springframework.social.DuplicateStatusException; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.web.ConnectInterceptor; import org.springframework.social.twitter.api.Twitter; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import org.springframework.web.context.request.WebRequest; public class TweetAfterConnectInterceptor implements ConnectInterceptor<Twitter> { public void preConnect(ConnectionFactory<Twitter> provider, MultiValueMap<String, String> parameters, WebRequest request) if (StringUtils.hasText(request.getParameter(POST_TWEET_PARAMETER))) { request.setAttribute(POST_TWEET_ATTRIBUTE, Boolean.TRUE, WebRequest.SCOPE_SESSION); } } public void postConnect(Connection<Twitter> connection, WebRequest request) { if (request.getAttribute(POST_TWEET_ATTRIBUTE, WebRequest.SCOPE_SESSION) != null) { try { connection.updateStatus("I've connected with the Spring Social Showcase!"); } catch (DuplicateStatusException e) { } request.removeAttribute(POST_TWEET_ATTRIBUTE, WebRequest.SCOPE_SESSION); } } private static final String POST_TWEET_PARAMETER = "postTweet"; private static final String POST_TWEET_ATTRIBUTE = "twitterConnect." + POST_TWEET_PARAMETER; } PostToWallAfterConnectInterceptor

×