Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates Micronaut to 2.5.5 #253

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions generators/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ module.exports = {
},
},
versions: {
micronaut: '2.4.4',
micronautData: '2.3.1',
micronautOpenApi: '2.3.1',
micronaut: '2.5.9',
micronautData: '2.4.7',
micronautOpenApi: '2.4.0',

hibernate: '5.4.24.Final',
jackson: '2.12.2',
javassist: '3.27.0-GA', // Should match Hibernate deps
javaxMail: '1.6.2',
jbcrypt: '0.4',
jhipsterDeps: '3.8.0',
liquibase: '4.2.2',
liquibaseHibernate5: '4.1.1',
liquibase: '4.3.4',
liquibaseHibernate5: '4.3.5',
logback: '1.2.3',
mapstruct: '1.3.1.Final',
swaggerAnnotations: '2.1.6',
mockito: '3.6.28',
problem: '0.24.0',
caffeine: '2.8.8',
problem: '0.25.0',
caffeine: '2.9.0',
archunit: '0.14.1',
simplejavamail: '6.4.4',
testcontainers: '1.15.2',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.ehcache.jsr107.EhcacheCachingProvider;
<%_ if (enableHibernateCache) { _%>
import org.hibernate.cache.jcache.ConfigSettings;
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ if (cacheProvider === 'caffeine') { _%>
import com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider;
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class CacheConfiguration {
.build());
<%_ } else if (cacheProvider === 'caffeine') { _%>
JHipsterProperties.Cache.Caffeine caffeine = jHipsterProperties.getCache().getCaffeine();

CaffeineConfiguration<Object, Object> caffeineConfiguration = new CaffeineConfiguration<>();
caffeineConfiguration.setMaximumSize(OptionalLong.of(caffeine.getMaxEntries()));
caffeineConfiguration.setExpireAfterWrite(OptionalLong.of(TimeUnit.SECONDS.toNanos(caffeine.getTimeToLiveSeconds())));
Expand All @@ -73,7 +73,7 @@ public class CacheConfiguration {
MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();

URI redisUri = URI.create(jHipsterProperties.getCache().getRedis().getServer()[0]);

Config config = new Config();
if (jHipsterProperties.getCache().getRedis().isCluster()) {
ClusterServersConfig clusterServersConfig = config
Expand All @@ -82,7 +82,7 @@ public class CacheConfiguration {
.setMasterConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
.setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
.addNodeAddress(jHipsterProperties.getCache().getRedis().getServer());

if (redisUri.getUserInfo() != null) {
clusterServersConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
}
Expand All @@ -93,7 +93,7 @@ public class CacheConfiguration {
.setConnectionMinimumIdleSize(jHipsterProperties.getCache().getRedis().getConnectionMinimumIdleSize())
.setSubscriptionConnectionPoolSize(jHipsterProperties.getCache().getRedis().getSubscriptionConnectionPoolSize())
.setAddress(jHipsterProperties.getCache().getRedis().getServer()[0]);

if (redisUri.getUserInfo() != null) {
singleServerConfig.setPassword(redisUri.getUserInfo().substring(redisUri.getUserInfo().indexOf(':') + 1));
}
Expand Down Expand Up @@ -123,6 +123,7 @@ public class CacheConfiguration {
<%_ if (!skipUserManagement || (authenticationType === 'oauth2' && databaseType !== 'no')) { _%>
createCache(cm, <%=packageName%>.repository.UserRepository.USERS_BY_LOGIN_CACHE);
createCache(cm, <%=packageName%>.repository.UserRepository.USERS_BY_EMAIL_CACHE);
createCache(cm, <%=packageName%>.repository.UserRepository.USERS_BY_EMAIL_OR_LOGIN_CACHE);
<%_ if (enableHibernateCache) { _%>
createCache(cm, <%=packageName%>.domain.User.class.getName());
createCache(cm, <%=packageName%>.domain.Authority.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public interface UserRepository extends JpaRepository<User, <% if (authenticatio

<%_ if (usesCache) { _%>
public static String USERS_BY_LOGIN_CACHE = "usersByLogin";

public static String USERS_BY_EMAIL_CACHE = "usersByEmail";
public static String USERS_BY_EMAIL_OR_LOGIN_CACHE = "usersByEmailOrLogin";
<%_ } _%>
<% if (authenticationType !== 'oauth2') { %>

Expand Down Expand Up @@ -55,6 +55,12 @@ public interface UserRepository extends JpaRepository<User, <% if (authenticatio
<%_ } _%>
public Optional<User> findOneByEmail(String email);

@EntityGraph(attributePaths = "authorities")
<%_ if (usesCache) { _%>
@Cacheable(cacheNames = "usersByEmailOrLogin")
<%_ } _%>
public Optional<User> findOneByEmailEqualsOrLoginEquals(String email, String login);

public Page<User> findAllByLoginNot(String login, Pageable pageable);

public void update(@Id <% if (authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %> id, Instant createdDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@ import <%=packageName%>.domain.User;
import <%=packageName%>.repository.UserRepository;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.http.HttpRequest;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.security.authentication.AuthenticationProvider;
import io.micronaut.security.authentication.AuthenticationRequest;
import io.micronaut.security.authentication.AuthenticationResponse;
import io.micronaut.security.authentication.UserDetails;
import io.micronaut.validation.validator.constraints.EmailValidator;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;

import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Named;
import javax.inject.Singleton;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@Singleton
public class DatabaseAuthenticationProvider implements AuthenticationProvider {

private final Logger log = LoggerFactory.getLogger(DatabaseAuthenticationProvider.class);

private static final NotAuthenticatedResponse BAD_ATTEMPT = new NotAuthenticatedResponse("Invalid username or password");

private final Scheduler scheduler;
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

public DatabaseAuthenticationProvider(UserRepository userRepository, PasswordEncoder passwordEncoder) {
public DatabaseAuthenticationProvider(@Named(TaskExecutors.IO) ExecutorService executorService, UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.scheduler = Schedulers.from(executorService);
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
Expand All @@ -38,18 +50,20 @@ public class DatabaseAuthenticationProvider implements AuthenticationProvider {

log.debug("Authenticating {}", username);

if (new EmailValidator().isValid(username, null)) {
return Flowable.just(userRepository.findOneByEmail(username)
String lowercaseLogin = username.toLowerCase(Locale.ENGLISH);
return Flowable.<AuthenticationResponse>create(emitter -> {
Optional<AuthenticationResponse> result = userRepository.findOneByEmailEqualsOrLoginEquals(username, lowercaseLogin)
.filter(user -> passwordEncoder.matches(authenticationRequest.getSecret().toString(), user.getPassword()))
.map(user -> createMicronautSecurityUser(username, user))
.orElse(new NotAuthenticatedResponse("Invalid username or password")));
}
.map(user -> createMicronautSecurityUser(lowercaseLogin, user));

String lowercaseLogin = username.toLowerCase(Locale.ENGLISH);
return Flowable.just(userRepository.findOneByLogin(lowercaseLogin)
.filter(user -> passwordEncoder.matches(authenticationRequest.getSecret().toString(), user.getPassword()))
.map(user -> createMicronautSecurityUser(lowercaseLogin, user))
.orElse(new NotAuthenticatedResponse("Invalid username or password")));
if (result.isPresent()) {
emitter.onNext(result.get());
} else {
emitter.onNext(BAD_ATTEMPT);
}

emitter.onComplete();
}, BackpressureStrategy.ERROR).subscribeOn(scheduler);
}

private AuthenticationResponse createMicronautSecurityUser(String lowercaseLogin, User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ eureka:
liquibase:
datasources:
default:
async: true
async: false
change-log: classpath:config/liquibase/master.xml
contexts: dev,faker

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jpa:
liquibase:
datasources:
default:
async: true
async: false
change-log: classpath:config/liquibase/master.xml
contexts: prod
mail:
Expand Down