ZhuDongming
2019-08-13 1485d6d9034fd9c36527133b0361cceaf97d7f5d
src/main/java/com/moral/security/auth/login/LoginAuthenticationProvider.java
@@ -1,6 +1,7 @@
package com.moral.security.auth.login;
import com.moral.entity.Account;
import com.moral.security.exceptions.AccountExpiredBadCredentialsException;
import com.moral.security.model.UserContext;
import com.moral.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +18,9 @@
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -51,6 +54,14 @@
        String password = (String) authentication.getCredentials();
        LoginMode mode = (LoginMode) authentication.getDetails();
        Account account = accountService.queryAccountByName(accountName).orElseThrow(() -> new UsernameNotFoundException("User not found: " + accountName));
        Date expireTime = Optional.ofNullable(account.getExpireTime())
                .orElseThrow(
        ()-> new AccountExpiredBadCredentialsException("Authentication Failed. Account has expired.")
                );
        Date nowTime = new Date();
        if(expireTime.getTime()<nowTime.getTime()){
            throw new AccountExpiredBadCredentialsException("Authentication Failed. Account has expired.");
        }
        if (!encoder.matches(password, account.getPassword())) {
            throw new BadCredentialsException("Authentication Failed. Username or Password not valid.");
        }