change travis java 9 to 11

This commit is contained in:
PEDSF 2020-12-15 19:30:03 +01:00
parent 026fcd0509
commit c69831ed18
9 changed files with 93 additions and 27 deletions

View file

@ -2,8 +2,8 @@ language: java
sudo: true
dist: trusty
# use Java 9
jdk: oraclejdk9
# use Java 11
jdk: oraclejdk11
before_install:
- chmod +x mvnw

View file

@ -13,6 +13,22 @@ public final class CommonAttribute {
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 NEW = "new";

View file

@ -35,7 +35,7 @@ class CacheConfiguration {
@Bean
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
return (cm) -> {
return cm -> {
if (cm.getCache("vets") == null) {
cm.createCache("vets", cacheConfiguration());
}

View file

@ -1,6 +1,5 @@
package org.springframework.samples.petclinic.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
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.oauth2.client.InMemoryOAuth2AuthorizedClientService;
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.";
// @Autowired
// private UserDetailsService userDetailsService;
@Resource
private Environment env;

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.controller.common;
import org.springframework.samples.petclinic.exception.FunctionalException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@ -29,8 +30,8 @@ import org.springframework.web.bind.annotation.GetMapping;
class CrashController {
@GetMapping("/oups")
public String triggerException() {
throw new RuntimeException(
public String triggerException() throws FunctionalException {
throw new FunctionalException(
"Expected: controller used to showcase what " + "happens when an exception is thrown");
}

View file

@ -156,18 +156,33 @@ public class UserController extends WebSocketSender {
@GetMapping(CommonEndPoint.OAUTH2_SUCCESS)
public String postLoginOAUTH2(Model model, OAuth2AuthenticationToken authentication) {
String firstName = authentication.getPrincipal().getAttribute("given_name");
String lastName = authentication.getPrincipal().getAttribute("family_name");
String firstName;
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);
UserDTO user = userService.findByEmail(email);
if (credential.isNew()) {
// first time authentification with this provider
credential = credentialService.saveNew(authentication);
String email = credential.getEmail();
UserDTO user = userService.findByEmail(email);
credential = credentialService.saveNew(provider, email, providerId);
if (user == null) {
user = new UserDTO();
@ -200,14 +215,14 @@ public class UserController extends WebSocketSender {
credential.setToken("");
credential.setVerified(true);
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);
sendSuccessMessage(message);
}
}
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);
sendSuccessMessage(message);
}
@ -215,7 +230,7 @@ public class UserController extends WebSocketSender {
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) {
CredentialDTO credential = credentialService.findByToken(token);

View file

@ -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);
}
}

View file

@ -104,15 +104,14 @@ public class CredentialService {
return entityToDTO(credential);
}
public CredentialDTO saveNew(OAuth2AuthenticationToken authentication) {
public CredentialDTO saveNew(String provider, String email, String providerId) {
Credential credential = new Credential();
AuthProvider authProvider = authProviderRepository
.findByName(authentication.getAuthorizedClientRegistrationId());
AuthProvider authProvider = authProviderRepository.findByName(provider);
credential.setEmail(authentication.getPrincipal().getAttribute("email"));
credential.setEmail(email);
credential.setProviderId(authProvider.getId());
credential.setPassword(authentication.getPrincipal().getAttribute("sub"));
credential.setPassword(providerId);
credential.setVerified(false);
credential.setToken();
credential.setExpiration();

View file

@ -1,8 +1,10 @@
package org.springframework.samples.petclinic.service.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
@ -12,7 +14,7 @@ import org.springframework.stereotype.Service;
*
* @author Paul-Emmanuel DOS SANTOS FACAO
*/
@Slf4j
@Service("SecurityService")
public class SecurityServiceImpl implements SecurityService {
@ -44,8 +46,6 @@ public class SecurityServiceImpl implements SecurityService {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
userDetails, motDePasse, userDetails.getAuthorities());
authenticationManager.authenticate(usernamePasswordAuthenticationToken);
if (usernamePasswordAuthenticationToken.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
}