Skip to content

Commit

Permalink
See #62. Use Optional wrappers for AccountService
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Dec 10, 2018
1 parent b38aaa6 commit 4302831
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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 <code>JpaRepository</code> 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 <code>JpaRepository</code> and additional custom behaviors may be
* defined in this interface.
*
* @author Matt Warman
*/
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {

/**
* 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 <code>null</code> if none found.
* @return An Optional Account.
*/
Account findByUsername(String username);
Optional<Account> findByUsername(String username);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,25 +43,21 @@ 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<Account> accountOptional = accountService.findByUsername(username);
final Account account = accountOptional
.orElseThrow(() -> new UsernameNotFoundException("Invalid credentials."));

final Set<Role> roles = account.getRoles();
if (roles == null || roles.isEmpty()) {
// No Roles assigned to Account...
throw new UsernameNotFoundException("Invalid credentials.");
}

final Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
for (final Role role : roles) {
grantedAuthorities.add(new SimpleGrantedAuthority(role.getCode()));
}
final Set<GrantedAuthority> 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;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/leanstacks/ws/service/AccountService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.leanstacks.ws.service;

import java.util.Optional;

import com.leanstacks.ws.model.Account;

/**
Expand All @@ -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 <code>null</code> if none found.
* @return An Optional wrapped Account.
*/
Account findByUsername(String username);
Optional<Account> findByUsername(String username);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,13 +31,13 @@ public class AccountServiceBean implements AccountService {
private transient AccountRepository accountRepository;

@Override
public Account findByUsername(final String username) {
public Optional<Account> findByUsername(final String username) {
logger.info("> findByUsername");

final Account account = accountRepository.findByUsername(username);
final Optional<Account> accountOptional = accountRepository.findByUsername(username);

logger.info("< findByUsername");
return account;
return accountOptional;
}

}

0 comments on commit 4302831

Please sign in to comment.