沈斌
2018-02-08 9e1db983254a475fd7a069937c3f706fe2efe9b4
Merge remote-tracking branch 'origin/master'

# Conflicts:
# src/main/java/com/moral/security/auth/login/LoginAuthenticationProvider.java
1 files added
4 files modified
30 ■■■■■ changed files
src/main/java/com/moral/controller/ScreenController.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/security/auth/login/LoginAuthenticationProvider.java 12 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/security/auth/login/LoginAwareAuthenticationFailureHandler.java 5 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/security/common/ErrorCode.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/moral/security/exceptions/AccountExpiredBadCredentialsException.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/controller/ScreenController.java
@@ -298,6 +298,8 @@
        Account account = accountService.getAccountById(accountId);
        String regionName = areaService.selectFullNameByCode(code);
        if(account!=null&&regionName!=null){
            //去除空格
            regionName = regionName.replace(" ","");
            Object sensors = sensorService.queryAll();
            JSONObject params = new JSONObject();
            params.put("regionCode",code);
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;
@@ -19,6 +20,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -52,7 +54,15 @@
        String password = (String) authentication.getCredentials();
        LoginMode mode = (LoginMode) authentication.getDetails();
        Account account = accountService.queryAccountByName(accountName).orElseThrow(() -> new UsernameNotFoundException("User not found: " + accountName));
        if (!encoder.matches(password, account.getPassword()) || account.getExpireTime().before(new Date())) {
        Date expireTime = Optional.of(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.");
        }
src/main/java/com/moral/security/auth/login/LoginAwareAuthenticationFailureHandler.java
@@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moral.security.common.ErrorCode;
import com.moral.security.common.ErrorResponse;
import com.moral.security.exceptions.AccountExpiredBadCredentialsException;
import com.moral.security.exceptions.AuthMethodNotSupportedException;
import com.moral.security.exceptions.JwtExpiredTokenException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +41,9 @@
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        
        if (e instanceof BadCredentialsException) {
        if(e instanceof AccountExpiredBadCredentialsException){
            mapper.writeValue(response.getWriter(), ErrorResponse.of(e.getMessage(), ErrorCode.ACCOUNT_EXPIRED, HttpStatus.UNAUTHORIZED));
        }else  if (e instanceof BadCredentialsException) {
            mapper.writeValue(response.getWriter(), ErrorResponse.of("Invalid username or password", ErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
        } else if (e instanceof JwtExpiredTokenException) {
            mapper.writeValue(response.getWriter(), ErrorResponse.of("Token has expired", ErrorCode.JWT_TOKEN_EXPIRED, HttpStatus.UNAUTHORIZED));
src/main/java/com/moral/security/common/ErrorCode.java
@@ -12,7 +12,7 @@
public enum ErrorCode {
    GLOBAL(2),
    AUTHENTICATION(10), JWT_TOKEN_EXPIRED(11);
    AUTHENTICATION(10), JWT_TOKEN_EXPIRED(11),ACCOUNT_EXPIRED(12);
    
    private int errorCode;
src/main/java/com/moral/security/exceptions/AccountExpiredBadCredentialsException.java
New file
@@ -0,0 +1,9 @@
package com.moral.security.exceptions;
import org.springframework.security.core.AuthenticationException;
public class AccountExpiredBadCredentialsException extends AuthenticationException {
    public AccountExpiredBadCredentialsException(String msg) {
        super(msg);
    }
}