Add facebook login

This commit is contained in:
PEDSF 2020-12-18 19:09:35 +01:00
parent 824bd0d209
commit fbdafee1a6
6 changed files with 114 additions and 21 deletions

View file

@ -13,6 +13,14 @@ public final class CommonAttribute {
public static final String EMAIL = "email"; 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 = "github";
public static final String GITHUB_FIRSTNAME = "login"; public static final String GITHUB_FIRSTNAME = "login";

View file

@ -159,25 +159,25 @@ public class UserController extends WebSocketSender {
String firstName; String firstName;
String lastName; String lastName;
String email; String email;
String providerId = ""; String providerId;
String provider = authentication.getAuthorizedClientRegistrationId(); String provider = authentication.getAuthorizedClientRegistrationId();
Map<String,Object> attributes = authentication.getPrincipal().getAttributes();
if (provider.equals(CommonAttribute.GOOGLE)) { if (provider.equals(CommonAttribute.GOOGLE)) {
firstName = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_FIRSTNAME); firstName = attributes.get(CommonAttribute.GOOGLE_FIRSTNAME).toString();
lastName = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_LASTNAME); lastName = attributes.get(CommonAttribute.GOOGLE_LASTNAME).toString();
providerId = authentication.getPrincipal().getAttribute(CommonAttribute.GOOGLE_PROVIDER_ID); providerId = attributes.get(CommonAttribute.GOOGLE_PROVIDER_ID).toString();
} } else if (provider.equals(CommonAttribute.GITHUB)) {
else { firstName = attributes.get(CommonAttribute.GITHUB_FIRSTNAME).toString();
firstName = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_FIRSTNAME); lastName = attributes.get(CommonAttribute.GITHUB_LASTNAME).toString();
lastName = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_LASTNAME); providerId = attributes.get(CommonAttribute.GITHUB_PROVIDER_ID) .toString();
try { } else {
providerId = authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_PROVIDER_ID).toString(); firstName = attributes.get(CommonAttribute.FACEBOOK_FIRSTNAME).toString();
} catch (NullPointerException exception) { lastName = attributes.get(CommonAttribute.FACEBOOK_LASTNAME).toString();
log.error("Cast integer to string ",exception); 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); CredentialDTO credential = credentialService.findByAuthentication(authentication);
@ -267,7 +267,16 @@ public class UserController extends WebSocketSender {
} }
@GetMapping(CommonEndPoint.LOGOUT_SUCCESS) @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); sendSuccessMessage(CommonWebSocket.USER_LOGGED_OUT);
return CommonView.HOME; return CommonView.HOME;
} }

View file

@ -82,7 +82,7 @@ INSERT INTO auth_providers (id, name) VALUES
(1,'local'), (1,'local'),
(2,'google'), (2,'google'),
(3,'github'), (3,'github'),
(4,'twitter'); (4,'facebook');
INSERT INTO credentials (provider_id, email, password, verified) VALUES INSERT INTO credentials (provider_id, email, password, verified) VALUES
(1, 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true), (1, 'georges.franklin@petclinic.com', '$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG', true),

View file

@ -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 (2, 8, '2013-01-02', 'rabies shot');
INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered'); INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered');
INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed'); 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);

View file

@ -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 (2, 8, '2011-03-04', 'rabies shot');
INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered'); INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered');
INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed'); 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);

View file

@ -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-id=${OAUTH2_GOOGLE_CLIENT_ID}
spring.security.oauth2.client.registration.google.client-secret=${OAUTH2_GOOGLE_CLIENT_SECRET} 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-id=${OAUTH2_GITHUB_CLIENT_ID}
spring.security.oauth2.client.registration.github.client-secret=${OAUTH2_GITHUB_CLIENT_SECRET} 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-id=${OAUTH2_FACEBOOK_CLIENT_ID}
#spring.security.oauth2.client.registration.facebook.client-secret=${OAUTH2_FACEBOOK_CLIENT_SECRET} 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