You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

3 years ago
package com.eco.plugin.xxx.zgdxsso.controller;
import com.eco.plugin.xxx.zgdxsso.db.bean.DBEntity;
import com.eco.plugin.xxx.zgdxsso.db.controller.DBController;
import com.eco.plugin.xxx.zgdxsso.utils.EncryptUtils;
import com.fr.decision.webservice.annotation.LoginStatusChecker;
import com.fr.json.JSONObject;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.stable.fun.Authorize;
import com.fr.third.springframework.stereotype.Controller;
import com.fr.third.springframework.web.bind.annotation.PostMapping;
import com.fr.third.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Controller
@LoginStatusChecker(required = false)
@FunctionRecorder
@Authorize(callSignKey = "com.eco.plugin.xxx.zgdxsso")
public class ControllerSelf {
@PostMapping(value = "/getPublicKey")
@ResponseBody
public Map<String,Object> getPublicKey(HttpServletRequest request, HttpServletResponse response){
Map<String,Object> result = new HashMap<String,Object>();
result.put("code",200);
if (!PluginContexts.currentContext().isAvailable()) {
result.put("msg","插件授权过期,请联系管理员");
result.put("code","-1");
return result;
}
Map<String,String> keys = null;
try {
keys = generateKey();
} catch (NoSuchAlgorithmException e) {
result.put("msg","系统异常,请联系管理员");
result.put("code","-1");
return result;
}
//唯一id
String uuid = UUID.randomUUID().toString();
String privateKey = keys.get("privateKey");
String publicKey = keys.get("publicKey");
//将秘钥信息存入数据库
DBEntity db = new DBEntity();
db.setId(uuid);
db.setPrivateKey(privateKey);
db.setUsed("N");
DBController.single(db,null);
//将公钥信息返回
Map<String,String> data = new HashMap<String,String>();
data.put("publicKey",publicKey.trim().replaceAll("\\r","").replaceAll("\\n",""));
data.put("keyId",uuid);
result.put("data",data);
return result;
}
/**
* 生成密钥对
* @return
*/
private Map<String, String> generateKey() throws NoSuchAlgorithmException {
return EncryptUtils.genRSAKeyPair();
}
}