{"id":246,"date":"2020-03-21T20:37:00","date_gmt":"2020-03-21T11:37:00","guid":{"rendered":"http:\/\/www.skyer9.pe.kr\/wordpress\/?p=246"},"modified":"2020-03-28T18:23:43","modified_gmt":"2020-03-28T09:23:43","slug":"from-hello-to-querydsl-facebook-kakao-login-11-12","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=246","title":{"rendered":"[From Hello To QueryDSL] Facebook\/Kakao Login (11\/12)"},"content":{"rendered":"<h1>Facebook\/Kakao Login<\/h1>\n<h2>\uac1c\ubc1c\ud658\uacbd<\/h2>\n<ul>\n<li>Spring Boot 2.1.x<\/li>\n<li>Gradle 4.10.2<\/li>\n<\/ul>\n<h2>\ud30c\uc77c \uc218\uc815 \ubc0f \ucd94\uac00<\/h2>\n<p>src\/main\/java\/kr\/co\/episode\/example\/config\/oauth2\/dto\/OAuthAttributes.java<\/p>\n<pre><code class=\"language-java\">@Getter\npublic class OAuthAttributes {\n\n    \/\/ ......\n\n    public static OAuthAttributes of(String registrationId, String userNameAttributeName, Map&lt;String, Object&gt; attributes) {\n        \/\/System.out.println(registrationId);\n        \/\/System.out.println(attributes);\n\n        if (&quot;naver&quot;.equals(registrationId)) {\n            return ofNaver(&quot;id&quot;, attributes);\n        }\n\n        if (&quot;facebook&quot;.equals(registrationId)) {\n            return ofFacebook(&quot;id&quot;, attributes);\n        }\n\n        if (&quot;kakao&quot;.equals(registrationId)) {\n            return ofKakao(&quot;id&quot;, attributes);\n        }\n\n        return ofGoogle(userNameAttributeName, attributes);\n    }\n\n    \/\/ ......\n\n    private static OAuthAttributes ofFacebook(String userNameAttributeName, Map&lt;String, Object&gt; attributes) {\n        \/\/ \uc544\ub798\uc758 \uacbd\uc6b0, \uc774\uba54\uc77c\uc744 \ubc18\ud658\ud558\uc9c0 \uc54a\ub294\ub2e4.\n        \/\/ No Email address on account\n        \/\/ No confirmed email address on account\n        \/\/ No verified email address on account\n        \/\/ https:\/\/stackoverflow.com\/questions\/17532476\/facebook-email-field-return-null-even-if-the-email-permission-is-set-and-acce\n        String email = (String) attributes.get(&quot;email&quot;);\n        if (email == null) {\n            email = ((String) attributes.get(&quot;name&quot;)) + &quot;@facebook.com&quot;;\n        }\n\n        Map&lt;String, Object&gt; picture = (Map&lt;String, Object&gt;) attributes.get(&quot;picture&quot;);\n        Map&lt;String, Object&gt; picture_data = (Map&lt;String, Object&gt;) picture.get(&quot;data&quot;);\n        String picture_url = (String) picture_data.get(&quot;url&quot;);\n\n        return OAuthAttributes.builder()\n                .name((String) attributes.get(&quot;name&quot;))\n                .email(email)\n                .picture(picture_url)\n                .attributes(attributes)\n                .nameAttributeKey(userNameAttributeName)\n                .build();\n    }\n\n    private static OAuthAttributes ofKakao(String userNameAttributeName, Map&lt;String, Object&gt; attributes) {\n        Map&lt;String, Object&gt; properties = (Map&lt;String, Object&gt;) attributes.get(&quot;properties&quot;);\n        Map&lt;String, Object&gt; kakao_account = (Map&lt;String, Object&gt;) attributes.get(&quot;kakao_account&quot;);\n        String name = (String) properties.get(&quot;nickname&quot;);\n        String email = (String) kakao_account.get(&quot;email&quot;);\n        String picture = (String) properties.get(&quot;profile_image&quot;);\n\n        return OAuthAttributes.builder()\n                .name(name)\n                .email(email)\n                .picture(picture)\n                .attributes(attributes)\n                .nameAttributeKey(userNameAttributeName)\n                .build();\n    }\n\n    \/\/ ......\n}<\/code><\/pre>\n<p>src\/main\/resources\/application.properties<\/p>\n<pre><code class=\"language-configuration\"># =========================================================\n# Facebook OAuth2\n# =========================================================\nspring.security.oauth2.client.registration.facebook.client-id=&lt;user client-id&gt;\nspring.security.oauth2.client.registration.facebook.client-secret=&lt;user secret&gt;\nspring.security.oauth2.client.registration.facebook.scope=profile,email\nspring.security.oauth2.client.provider.facebook.user-info-uri=https:\/\/graph.facebook.com\/me?fields=email,name,locale\n\n# =========================================================\n# Kakao OAuth2\n# =========================================================\nspring.security.oauth2.client.registration.kakao.client-id=&lt;user client-id&gt;\nspring.security.oauth2.client.registration.kakao.client-secret=&lt;user secret&gt;\nspring.security.oauth2.client.registration.kakao.authorization-grant-type=authorization_code\nspring.security.oauth2.client.registration.kakao.redirect-uri={baseUrl}\/{action}\/oauth2\/code\/{registrationId}\nspring.security.oauth2.client.registration.kakao.scope=profile,account_email\nspring.security.oauth2.client.registration.kakao.client-name=Kakao\nspring.security.oauth2.client.registration.kakao.client-authentication-method=POST\nspring.security.oauth2.client.provider.kakao.token-uri=https:\/\/kauth.kakao.com\/oauth\/token\nspring.security.oauth2.client.provider.kakao.authorization-uri=https:\/\/kauth.kakao.com\/oauth\/authorize\nspring.security.oauth2.client.provider.kakao.user-info-uri=https:\/\/kapi.kakao.com\/v2\/user\/me\nspring.security.oauth2.client.provider.kakao.user-name-attribute=id<\/code><\/pre>\n<p>src\/main\/resources\/templates\/index.html<\/p>\n<pre><code class=\"language-html\">&lt;h1&gt;\uc2a4\ud504\ub9c1 \ubd80\ud2b8 \uac8c\uc2dc\ud310&lt;\/h1&gt;\n\n&lt;div class=&quot;col-md-12&quot;&gt;\n    &lt;div class=&quot;row&quot;&gt;\n        &lt;div class=&quot;col-md-6&quot;&gt;\n            &lt;a href=&quot;\/posts\/save&quot; role=&quot;button&quot; class=&quot;btn&quot; btn-primary&gt;\uae00 \ub4f1\ub85d&lt;\/a&gt;\n            &lt;div th:if=&quot;${userName != null}&quot; th:inline=&quot;text&quot;&gt;\n                [[${userName}]] \ub2d8, \uc548\ub155\ud558\uc138\uc694.\n                &lt;a href=&quot;\/logout&quot; class=&quot;btn btn-info active&quot; role=&quot;button&quot;&gt;\ub85c\uadf8\uc544\uc6c3&lt;\/a&gt;\n            &lt;\/div&gt;\n            &lt;div th:if=&quot;${userName == null}&quot;&gt;\n                &lt;a href=&quot;\/oauth2\/authorization\/google&quot; class=&quot;btn btn-success active&quot; role=&quot;button&quot;&gt;\uad6c\uae00 \ub85c\uadf8\uc778&lt;\/a&gt;\n                &lt;a href=&quot;\/oauth2\/authorization\/facebook&quot; class=&quot;btn btn-success active&quot; role=&quot;button&quot;&gt;\ud398\uc774\uc2a4\ubd81 \ub85c\uadf8\uc778&lt;\/a&gt;\n                &lt;a href=&quot;\/oauth2\/authorization\/naver&quot; class=&quot;btn btn-success active&quot; role=&quot;button&quot;&gt;\ub124\uc774\ubc84 \ub85c\uadf8\uc778&lt;\/a&gt;\n                &lt;a href=&quot;\/oauth2\/authorization\/kakao&quot; class=&quot;btn btn-success active&quot; role=&quot;button&quot;&gt;\uce74\uce74\uc624 \ub85c\uadf8\uc778&lt;\/a&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;div style=&quot;height: 80px;&quot;&gt;\n\n&lt;\/div&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Facebook\/Kakao OAuth2 Login \uc744 \uad6c\ud604\ud569\ub2c8\ub2e4.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-246","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-1"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=246"}],"version-history":[{"count":4,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}