mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
change travis java 9 to 11
This commit is contained in:
parent
026fcd0509
commit
c69831ed18
9 changed files with 93 additions and 27 deletions
|
@ -2,8 +2,8 @@ language: java
|
||||||
sudo: true
|
sudo: true
|
||||||
dist: trusty
|
dist: trusty
|
||||||
|
|
||||||
# use Java 9
|
# use Java 11
|
||||||
jdk: oraclejdk9
|
jdk: oraclejdk11
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- chmod +x mvnw
|
- chmod +x mvnw
|
||||||
|
|
|
@ -13,6 +13,22 @@ public final class CommonAttribute {
|
||||||
|
|
||||||
public static final String EMAIL = "email";
|
public static final String EMAIL = "email";
|
||||||
|
|
||||||
|
public static final String GITHUB = "github";
|
||||||
|
|
||||||
|
public static final String GITHUB_FIRSTNAME = "login";
|
||||||
|
|
||||||
|
public static final String GITHUB_LASTNAME = "name";
|
||||||
|
|
||||||
|
public static final String GITHUB_PROVIDER_ID = "id";
|
||||||
|
|
||||||
|
public static final String GOOGLE = "google";
|
||||||
|
|
||||||
|
public static final String GOOGLE_FIRSTNAME = "given_name";
|
||||||
|
|
||||||
|
public static final String GOOGLE_LASTNAME = "family_name";
|
||||||
|
|
||||||
|
public static final String GOOGLE_PROVIDER_ID = "sub";
|
||||||
|
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
|
|
||||||
public static final String NEW = "new";
|
public static final String NEW = "new";
|
||||||
|
|
|
@ -35,7 +35,7 @@ class CacheConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
||||||
return (cm) -> {
|
return cm -> {
|
||||||
if (cm.getCache("vets") == null) {
|
if (cm.getCache("vets") == null) {
|
||||||
cm.createCache("vets", cacheConfiguration());
|
cm.createCache("vets", cacheConfiguration());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package org.springframework.samples.petclinic.configuration;
|
package org.springframework.samples.petclinic.configuration;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
@ -13,7 +12,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
|
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
|
import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService;
|
||||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
||||||
|
@ -33,9 +31,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
private static final String CLIENT_PROPERTY_KEY = "spring.security.oauth2.client.registration.";
|
private static final String CLIENT_PROPERTY_KEY = "spring.security.oauth2.client.registration.";
|
||||||
|
|
||||||
// @Autowired
|
|
||||||
// private UserDetailsService userDetailsService;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.samples.petclinic.controller.common;
|
package org.springframework.samples.petclinic.controller.common;
|
||||||
|
|
||||||
|
import org.springframework.samples.petclinic.exception.FunctionalException;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
@ -29,8 +30,8 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||||
class CrashController {
|
class CrashController {
|
||||||
|
|
||||||
@GetMapping("/oups")
|
@GetMapping("/oups")
|
||||||
public String triggerException() {
|
public String triggerException() throws FunctionalException {
|
||||||
throw new RuntimeException(
|
throw new FunctionalException(
|
||||||
"Expected: controller used to showcase what " + "happens when an exception is thrown");
|
"Expected: controller used to showcase what " + "happens when an exception is thrown");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -156,18 +156,33 @@ public class UserController extends WebSocketSender {
|
||||||
|
|
||||||
@GetMapping(CommonEndPoint.OAUTH2_SUCCESS)
|
@GetMapping(CommonEndPoint.OAUTH2_SUCCESS)
|
||||||
public String postLoginOAUTH2(Model model, OAuth2AuthenticationToken authentication) {
|
public String postLoginOAUTH2(Model model, OAuth2AuthenticationToken authentication) {
|
||||||
String firstName = authentication.getPrincipal().getAttribute("given_name");
|
String firstName;
|
||||||
String lastName = authentication.getPrincipal().getAttribute("family_name");
|
String lastName;
|
||||||
|
String email;
|
||||||
|
String providerId;
|
||||||
|
String provider = authentication.getAuthorizedClientRegistrationId();
|
||||||
|
|
||||||
|
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);
|
||||||
|
providerId = String.valueOf(authentication.getPrincipal().getAttribute(CommonAttribute.GITHUB_PROVIDER_ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
email = authentication.getPrincipal().getAttribute("email");
|
||||||
|
|
||||||
CredentialDTO credential = credentialService.findByAuthentication(authentication);
|
CredentialDTO credential = credentialService.findByAuthentication(authentication);
|
||||||
|
|
||||||
|
UserDTO user = userService.findByEmail(email);
|
||||||
|
|
||||||
if (credential.isNew()) {
|
if (credential.isNew()) {
|
||||||
|
|
||||||
// first time authentification with this provider
|
// first time authentification with this provider
|
||||||
credential = credentialService.saveNew(authentication);
|
credential = credentialService.saveNew(provider, email, providerId);
|
||||||
String email = credential.getEmail();
|
|
||||||
|
|
||||||
UserDTO user = userService.findByEmail(email);
|
|
||||||
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = new UserDTO();
|
user = new UserDTO();
|
||||||
|
@ -200,14 +215,14 @@ public class UserController extends WebSocketSender {
|
||||||
credential.setToken("");
|
credential.setToken("");
|
||||||
credential.setVerified(true);
|
credential.setVerified(true);
|
||||||
credentialService.save(credential);
|
credentialService.save(credential);
|
||||||
securityService.autoLogin(credential.getEmail(), credential.getPassword());
|
securityService.autoLogin(user.getEmail(), user.getPassword());
|
||||||
String message = String.format(CommonWebSocket.USER_LOGGED_IN, firstName, lastName);
|
String message = String.format(CommonWebSocket.USER_LOGGED_IN, firstName, lastName);
|
||||||
sendSuccessMessage(message);
|
sendSuccessMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (Boolean.TRUE.equals(credential.isVerified())) {
|
else if (Boolean.TRUE.equals(credential.isVerified())) {
|
||||||
securityService.autoLogin(credential.getEmail(), credential.getPassword());
|
securityService.autoLogin(user.getEmail(), user.getPassword());
|
||||||
String message = String.format(CommonWebSocket.USER_LOGGED_IN, firstName, lastName);
|
String message = String.format(CommonWebSocket.USER_LOGGED_IN, firstName, lastName);
|
||||||
sendSuccessMessage(message);
|
sendSuccessMessage(message);
|
||||||
}
|
}
|
||||||
|
@ -215,7 +230,7 @@ public class UserController extends WebSocketSender {
|
||||||
return CommonView.HOME;
|
return CommonView.HOME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = CommonEndPoint.CONFIRM_ACCOUNT, method = { RequestMethod.GET, RequestMethod.POST })
|
@GetMapping(CommonEndPoint.CONFIRM_ACCOUNT)
|
||||||
public String confirmUserAccount(@RequestParam(CommonAttribute.TOKEN) String token, Model model) {
|
public String confirmUserAccount(@RequestParam(CommonAttribute.TOKEN) String token, Model model) {
|
||||||
CredentialDTO credential = credentialService.findByToken(token);
|
CredentialDTO credential = credentialService.findByToken(token);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package org.springframework.samples.petclinic.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classe des Exceptions Fonctionnelles
|
||||||
|
*/
|
||||||
|
public class FunctionalException extends Exception {
|
||||||
|
|
||||||
|
/** serialVersionUID */
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
|
||||||
|
// ==================== Constructeurs ====================
|
||||||
|
/**
|
||||||
|
* Constructeur.
|
||||||
|
*
|
||||||
|
* @param pMessage -
|
||||||
|
*/
|
||||||
|
public FunctionalException(String pMessage) {
|
||||||
|
super(pMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur.
|
||||||
|
*
|
||||||
|
* @param pCause -
|
||||||
|
*/
|
||||||
|
public FunctionalException(Throwable pCause) {
|
||||||
|
super(pCause);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur.
|
||||||
|
*
|
||||||
|
* @param pMessage -
|
||||||
|
* @param pCause -
|
||||||
|
*/
|
||||||
|
public FunctionalException(String pMessage, Throwable pCause) {
|
||||||
|
super(pMessage, pCause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -104,15 +104,14 @@ public class CredentialService {
|
||||||
return entityToDTO(credential);
|
return entityToDTO(credential);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CredentialDTO saveNew(OAuth2AuthenticationToken authentication) {
|
public CredentialDTO saveNew(String provider, String email, String providerId) {
|
||||||
Credential credential = new Credential();
|
Credential credential = new Credential();
|
||||||
|
|
||||||
AuthProvider authProvider = authProviderRepository
|
AuthProvider authProvider = authProviderRepository.findByName(provider);
|
||||||
.findByName(authentication.getAuthorizedClientRegistrationId());
|
|
||||||
|
|
||||||
credential.setEmail(authentication.getPrincipal().getAttribute("email"));
|
credential.setEmail(email);
|
||||||
credential.setProviderId(authProvider.getId());
|
credential.setProviderId(authProvider.getId());
|
||||||
credential.setPassword(authentication.getPrincipal().getAttribute("sub"));
|
credential.setPassword(providerId);
|
||||||
credential.setVerified(false);
|
credential.setVerified(false);
|
||||||
credential.setToken();
|
credential.setToken();
|
||||||
credential.setExpiration();
|
credential.setExpiration();
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package org.springframework.samples.petclinic.service.common;
|
package org.springframework.samples.petclinic.service.common;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -12,7 +14,7 @@ import org.springframework.stereotype.Service;
|
||||||
*
|
*
|
||||||
* @author Paul-Emmanuel DOS SANTOS FACAO
|
* @author Paul-Emmanuel DOS SANTOS FACAO
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service("SecurityService")
|
@Service("SecurityService")
|
||||||
public class SecurityServiceImpl implements SecurityService {
|
public class SecurityServiceImpl implements SecurityService {
|
||||||
|
|
||||||
|
@ -44,8 +46,6 @@ public class SecurityServiceImpl implements SecurityService {
|
||||||
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
|
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
|
||||||
userDetails, motDePasse, userDetails.getAuthorities());
|
userDetails, motDePasse, userDetails.getAuthorities());
|
||||||
|
|
||||||
authenticationManager.authenticate(usernamePasswordAuthenticationToken);
|
|
||||||
|
|
||||||
if (usernamePasswordAuthenticationToken.isAuthenticated()) {
|
if (usernamePasswordAuthenticationToken.isAuthenticated()) {
|
||||||
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
|
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue