From fbdafee1a6756d2f205a4274d8f091b0a3668759 Mon Sep 17 00:00:00 2001 From: PEDSF Date: Fri, 18 Dec 2020 19:09:35 +0100 Subject: [PATCH] Add facebook login --- .../petclinic/common/CommonAttribute.java | 8 ++++ .../controller/common/UserController.java | 39 ++++++++++++------- src/main/resources/db/h2/data.sql | 2 +- src/main/resources/db/hsqldb/data.sql | 39 +++++++++++++++++++ src/main/resources/db/mysql/data.sql | 39 +++++++++++++++++++ src/main/resources/oauth2.properties | 8 ++-- 6 files changed, 114 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java b/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java index 9571befb1..1287c57fb 100644 --- a/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java +++ b/src/main/java/org/springframework/samples/petclinic/common/CommonAttribute.java @@ -13,6 +13,14 @@ public final class CommonAttribute { public static final String EMAIL = "email"; + public static final String FACEBOOK = "facebook"; + + public static final String FACEBOOK_FIRSTNAME = "name"; + + public static final String FACEBOOK_LASTNAME = "name"; + + public static final String FACEBOOK_PROVIDER_ID = "id"; + public static final String GITHUB = "github"; public static final String GITHUB_FIRSTNAME = "login"; diff --git a/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java b/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java index fe11aaa09..11f456fa1 100644 --- a/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java +++ b/src/main/java/org/springframework/samples/petclinic/controller/common/UserController.java @@ -159,25 +159,25 @@ public class UserController extends WebSocketSender { String firstName; String lastName; String email; - String providerId = ""; + String providerId; String provider = authentication.getAuthorizedClientRegistrationId(); + Map attributes = authentication.getPrincipal().getAttributes(); if (provider.equals(CommonAttribute.GOOGLE)) { - firstName = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_FIRSTNAME); - lastName = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_LASTNAME); - providerId = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_PROVIDER_ID); - } - else { - firstName = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_FIRSTNAME); - lastName = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_LASTNAME); - try { - providerId = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_PROVIDER_ID).toString(); - } catch (NullPointerException exception) { - log.error("Cast integer to string ",exception); - } + firstName = attributes.get(CommonAttribute.GOOGLE_FIRSTNAME).toString(); + lastName = attributes.get(CommonAttribute.GOOGLE_LASTNAME).toString(); + providerId = attributes.get(CommonAttribute.GOOGLE_PROVIDER_ID).toString(); + } else if (provider.equals(CommonAttribute.GITHUB)) { + firstName = attributes.get(CommonAttribute.GITHUB_FIRSTNAME).toString(); + lastName = attributes.get(CommonAttribute.GITHUB_LASTNAME).toString(); + providerId = attributes.get(CommonAttribute.GITHUB_PROVIDER_ID) .toString(); + } else { + firstName = attributes.get(CommonAttribute.FACEBOOK_FIRSTNAME).toString(); + lastName = attributes.get(CommonAttribute.FACEBOOK_LASTNAME).toString(); + providerId = attributes.get(CommonAttribute.FACEBOOK_PROVIDER_ID).toString(); } - email = authentication.getPrincipal().getAttribute(CommonAttribute.EMAIL); + email = attributes.get(CommonAttribute.EMAIL).toString(); CredentialDTO credential = credentialService.findByAuthentication(authentication); @@ -267,7 +267,16 @@ public class UserController extends WebSocketSender { } @GetMapping(CommonEndPoint.LOGOUT_SUCCESS) - public String postLogout(Model model) { + public String postLogout(HttpServletRequest request, HttpServletResponse response) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null) { + new SecurityContextLogoutHandler().logout(request, response, authentication); + } + + SecurityContextLogoutHandler securityContextLogoutHandler = new SecurityContextLogoutHandler(); + securityContextLogoutHandler.setInvalidateHttpSession(true); + securityContextLogoutHandler.setClearAuthentication(true); + sendSuccessMessage(CommonWebSocket.USER_LOGGED_OUT); return CommonView.HOME; } diff --git a/src/main/resources/db/h2/data.sql b/src/main/resources/db/h2/data.sql index 408505d02..21d03e803 100644 --- a/src/main/resources/db/h2/data.sql +++ b/src/main/resources/db/h2/data.sql @@ -82,7 +82,7 @@ INSERT INTO auth_providers (id, name) VALUES (1,'local'), (2,'google'), (3,'github'), - (4,'twitter'); + (4,'facebook'); INSERT INTO credentials (provider_id, email, password, verified) VALUES (1, 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true), diff --git a/src/main/resources/db/hsqldb/data.sql b/src/main/resources/db/hsqldb/data.sql index 16dda3e84..21d03e803 100644 --- a/src/main/resources/db/hsqldb/data.sql +++ b/src/main/resources/db/hsqldb/data.sql @@ -51,3 +51,42 @@ INSERT INTO visits VALUES (1, 7, '2013-01-01', 'rabies shot'); INSERT INTO visits VALUES (2, 8, '2013-01-02', 'rabies shot'); INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered'); INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed'); + +INSERT INTO roles (id, name) VALUES + (1,'ROLE_ADMIN'), + (2,'ROLE_STAFF'), + (3,'ROLE_USER'); + +INSERT INTO privileges (id, name) VALUES + (1,'CREATE'), + (2,'READ'), + (3,'UPDATE'), + (4,'DELETE'); + +INSERT INTO users (id, first_name, last_name, email, password, enabled, telephone, street1, zip_code, city, country) VALUES + (1, 'George', 'Franklin', 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true, '6085551023', '110 W. Liberty St.',12354,'Madison','USA'), + (2, 'Betty', 'Davis', 'betty.davis@petclinic.com', '$2a$10$InKx/fhX3CmLi8zKpHYx/.ETHUlZwvT1xn.Za/pp2JR0iEtYV9a9O', true, '6085551749','638 Cardinal Ave.', 6546, 'Sun Prairie', 'USA'), + (3, 'Eduardo', 'Rodriquez', 'eduardo.rodriguez@petclinic.com', '$2a$10$P55nbvVibHpoyWzenHngjOf.oEmcj74mI/VJaUZwGX9v8klctzsNW', true, '6085558763','2693 Commerce St.', 65454, 'McFarland', 'USA'), + (4, 'Paul-Emmanuel','DOS SANTOS FACAO','pedsf.fullstack@gmail.com','$2a$10$AzoUxi1IQFJMzLHcCGmDjuDHAQqAcAiRLz6UMeItdTL3mMWxMZEPC', true, '6085558763','2693 Commerce St.', 65454, 'McFarland', 'USA'); + +INSERT INTO users_roles (user_id, role_id) VALUES + (1,1),(1,2),(1,3), + (2,3),(3,3); + +INSERT INTO roles_privileges (role_id, privilege_id) values + (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3), + (3,1),(3,2); + +INSERT INTO auth_providers (id, name) VALUES + (1,'local'), + (2,'google'), + (3,'github'), + (4,'facebook'); + +INSERT INTO credentials (provider_id, email, password, verified) VALUES + (1, 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true), + (1, 'betty.davis@petclinic.com', '$2a$10$InKx/fhX3CmLi8zKpHYx/.ETHUlZwvT1xn.Za/pp2JR0iEtYV9a9O', true), + (1, 'eduardo.rodriguez@petclinic.com', '$2a$10$P55nbvVibHpoyWzenHngjOf.oEmcj74mI/VJaUZwGX9v8klctzsNW', true), + (2, 'pedsf.fullstack@gmail.com','117496521794255275093', true); + diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 3f1dcf8ea..2049d3483 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -51,3 +51,42 @@ INSERT IGNORE INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot'); INSERT IGNORE INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot'); INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered'); INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed'); + +INSERT INTO roles (id, name) VALUES + (1,'ROLE_ADMIN'), + (2,'ROLE_STAFF'), + (3,'ROLE_USER'); + +INSERT INTO privileges (id, name) VALUES + (1,'CREATE'), + (2,'READ'), + (3,'UPDATE'), + (4,'DELETE'); + +INSERT INTO users (id, first_name, last_name, email, password, enabled, telephone, street1, zip_code, city, country) VALUES + (1, 'George', 'Franklin', 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true, '6085551023', '110 W. Liberty St.',12354,'Madison','USA'), + (2, 'Betty', 'Davis', 'betty.davis@petclinic.com', '$2a$10$InKx/fhX3CmLi8zKpHYx/.ETHUlZwvT1xn.Za/pp2JR0iEtYV9a9O', true, '6085551749','638 Cardinal Ave.', 6546, 'Sun Prairie', 'USA'), + (3, 'Eduardo', 'Rodriquez', 'eduardo.rodriguez@petclinic.com', '$2a$10$P55nbvVibHpoyWzenHngjOf.oEmcj74mI/VJaUZwGX9v8klctzsNW', true, '6085558763','2693 Commerce St.', 65454, 'McFarland', 'USA'), + (4, 'Paul-Emmanuel','DOS SANTOS FACAO','pedsf.fullstack@gmail.com','$2a$10$AzoUxi1IQFJMzLHcCGmDjuDHAQqAcAiRLz6UMeItdTL3mMWxMZEPC', true, '6085558763','2693 Commerce St.', 65454, 'McFarland', 'USA'); + +INSERT INTO users_roles (user_id, role_id) VALUES + (1,1),(1,2),(1,3), + (2,3),(3,3); + +INSERT INTO roles_privileges (role_id, privilege_id) values + (1,1),(1,2),(1,3),(1,4), + (2,1),(2,2),(2,3), + (3,1),(3,2); + +INSERT INTO auth_providers (id, name) VALUES + (1,'local'), + (2,'google'), + (3,'github'), + (4,'facebook'); + +INSERT INTO credentials (provider_id, email, password, verified) VALUES + (1, 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true), + (1, 'betty.davis@petclinic.com', '$2a$10$InKx/fhX3CmLi8zKpHYx/.ETHUlZwvT1xn.Za/pp2JR0iEtYV9a9O', true), + (1, 'eduardo.rodriguez@petclinic.com', '$2a$10$P55nbvVibHpoyWzenHngjOf.oEmcj74mI/VJaUZwGX9v8klctzsNW', true), + (2, 'pedsf.fullstack@gmail.com','117496521794255275093', true); + diff --git a/src/main/resources/oauth2.properties b/src/main/resources/oauth2.properties index 3d4adbfc4..f6d360489 100644 --- a/src/main/resources/oauth2.properties +++ b/src/main/resources/oauth2.properties @@ -1,11 +1,9 @@ - +# credentials for providers API with Oauth2 spring.security.oauth2.client.registration.google.client-id=${OAUTH2_GOOGLE_CLIENT_ID} spring.security.oauth2.client.registration.google.client-secret=${OAUTH2_GOOGLE_CLIENT_SECRET} spring.security.oauth2.client.registration.github.client-id=${OAUTH2_GITHUB_CLIENT_ID} spring.security.oauth2.client.registration.github.client-secret=${OAUTH2_GITHUB_CLIENT_SECRET} -#spring.security.oauth2.client.registration.facebook.client-id=${OAUTH2_FACEBOOK_CLIENT_ID} -#spring.security.oauth2.client.registration.facebook.client-secret=${OAUTH2_FACEBOOK_CLIENT_SECRET} -spring.security.oauth2.client.registration.facebook.client-id=121189305185277 -spring.security.oauth2.client.registration.facebook.client-secret=42ffe5aa7379e8326387e0fe16f34132 +spring.security.oauth2.client.registration.facebook.client-id=${OAUTH2_FACEBOOK_CLIENT_ID} +spring.security.oauth2.client.registration.facebook.client-secret=${OAUTH2_FACEBOOK_CLIENT_SECRET}