feat:支持授权参数加密
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.central.oauth.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@@ -3,19 +3,21 @@ package com.central.oauth.filter;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.context.TenantContextHolder;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.oauth.handler.decryptParamHandler.IDecryptParamHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
|
||||
import org.springframework.security.oauth2.common.util.OAuth2Utils;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -34,17 +36,23 @@ import java.util.Map;
|
||||
@Component
|
||||
@Aspect
|
||||
public class OauthTokenAspect {
|
||||
@Resource
|
||||
private IDecryptParamHandler decryptParamHandler;
|
||||
|
||||
@Around("execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
|
||||
public Object handleControllerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
try {
|
||||
Object[] args = joinPoint.getArgs();
|
||||
Principal principal = (Principal) args[0];
|
||||
if (!(principal instanceof Authentication)) {
|
||||
throw new InsufficientAuthenticationException(
|
||||
throw new OAuth2Exception(
|
||||
"There is no client authentication. Try adding an appropriate authentication filter.");
|
||||
}
|
||||
String clientId = getClientId(principal);
|
||||
Map<String, String> parameters = (Map<String, String>) args[1];
|
||||
//解密参数
|
||||
decryptParamHandler.decryptParams(parameters);
|
||||
|
||||
String grantType = parameters.get(OAuth2Utils.GRANT_TYPE);
|
||||
if (!parameters.containsKey(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME)) {
|
||||
parameters.put(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME, SecurityConstants.DEF_ACCOUNT_TYPE);
|
||||
@@ -74,7 +82,7 @@ public class OauthTokenAspect {
|
||||
private String getClientId(Principal principal) {
|
||||
Authentication client = (Authentication) principal;
|
||||
if (!client.isAuthenticated()) {
|
||||
throw new InsufficientAuthenticationException("The client is not authenticated.");
|
||||
throw new OAuth2Exception("The client is not authenticated.");
|
||||
}
|
||||
String clientId = client.getName();
|
||||
if (client instanceof OAuth2Authentication) {
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.central.oauth.handler.decryptParamHandler;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 解密参数的处理接口
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2022/12/29
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public interface IDecryptParamHandler {
|
||||
String SEPARATOR = ";";
|
||||
/**
|
||||
* 需要解密的参数名,多个以 ; 隔离
|
||||
* 例如:decrypt_param=username;password
|
||||
*/
|
||||
String PARAM_KEY_DECRYPT_PARAM = "decrypt_param";
|
||||
|
||||
/**
|
||||
* 根据 parameters里面decrypt_param 的值进行参数解密,并把解密后的值替换原值
|
||||
* @param parameters 参数集合
|
||||
*/
|
||||
default void decryptParams(Map<String, String> parameters) {
|
||||
if (CollUtil.isNotEmpty(parameters)) {
|
||||
//从 parameters 中获取 decrypt_param 参数值
|
||||
String decryptParam = parameters.get(PARAM_KEY_DECRYPT_PARAM);
|
||||
//如果参数decrypt_param有值,则进行参数解密
|
||||
if (StrUtil.isNotEmpty(decryptParam)) {
|
||||
String[] paramNames = decryptParam.split(SEPARATOR);
|
||||
this.decrypt(paramNames, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密并更新参数值
|
||||
* @param paramNames 需要解密的参数名
|
||||
* @param parameters 参数集合(参数名:参数值)
|
||||
*/
|
||||
void decrypt(String[] paramNames, Map<String, String> parameters);
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.central.oauth.handler.decryptParamHandler;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import com.central.oauth2.common.properties.AuthProperties;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 使用rsa算法对参数进行解密
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2022/12/29
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Service
|
||||
public class RsaDecryptParamHandler implements IDecryptParamHandler {
|
||||
private final AuthProperties authProperties;
|
||||
|
||||
public RsaDecryptParamHandler(SecurityProperties securityProperties) {
|
||||
this.authProperties = securityProperties.getAuth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrypt(String[] paramNames, Map<String, String> parameters) {
|
||||
RSA rsaTools = SecureUtil.rsa(authProperties.getDecryptParamPrivateKey(), null);
|
||||
String value;
|
||||
for (String param : paramNames) {
|
||||
value = parameters.get(param);
|
||||
if (StrUtil.isNotEmpty(value)) {
|
||||
//解密
|
||||
value = this.decrypt(rsaTools, value, param);
|
||||
//更新参数为解密后的值
|
||||
parameters.put(param, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String decrypt(RSA rsaTools, String value, String param) {
|
||||
try {
|
||||
return rsaTools.decryptStr(value, KeyType.PrivateKey);
|
||||
} catch (Exception e) {
|
||||
throw new OAuth2Exception("参数 " + param + " 解密失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user