parent
7930b85b94
commit
564dae24ba
|
@ -2,3 +2,5 @@
|
||||||
/workspace.xml
|
/workspace.xml
|
||||||
# Editor-based HTTP Client requests
|
# Editor-based HTTP Client requests
|
||||||
/httpRequests/
|
/httpRequests/
|
||||||
|
/intellij-javadocs-4.0.1.xml
|
||||||
|
/sqldialects.xml
|
||||||
|
|
|
@ -57,6 +57,7 @@ public class ConstValue {
|
||||||
ERR_LOGOUT (15, "用户未登录"),
|
ERR_LOGOUT (15, "用户未登录"),
|
||||||
ERR_TOKENTIMEOUT (16, "Token超时"),
|
ERR_TOKENTIMEOUT (16, "Token超时"),
|
||||||
ERR_TOKENNOTFOUND (17, "非法Token"),
|
ERR_TOKENNOTFOUND (17, "非法Token"),
|
||||||
|
ERR_MISSAUTHHEAD (18, "Http 请求缺少认证头部"),
|
||||||
;
|
;
|
||||||
|
|
||||||
private int errno;
|
private int errno;
|
||||||
|
|
|
@ -66,16 +66,21 @@ public class AuthController {
|
||||||
err = loginMap.keySet().iterator().next();
|
err = loginMap.keySet().iterator().next();
|
||||||
msgCtx = loginMap.get(err);
|
msgCtx = loginMap.get(err);
|
||||||
} else if (mr.getCmdId() == ConstValue.ProtocolCmdId.AUTH_LOGOUT) {
|
} else if (mr.getCmdId() == ConstValue.ProtocolCmdId.AUTH_LOGOUT) {
|
||||||
|
if(headers.get("Authorization") == null) {
|
||||||
if(headers.get("Authorization") == null
|
err = ConstValue.ErrorCode.ERR_MISSAUTHHEAD;
|
||||||
|| Objects.requireNonNull(headers.get("Authorization")).size() == 0) {
|
|
||||||
err = ConstValue.ErrorCode.ERR_LOGOUT;
|
|
||||||
} else {
|
} else {
|
||||||
EnumMap<ConstValue.ErrorCode, String> loginMap = userLogout(mr,
|
String token = Objects.
|
||||||
Objects.requireNonNull(headers.get("Authorization")).get(0));
|
requireNonNull(headers.get("Authorization"))
|
||||||
|
.get(0).replaceFirst("Bearer ", "");
|
||||||
|
|
||||||
|
err = loginService.authTokenCheck(token);
|
||||||
|
|
||||||
|
if(err == ConstValue.ErrorCode.ERR_OK) {
|
||||||
|
EnumMap<ConstValue.ErrorCode, String> loginMap = userLogout(mr, token);
|
||||||
err = loginMap.keySet().iterator().next();
|
err = loginMap.keySet().iterator().next();
|
||||||
msgCtx = loginMap.get(err);
|
msgCtx = loginMap.get(err);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}else {
|
}else {
|
||||||
err = ConstValue.ErrorCode.ERR_UNKNOWNCMD;
|
err = ConstValue.ErrorCode.ERR_UNKNOWNCMD;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +106,7 @@ public class AuthController {
|
||||||
UserLogoutReq reqInfo = objectMapper.readValue(mr.getMsgContent(), UserLogoutReq.class);
|
UserLogoutReq reqInfo = objectMapper.readValue(mr.getMsgContent(), UserLogoutReq.class);
|
||||||
rspInfo.setUserName(reqInfo.getUserName());
|
rspInfo.setUserName(reqInfo.getUserName());
|
||||||
|
|
||||||
err = loginService.logoutService(reqInfo.userName, token.replaceFirst("Bearer ", ""));
|
err = loginService.logoutService(reqInfo.userName, token);
|
||||||
|
|
||||||
rspInfo.setStatus(err.getCode());
|
rspInfo.setStatus(err.getCode());
|
||||||
rspInfo.setMessage(err.getMsg());
|
rspInfo.setMessage(err.getMsg());
|
||||||
|
|
|
@ -8,4 +8,5 @@ import java.util.EnumMap;
|
||||||
public interface LoginService {
|
public interface LoginService {
|
||||||
public EnumMap<ConstValue.ErrorCode, String> loginService(String username, String password) throws NoSuchAlgorithmException;
|
public EnumMap<ConstValue.ErrorCode, String> loginService(String username, String password) throws NoSuchAlgorithmException;
|
||||||
public ConstValue.ErrorCode logoutService(String username, String token);
|
public ConstValue.ErrorCode logoutService(String username, String token);
|
||||||
|
ConstValue.ErrorCode authTokenCheck(String token);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ public interface UserAccountCacheService {
|
||||||
void setUserPwdErrTimes(String username, Integer errTimes);
|
void setUserPwdErrTimes(String username, Integer errTimes);
|
||||||
void cleanUserToken(String username);
|
void cleanUserToken(String username);
|
||||||
ConstValue.ErrorCode verifyUserLogin(String username, String token);
|
ConstValue.ErrorCode verifyUserLogin(String username, String token);
|
||||||
|
ConstValue.ErrorCode verifyToken(String token);
|
||||||
|
|
||||||
String getCacheUser() throws JsonProcessingException;
|
String getCacheUser() throws JsonProcessingException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,10 @@ public class LoginServiceImpl implements LoginService {
|
||||||
@Resource
|
@Resource
|
||||||
private UserAccountMapper userAccountMapper;
|
private UserAccountMapper userAccountMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConstValue.ErrorCode authTokenCheck(String token) {
|
||||||
|
return userAccountService.verifyToken(token);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EnumMap<ConstValue.ErrorCode, String> loginService(String username, String password) throws NoSuchAlgorithmException {
|
public EnumMap<ConstValue.ErrorCode, String> loginService(String username, String password) throws NoSuchAlgorithmException {
|
||||||
|
|
|
@ -13,8 +13,10 @@ import org.springframework.stereotype.Service;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -25,13 +27,36 @@ public class UserAccountCacheServiceImpl implements UserAccountCacheService {
|
||||||
|
|
||||||
private ConcurrentHashMap<String, UserAccountCache> userAccountMap = new ConcurrentHashMap<>();
|
private ConcurrentHashMap<String, UserAccountCache> userAccountMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConstValue.ErrorCode verifyToken(String token) {
|
||||||
|
//userAccountMap
|
||||||
|
|
||||||
|
if(!userAccountMap.containsKey(token)){
|
||||||
|
return ConstValue.ErrorCode.ERR_LOGOUT;
|
||||||
|
} else {
|
||||||
|
UserAccountCache uc = userAccountMap.get(token);
|
||||||
|
|
||||||
|
if((System.currentTimeMillis() - uc.getLastAccess())
|
||||||
|
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS) {
|
||||||
|
return ConstValue.ErrorCode.ERR_TOKENTIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ConstValue.ErrorCode.ERR_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConstValue.ErrorCode verifyUserLogin(String username, String token) {
|
public ConstValue.ErrorCode verifyUserLogin(String username, String token) {
|
||||||
if(!userAccountMap.containsKey(username)) {
|
Optional<UserAccountCache> findRet = userAccountMap.values().stream()
|
||||||
|
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if(!findRet.isPresent()) {
|
||||||
return ConstValue.ErrorCode.ERR_USERNOTFOUND;
|
return ConstValue.ErrorCode.ERR_USERNOTFOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserAccountCache uc = userAccountMap.get(username);
|
UserAccountCache uc = findRet.get();
|
||||||
|
|
||||||
if(uc.getToken().length() == 0) {
|
if(uc.getToken().length() == 0) {
|
||||||
return ConstValue.ErrorCode.ERR_LOGOUT;
|
return ConstValue.ErrorCode.ERR_LOGOUT;
|
||||||
}
|
}
|
||||||
|
@ -56,16 +81,24 @@ public class UserAccountCacheServiceImpl implements UserAccountCacheService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanUserToken(String username) {
|
public void cleanUserToken(String username) {
|
||||||
if(userAccountMap.containsKey(username)) {
|
Optional<UserAccountCache> findRet = userAccountMap.values().stream()
|
||||||
UserAccountCache uc = userAccountMap.get(username);
|
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||||
uc.setToken("");
|
.findFirst();
|
||||||
|
|
||||||
|
if(findRet.isPresent()) {
|
||||||
|
UserAccountCache uc = findRet.get();
|
||||||
|
userAccountMap.remove(uc.getToken());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getUsrPwdErrTimes(String username) {
|
public int getUsrPwdErrTimes(String username) {
|
||||||
if(userAccountMap.containsKey(username)) {
|
Optional<UserAccountCache> findRet = userAccountMap.values().stream()
|
||||||
UserAccountCache uc = userAccountMap.get(username);
|
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if(findRet.isPresent()) {
|
||||||
|
UserAccountCache uc = findRet.get();
|
||||||
return uc.getPwdErrTimes();
|
return uc.getPwdErrTimes();
|
||||||
} else {
|
} else {
|
||||||
UserAccountCache uc = UserAccountCache.builder()
|
UserAccountCache uc = UserAccountCache.builder()
|
||||||
|
@ -75,24 +108,32 @@ public class UserAccountCacheServiceImpl implements UserAccountCacheService {
|
||||||
.pwdErrTimes(0)
|
.pwdErrTimes(0)
|
||||||
.lastAccess(System.currentTimeMillis()).build();
|
.lastAccess(System.currentTimeMillis()).build();
|
||||||
|
|
||||||
userAccountMap.put(username, uc);
|
userAccountMap.put(uc.getToken(), uc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUserPwdErrTimes(String username, Integer errTimes) {
|
public void setUserPwdErrTimes(String username, Integer errTimes) {
|
||||||
if(userAccountMap.containsKey(username)) {
|
Optional<UserAccountCache> findRet = userAccountMap.values().stream()
|
||||||
UserAccountCache uc = userAccountMap.get(username);
|
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if(findRet.isPresent()) {
|
||||||
|
UserAccountCache uc = findRet.get();
|
||||||
uc.setPwdErrTimes(Math.abs(errTimes));
|
uc.setPwdErrTimes(Math.abs(errTimes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUserToken(String username) throws NoSuchAlgorithmException {
|
public String getUserToken(String username) throws NoSuchAlgorithmException {
|
||||||
if(userAccountMap.containsKey(username)) {
|
|
||||||
UserAccountCache uc = userAccountMap.get(username);
|
Optional<UserAccountCache> findRet = userAccountMap.values().stream()
|
||||||
|
.filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if(findRet.isPresent()) {
|
||||||
|
UserAccountCache uc = findRet.get();
|
||||||
|
|
||||||
if((System.currentTimeMillis() - uc.getLastAccess())
|
if((System.currentTimeMillis() - uc.getLastAccess())
|
||||||
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS
|
>= ConstValue.GlobalConfigure.TOKEN_TIMEOUT_MS
|
||||||
|
@ -114,7 +155,7 @@ public class UserAccountCacheServiceImpl implements UserAccountCacheService {
|
||||||
.pwdErrTimes(0)
|
.pwdErrTimes(0)
|
||||||
.lastAccess(System.currentTimeMillis()).build();
|
.lastAccess(System.currentTimeMillis()).build();
|
||||||
|
|
||||||
userAccountMap.put(username, uc);
|
userAccountMap.put(uc.getToken(), uc);
|
||||||
|
|
||||||
log.info("Create {} Token:{}", username, uc.getToken());
|
log.info("Create {} Token:{}", username, uc.getToken());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue