From 4302831c91630d3467d8b1022f16dff0ff363988 Mon Sep 17 00:00:00 2001 From: Matt Warman Date: Mon, 10 Dec 2018 14:44:47 -0500 Subject: [PATCH] See #62. Use Optional wrappers for AccountService --- .../ws/repository/AccountRepository.java | 15 +++++++------- .../security/AccountUserDetailsService.java | 20 ++++++++----------- .../leanstacks/ws/service/AccountService.java | 6 ++++-- .../ws/service/AccountServiceBean.java | 8 +++++--- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/leanstacks/ws/repository/AccountRepository.java b/src/main/java/com/leanstacks/ws/repository/AccountRepository.java index f0df7fd..6c29937 100644 --- a/src/main/java/com/leanstacks/ws/repository/AccountRepository.java +++ b/src/main/java/com/leanstacks/ws/repository/AccountRepository.java @@ -1,15 +1,16 @@ package com.leanstacks.ws.repository; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.leanstacks.ws.model.Account; /** - * The AccountRepository interface is a Spring Data JPA data repository for - * Account entities. The AccountRepository provides all the data access - * behaviors exposed by JpaRepository and additional custom - * behaviors may be defined in this interface. + * The AccountRepository interface is a Spring Data JPA data repository for Account entities. The AccountRepository + * provides all the data access behaviors exposed by JpaRepository and additional custom behaviors may be + * defined in this interface. * * @author Matt Warman */ @@ -17,11 +18,11 @@ public interface AccountRepository extends JpaRepository { /** - * Query for a single Account entities by username. + * Query for a single Account entity by username. * * @param username The username value to query the repository. - * @return An Account or null if none found. + * @return An Optional Account. */ - Account findByUsername(String username); + Optional findByUsername(String username); } diff --git a/src/main/java/com/leanstacks/ws/security/AccountUserDetailsService.java b/src/main/java/com/leanstacks/ws/security/AccountUserDetailsService.java index 9e95090..c74fbc3 100644 --- a/src/main/java/com/leanstacks/ws/security/AccountUserDetailsService.java +++ b/src/main/java/com/leanstacks/ws/security/AccountUserDetailsService.java @@ -1,8 +1,8 @@ package com.leanstacks.ws.security; -import java.util.ArrayList; -import java.util.Collection; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,11 +43,9 @@ public class AccountUserDetailsService implements UserDetailsService { public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { logger.info("> loadUserByUsername {}", username); - final Account account = accountService.findByUsername(username); - if (account == null) { - // Not found... - throw new UsernameNotFoundException("Invalid credentials."); - } + final Optional accountOptional = accountService.findByUsername(username); + final Account account = accountOptional + .orElseThrow(() -> new UsernameNotFoundException("Invalid credentials.")); final Set roles = account.getRoles(); if (roles == null || roles.isEmpty()) { @@ -55,13 +53,11 @@ public UserDetails loadUserByUsername(final String username) throws UsernameNotF throw new UsernameNotFoundException("Invalid credentials."); } - final Collection grantedAuthorities = new ArrayList(); - for (final Role role : roles) { - grantedAuthorities.add(new SimpleGrantedAuthority(role.getCode())); - } + final Set authorities = roles.stream().map(role -> new SimpleGrantedAuthority(role.getCode())) + .collect(Collectors.toSet()); final User userDetails = new User(account.getUsername(), account.getPassword(), account.isEnabled(), - !account.isExpired(), !account.isCredentialsexpired(), !account.isLocked(), grantedAuthorities); + !account.isExpired(), !account.isCredentialsexpired(), !account.isLocked(), authorities); logger.info("< loadUserByUsername {}", username); return userDetails; diff --git a/src/main/java/com/leanstacks/ws/service/AccountService.java b/src/main/java/com/leanstacks/ws/service/AccountService.java index b325afb..2569edb 100644 --- a/src/main/java/com/leanstacks/ws/service/AccountService.java +++ b/src/main/java/com/leanstacks/ws/service/AccountService.java @@ -1,5 +1,7 @@ package com.leanstacks.ws.service; +import java.util.Optional; + import com.leanstacks.ws.model.Account; /** @@ -19,8 +21,8 @@ public interface AccountService { * Find an Account by the username attribute value. * * @param username A String username to query the repository. - * @return An Account instance or null if none found. + * @return An Optional wrapped Account. */ - Account findByUsername(String username); + Optional findByUsername(String username); } diff --git a/src/main/java/com/leanstacks/ws/service/AccountServiceBean.java b/src/main/java/com/leanstacks/ws/service/AccountServiceBean.java index 4367b5b..62ce6de 100644 --- a/src/main/java/com/leanstacks/ws/service/AccountServiceBean.java +++ b/src/main/java/com/leanstacks/ws/service/AccountServiceBean.java @@ -1,5 +1,7 @@ package com.leanstacks.ws.service; +import java.util.Optional; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -29,13 +31,13 @@ public class AccountServiceBean implements AccountService { private transient AccountRepository accountRepository; @Override - public Account findByUsername(final String username) { + public Optional findByUsername(final String username) { logger.info("> findByUsername"); - final Account account = accountRepository.findByUsername(username); + final Optional accountOptional = accountRepository.findByUsername(username); logger.info("< findByUsername"); - return account; + return accountOptional; } }