mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(auth)Avoid printing error logs when key decryption and token parsing fail (#1681)
* (improvement)(auth) Avoid throwing exceptions after login token authentication fails #1679 (improvement)(common) Do not print error log when key decryption fails #1679 --------- Co-authored-by: lxwcodemonkey
This commit is contained in:
@@ -33,6 +33,10 @@ public class User {
|
|||||||
return new User(1L, "admin", "admin", "admin@email", 1);
|
return new User(1L, "admin", "admin", "admin@email", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static User getVisitUser() {
|
||||||
|
return new User(1L, "visit", "visit", "visit@email", 0);
|
||||||
|
}
|
||||||
|
|
||||||
public static User getAppUser(int appId) {
|
public static User getAppUser(int appId) {
|
||||||
String name = String.format("app_%s", appId);
|
String name = String.format("app_%s", appId);
|
||||||
return new User(1L, name, name, "", 1);
|
return new User(1L, name, name, "", 1);
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import com.tencent.supersonic.common.util.ContextUtils;
|
|||||||
import com.tencent.supersonic.common.util.S2ThreadContext;
|
import com.tencent.supersonic.common.util.S2ThreadContext;
|
||||||
import com.tencent.supersonic.common.util.ThreadContext;
|
import com.tencent.supersonic.common.util.ThreadContext;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -61,7 +60,7 @@ public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor
|
|||||||
}
|
}
|
||||||
|
|
||||||
UserWithPassword user = userTokenUtils.getUserWithPassword(request);
|
UserWithPassword user = userTokenUtils.getUserWithPassword(request);
|
||||||
if (StringUtils.isNotBlank(user.getName())) {
|
if (user != null) {
|
||||||
setContext(user.getName(), request);
|
setContext(user.getName(), request);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_CREATE_TIME;
|
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_CREATE_TIME;
|
||||||
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_IS_ADMIN;
|
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_IS_ADMIN;
|
||||||
@@ -68,13 +69,13 @@ public class UserTokenUtils {
|
|||||||
|
|
||||||
public User getUser(HttpServletRequest request) {
|
public User getUser(HttpServletRequest request) {
|
||||||
String token = request.getHeader(authenticationConfig.getTokenHttpHeaderKey());
|
String token = request.getHeader(authenticationConfig.getTokenHttpHeaderKey());
|
||||||
final Claims claims = getClaims(token, request);
|
final Optional<Claims> claimsOptional = getClaims(token, request);
|
||||||
return getUser(claims);
|
return claimsOptional.map(this::getUser).orElse(User.getVisitUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser(String token, String appKey) {
|
public User getUser(String token, String appKey) {
|
||||||
final Claims claims = getClaims(token, appKey);
|
final Optional<Claims> claimsOptional = getClaims(token, appKey);
|
||||||
return getUser(claims);
|
return claimsOptional.map(this::getUser).orElse(User.getVisitUser());
|
||||||
}
|
}
|
||||||
|
|
||||||
private User getUser(Claims claims) {
|
private User getUser(Claims claims) {
|
||||||
@@ -92,11 +93,13 @@ public class UserTokenUtils {
|
|||||||
public UserWithPassword getUserWithPassword(HttpServletRequest request) {
|
public UserWithPassword getUserWithPassword(HttpServletRequest request) {
|
||||||
String token = request.getHeader(authenticationConfig.getTokenHttpHeaderKey());
|
String token = request.getHeader(authenticationConfig.getTokenHttpHeaderKey());
|
||||||
if (StringUtils.isBlank(token)) {
|
if (StringUtils.isBlank(token)) {
|
||||||
String message = "token is blank, get user failed";
|
return null;
|
||||||
log.warn("{}, uri: {}", message, request.getServletPath());
|
|
||||||
throw new AccessException(message);
|
|
||||||
}
|
}
|
||||||
final Claims claims = getClaims(token, request);
|
final Optional<Claims> claimsOptional = getClaims(token, request);
|
||||||
|
if (!claimsOptional.isPresent()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final Claims claims = claimsOptional.get();
|
||||||
Long userId = Long.parseLong(claims.getOrDefault(TOKEN_USER_ID, 0).toString());
|
Long userId = Long.parseLong(claims.getOrDefault(TOKEN_USER_ID, 0).toString());
|
||||||
String userName = String.valueOf(claims.get(TOKEN_USER_NAME));
|
String userName = String.valueOf(claims.get(TOKEN_USER_NAME));
|
||||||
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
|
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
|
||||||
@@ -109,32 +112,25 @@ public class UserTokenUtils {
|
|||||||
return UserWithPassword.get(userId, userName, displayName, email, password, isAdmin);
|
return UserWithPassword.get(userId, userName, displayName, email, password, isAdmin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Claims getClaims(String token, HttpServletRequest request) {
|
private Optional<Claims> getClaims(String token, HttpServletRequest request) {
|
||||||
Claims claims;
|
String appKey = getAppKey(request);
|
||||||
try {
|
return getClaims(token, appKey);
|
||||||
String appKey = getAppKey(request);
|
|
||||||
claims = getClaims(token, appKey);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new AccessException("parse user info from token failed :" + token);
|
|
||||||
}
|
|
||||||
return claims;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Claims getClaims(String token, String appKey) {
|
private Optional<Claims> getClaims(String token, String appKey) {
|
||||||
Claims claims;
|
|
||||||
try {
|
try {
|
||||||
String tokenSecret = getTokenSecret(appKey);
|
String tokenSecret = getTokenSecret(appKey);
|
||||||
claims =
|
Claims claims =
|
||||||
Jwts.parser()
|
Jwts.parser()
|
||||||
.setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8))
|
.setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8))
|
||||||
.build()
|
.build()
|
||||||
.parseClaimsJws(getTokenString(token))
|
.parseClaimsJws(getTokenString(token))
|
||||||
.getBody();
|
.getBody();
|
||||||
|
return Optional.of(claims);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("getClaims", e);
|
log.info("can not getClaims from appKey:{} token:{}, please login", appKey, token);
|
||||||
throw new AccessException("parse user info from token failed :" + token);
|
|
||||||
}
|
}
|
||||||
return claims;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getTokenString(String token) {
|
private static String getTokenString(String token) {
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ public class AESEncryptionUtil {
|
|||||||
byte[] decryptedBytes = cipher.doFinal(encryptBytes);
|
byte[] decryptedBytes = cipher.doFinal(encryptBytes);
|
||||||
return new String(decryptedBytes, ENCODE);
|
return new String(decryptedBytes, ENCODE);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("encryptStr decrypt failed:{}", encryptStr);
|
|
||||||
return encryptStr;
|
return encryptStr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ public class RestExceptionHandler {
|
|||||||
@ExceptionHandler(AccessException.class)
|
@ExceptionHandler(AccessException.class)
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public ResultData<String> accessException(Exception e) {
|
public ResultData<String> accessException(Exception e) {
|
||||||
log.error("default global exception", e);
|
|
||||||
return ResultData.fail(ReturnCode.ACCESS_ERROR.getCode(), e.getMessage());
|
return ResultData.fail(ReturnCode.ACCESS_ERROR.getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user