restful中數據權限加密方式有不少種,特別是針對於request json的方式加密,這裏詳細介紹了SOA中針對於get、post數據加密方式,保證數據傳輸中的安全性: html
- @Component("dataSignInterceptor")
- public class DataSignInterceptor implements MethodInterceptor {
- @Autowired
- private SoaServiceConfigService soaServiceConfigService;
- @Autowired
- private SoaAppSecretService soaAppSecretService;
- @Autowired
- private SoaServiceApplyService soaServiceApplyService;
- public Object invoke(MethodInvocation mi) throws Throwable {
- //TODO 此處應該先查詢緩存
- //獲取全部未管控的服務列表
- List soaServiceConfigList = soaServiceConfigService.findNoSwitchList();
- if(soaServiceConfigList != null){
- //放入緩存
- }
- Object[] ars = mi.getArguments();
- // 判斷該方法是否加了DataSign 註解
- if (mi.getMethod().isAnnotationPresent(DataSign.class)) {
- // 獲取攔截方法的請求參數
- HttpServletRequest request = null;
- JSONObject jsonBody = null;
- Map reqGetParamMap = null; // 客戶端傳遞的參數信息
- Map reqPostParamMap = null; // 客戶端傳遞的參數信息
- Map resultParamMap = new HashMap();
- for (Object obj : ars) {
- if (obj instanceof 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(ov instanceof String[]){
- value=(String[])ov;
- }else{
- value[0]=ov.toString();
- }
- resultParamMap.put(key, value[0]);
- }
- } else if (obj instanceof 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 應該從緩存取,當前先這樣處理
- //若是全部未管控的服務列表中包含用戶請求的服務鏈接,則不須要再進行管控,直接放開服務控制權
- if(soaServiceConfigList != null){
- for(SoaServiceConfig soaServiceConfig : soaServiceConfigList){
- if(serviceUrl.equals(soaServiceConfig.getServiceUrl())){
- return mi.proceed();
- }
- }
- }
- String reqSign = resultParamMap.get("sign");// 客戶端傳遞的簽名認證信息
- // 驗證簽名不能爲空
- if (StringUtils.isEmpty(reqSign)) {
- return new ResponseVO(DataSignEnum.SIGN_NOT_NULL.getCode(), DataSignEnum.SIGN_NOT_NULL.getMessage(),
- null);
- }
- String appname = resultParamMap.get("appname");
- // 驗證應用名不能爲空
- if (StringUtils.isEmpty(appname)) {
- return new ResponseVO(DataSignEnum.APPNAME_NOT_NULL.getCode(),
- DataSignEnum.APPNAME_NOT_NULL.getMessage(), null);
- }
- SoaServiceApply soaServiceApply = soaServiceApplyService.getServiceApplyByAppname(serviceUrl, appname);
- if(null == soaServiceApply){
- return new ResponseVO(DataSignEnum.APPNAME_NOT_APPLY.getCode(),
- DataSignEnum.APPNAME_NOT_APPLY.getMessage(), null);
- }
- SoaAppSecret soaAppSecret = soaAppSecretService.findAppSecretByAppName(appname);
- if(null == soaAppSecret){
- return new ResponseVO(DataSignEnum.APPNAME_NOT_EXISTS.getCode(),
- DataSignEnum.APPNAME_NOT_EXISTS.getMessage(), null);
- }
- // 根據參數從新生成新的簽名sign(由於sign不能認爲是業務參數,故將sign從map中移除後再加密)
- resultParamMap.remove("sign");
- resultParamMap.put("token", soaAppSecret.getToken());
- String sign = MD5Utils.paramString(resultParamMap);
- // 根據應用名獲取應用名對應的祕鑰(目的是和url請求的參數一塊兒進行簽名認證)
- if (!StringUtils.equals(sign, reqSign)) {
- return new ResponseVO(DataSignEnum.SIGN_NOT_MATCH.getCode(), DataSignEnum.SIGN_NOT_MATCH.getMessage(),
- null);
- }
- }
- // 執行被攔截的方法,切記,若是此方法不調用,則被攔截的方法不會被執行。
- return mi.proceed();
- }
- public enum DataSignEnum {
- SIGN_NOT_NULL(2001, "簽名不能爲空."),
- SIGN_NOT_MATCH(2002, "簽名不匹配,傳遞的數據被篡改過."),
- APPNAME_NOT_NULL(2003, "應用名不能爲空."),
- APPNAME_NOT_EXISTS(2004, "應用名不存在."),
- APPNAME_NOT_APPLY(2005, "當前應用沒有權限訪問此服務,請聯繫管理員進行服務申請.");
- // 成員變量
- private int code; // 狀態碼
- private String message; // 返回消息
- // 構造方法
- private DataSignEnum(int code, String message) {
- this.code = code;
- this.message = message;
- }
- public int getCode() {
- return code;
- }
- public void setCode(int code) {
- this.code = code;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
- 願意瞭解框架技術或者源碼的朋友直接求求交流分享技術:3133806896
- 分佈式的一些解決方案,有願意瞭解的朋友能夠找咱們團隊探討
- 更多詳細源碼參考來源