| | |
| | | package com.moral.security.auth.login; |
| | | |
| | | import com.moral.entity.Account; |
| | | import com.moral.entity.Role; |
| | | import com.moral.security.exceptions.AccountExpiredBadCredentialsException; |
| | | import com.moral.security.model.UserContext; |
| | | import com.moral.service.AccountService; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.authentication.AuthenticationProvider; |
| | | import org.springframework.security.authentication.BadCredentialsException; |
| | |
| | | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | |
| | | String password = (String) authentication.getCredentials(); |
| | | LoginMode mode = (LoginMode) authentication.getDetails(); |
| | | Account account = accountService.queryAccountByName(accountName).orElseThrow(() -> new UsernameNotFoundException("User not found: " + accountName)); |
| | | List<Role> roleList = accountService.getRolesByAccountName(accountName); |
| | | Date expireTime = Optional.ofNullable(account.getExpireTime()) |
| | | .orElseThrow( |
| | | ()-> new AccountExpiredBadCredentialsException("Authentication Failed. Account has expired.") |
| | |
| | | throw new BadCredentialsException("Authentication Failed. Username or Password not valid."); |
| | | } |
| | | |
| | | if (account.getRoles() == null) { |
| | | throw new InsufficientAuthenticationException("User has no roles assigned"); |
| | | if (CollectionUtils.isEmpty(roleList)) { |
| | | throw new InsufficientAuthenticationException("Authentication Failed. User has no roles assigned"); |
| | | } |
| | | List<GrantedAuthority> authorities = account.getRoles().stream() |
| | | .map(authority -> new SimpleGrantedAuthority(authority.getName())) |
| | | List<GrantedAuthority> authorities = roleList.stream() |
| | | .map(authority -> new SimpleGrantedAuthority(authority.getRoleName())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | UserContext userContext = UserContext.create(account.getAccountName(),mode,account.getOrganizationId(),authorities); |