restful中數(shù)據(jù)權(quán)限加密方式有很多種,特別是針對(duì)于request json的方式加密,這里詳細(xì)介紹了SOA中針對(duì)于get、post數(shù)據(jù)加密方式薪贫,保證數(shù)據(jù)傳輸中的安全性:
Java代碼
@Component("dataSignInterceptor")
publicclass?DataSignInterceptorimplements?MethodInterceptor?{
@Autowired
private?SoaServiceConfigService?soaServiceConfigService;
@Autowired
private?SoaAppSecretService?soaAppSecretService;
@Autowired
private?SoaServiceApplyService?soaServiceApplyService;
public?Object?invoke(MethodInvocation?mi)throws?Throwable?{
//TODO?此處應(yīng)該先查詢(xún)緩存
//獲取所有未管控的服務(wù)列表
List?soaServiceConfigList?=?soaServiceConfigService.findNoSwitchList();
if(soaServiceConfigList?!=null){
//放入緩存
}
Object[]?ars?=?mi.getArguments();
//?判斷該方法是否加了DataSign?注解
if?(mi.getMethod().isAnnotationPresent(DataSign.class))?{
//?獲取攔截方法的請(qǐng)求參數(shù)
HttpServletRequest?request?=null;
JSONObject?jsonBody?=null;
Map?reqGetParamMap?=null;//?客戶(hù)端傳遞的參數(shù)信息
Map?reqPostParamMap?=null;//?客戶(hù)端傳遞的參數(shù)信息
Map?resultParamMap?=new?HashMap();
for?(Object?obj?:?ars)?{
if?(objinstanceof?HttpServletRequest)?{
request?=?(HttpServletRequest)?obj;
reqGetParamMap?=?(Map)?request.getParameterMap();
Set?keSet?=?reqGetParamMap.entrySet();
for?(Iterator?itr?=?keSet.iterator();?itr.hasNext();)?{
Map.Entry?me?=?(Map.Entry)?itr.next();
String?key?=?me.getKey().toString();
Object?ov?=?me.getValue();
String[]?value?=new?String[1];
if(ovinstanceof?String[]){
value=(String[])ov;
}else{
value[0]=ov.toString();
}
resultParamMap.put(key,?value[0]);
}
}elseif?(objinstanceof?JSONObject)?{
jsonBody?=?(JSONObject)?obj;
ObjectMapper?objMap?=new?ObjectMapper();
reqPostParamMap?=?objMap.readValue(jsonBody.toString(),?Map.class);
resultParamMap?=?reqPostParamMap;
}
}
String?serviceUrl?=?request.getServletPath();
if(StringUtils.isNotEmpty(serviceUrl)){
serviceUrl?=?serviceUrl.substring(serviceUrl.indexOf("/",2));
}
//TODO?應(yīng)該從緩存取,當(dāng)前先這樣處理
//如果所有未管控的服務(wù)列表中包含用戶(hù)請(qǐng)求的服務(wù)連接刻恭,則不需要再進(jìn)行管控瞧省,直接放開(kāi)服務(wù)控制權(quán)
if(soaServiceConfigList?!=null){
for(SoaServiceConfig?soaServiceConfig?:?soaServiceConfigList){
if(serviceUrl.equals(soaServiceConfig.getServiceUrl())){
return?mi.proceed();
}
}
}
String?reqSign?=?resultParamMap.get("sign");//?客戶(hù)端傳遞的簽名認(rèn)證信息
//?驗(yàn)證簽名不能為空
if?(StringUtils.isEmpty(reqSign))?{
returnnew?ResponseVO(DataSignEnum.SIGN_NOT_NULL.getCode(),?DataSignEnum.SIGN_NOT_NULL.getMessage(),
null);
}
String?appname?=?resultParamMap.get("appname");
//?驗(yàn)證應(yīng)用名不能為空
if?(StringUtils.isEmpty(appname))?{
returnnew?ResponseVO(DataSignEnum.APPNAME_NOT_NULL.getCode(),
DataSignEnum.APPNAME_NOT_NULL.getMessage(),null);
}
SoaServiceApply?soaServiceApply?=?soaServiceApplyService.getServiceApplyByAppname(serviceUrl,?appname);
if(null?==?soaServiceApply){
returnnew?ResponseVO(DataSignEnum.APPNAME_NOT_APPLY.getCode(),
DataSignEnum.APPNAME_NOT_APPLY.getMessage(),null);
}
SoaAppSecret?soaAppSecret?=?soaAppSecretService.findAppSecretByAppName(appname);
if(null?==?soaAppSecret){
returnnew?ResponseVO(DataSignEnum.APPNAME_NOT_EXISTS.getCode(),
DataSignEnum.APPNAME_NOT_EXISTS.getMessage(),null);
}
//?根據(jù)參數(shù)重新生成新的簽名sign(因?yàn)閟ign不能認(rèn)為是業(yè)務(wù)參數(shù),故將sign從map中移除后再加密)
resultParamMap.remove("sign");
resultParamMap.put("token",?soaAppSecret.getToken());
String?sign?=?MD5Utils.paramString(resultParamMap);
//?根據(jù)應(yīng)用名獲取應(yīng)用名對(duì)應(yīng)的秘鑰(目的是和url請(qǐng)求的參數(shù)一起進(jìn)行簽名認(rèn)證)
if?(!StringUtils.equals(sign,?reqSign))?{
returnnew?ResponseVO(DataSignEnum.SIGN_NOT_MATCH.getCode(),?DataSignEnum.SIGN_NOT_MATCH.getMessage(),
null);
}
}
//?執(zhí)行被攔截的方法鳍贾,切記鞍匾,如果此方法不調(diào)用,則被攔截的方法不會(huì)被執(zhí)行骑科。
return?mi.proceed();
}
/**
*?數(shù)據(jù)簽名枚舉
*
*?@author?Administrator
*/
publicenum?DataSignEnum?{
SIGN_NOT_NULL(2001,"簽名不能為空."),
SIGN_NOT_MATCH(2002,"簽名不匹配橡淑,傳遞的數(shù)據(jù)被篡改過(guò)."),
APPNAME_NOT_NULL(2003,"應(yīng)用名不能為空."),
APPNAME_NOT_EXISTS(2004,"應(yīng)用名不存在."),
APPNAME_NOT_APPLY(2005,"當(dāng)前應(yīng)用沒(méi)有權(quán)限訪問(wèn)此服務(wù),請(qǐng)聯(lián)系管理員進(jìn)行服務(wù)申請(qǐng).");
//?成員變量
privateint?code;//?狀態(tài)碼
private?String?message;//?返回消息
//?構(gòu)造方法
private?DataSignEnum(int?code,?String?message)?{
this.code?=?code;
this.message?=?message;
}
publicint?getCode()?{
return?code;
}
publicvoid?setCode(int?code)?{
this.code?=?code;
}
public?String?getMessage()?{
return?message;
}
publicvoid?setMessage(String?message)?{
this.message?=?message;
}
}
}
愿意了解框架技術(shù)或者源碼的朋友直接求求交流分享技術(shù):3133806896
分布式的一些解決方案咆爽,有愿意了解的朋友可以找我們團(tuán)隊(duì)探討