tab to 4 space

This commit is contained in:
MaxKey
2025-11-01 11:17:01 +08:00
parent e823cb1fec
commit dc5e773726
834 changed files with 43445 additions and 43445 deletions

View File

@@ -47,39 +47,39 @@ import jakarta.servlet.http.HttpServletRequest;
@Tag(name = "2-8-ExtendApi接口文档模块")
@Controller
public class ExtendApiAuthorizeEndpoint extends AuthorizeBaseEndpoint{
static final Logger _logger = LoggerFactory.getLogger(ExtendApiAuthorizeEndpoint.class);
static final Logger _logger = LoggerFactory.getLogger(ExtendApiAuthorizeEndpoint.class);
@Operation(summary = "ExtendApi认证地址接口", description = "参数应用ID")
@GetMapping("/authz/api/{id}")
public ModelAndView authorize(
HttpServletRequest request,
@PathVariable("id") String id,
@CurrentUser UserInfo currentUser){
ModelAndView modelAndView = new ModelAndView("authorize/redirect_sso_submit");
modelAndView.addObject("errorCode", 0);
modelAndView.addObject("errorMessage", "");
Apps apps = getApp(id);
_logger.debug("{}" , apps);
if(ConstsBoolean.isTrue(apps.getIsAdapter())){
_logger.debug("Adapter {}",apps.getAdapter());
AbstractAuthorizeAdapter adapter = (AbstractAuthorizeAdapter)Instance.newInstance(apps.getAdapter());
Accounts account = getAccounts(apps,currentUser);
if(apps.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.USER_DEFINED) && account == null) {
return initCredentialView(id,"/authorize/api/"+id);
}
adapter.setPrincipal(AuthorizationUtils.getPrincipal());
adapter.setApp(apps);
adapter.setAccount(account);
return adapter.authorize(modelAndView);
}else{
_logger.debug("redirect_uri {}",apps.getLoginUrl());
modelAndView.addObject("redirect_uri", apps.getLoginUrl());
return modelAndView;
}
}
@Operation(summary = "ExtendApi认证地址接口", description = "参数应用ID")
@GetMapping("/authz/api/{id}")
public ModelAndView authorize(
HttpServletRequest request,
@PathVariable("id") String id,
@CurrentUser UserInfo currentUser){
ModelAndView modelAndView = new ModelAndView("authorize/redirect_sso_submit");
modelAndView.addObject("errorCode", 0);
modelAndView.addObject("errorMessage", "");
Apps apps = getApp(id);
_logger.debug("{}" , apps);
if(ConstsBoolean.isTrue(apps.getIsAdapter())){
_logger.debug("Adapter {}",apps.getAdapter());
AbstractAuthorizeAdapter adapter = (AbstractAuthorizeAdapter)Instance.newInstance(apps.getAdapter());
Accounts account = getAccounts(apps,currentUser);
if(apps.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.USER_DEFINED) && account == null) {
return initCredentialView(id,"/authorize/api/"+id);
}
adapter.setPrincipal(AuthorizationUtils.getPrincipal());
adapter.setApp(apps);
adapter.setAccount(account);
return adapter.authorize(modelAndView);
}else{
_logger.debug("redirect_uri {}",apps.getLoginUrl());
modelAndView.addObject("redirect_uri", apps.getLoginUrl());
return modelAndView;
}
}
}

View File

@@ -35,16 +35,16 @@ import jakarta.servlet.http.HttpServletResponse;
@Tag(name = "2-8-ExtendApi接口文档模块-元数据")
@Controller
public class ExtendApiMetadata {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiMetadata.class);
@Operation(summary = "netease qiye mail RSA Key", description = "网易企业邮箱RSA Key生成器",method="GET")
@RequestMapping(
value = "/metadata/netease/qiye/mail/rsa/gen",
method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public String metadata(HttpServletRequest request,HttpServletResponse response) {
NeteaseRSATool neteaseRSATool =new NeteaseRSATool();
neteaseRSATool.genRSAKeyPair();
return JsonPretty.getInstance().formatln(neteaseRSATool);
}
static final Logger _logger = LoggerFactory.getLogger(ExtendApiMetadata.class);
@Operation(summary = "netease qiye mail RSA Key", description = "网易企业邮箱RSA Key生成器",method="GET")
@RequestMapping(
value = "/metadata/netease/qiye/mail/rsa/gen",
method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public String metadata(HttpServletRequest request,HttpServletResponse response) {
NeteaseRSATool neteaseRSATool =new NeteaseRSATool();
neteaseRSATool.genRSAKeyPair();
return JsonPretty.getInstance().formatln(neteaseRSATool);
}
}

View File

@@ -38,88 +38,88 @@ import org.springframework.web.servlet.ModelAndView;
*
*/
public class ExtendApiCndnsApiMailAdapter extends AbstractAuthorizeAdapter {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiCndnsApiMailAdapter.class);
//sign no parameter
//sign=md5(action=getDomainInfo&appid=***&time=1579736456 + md5(token))
//sign with parameter
//sign=md5(action=getUserInfo&appid=***&email=admin@maxkey.org&time=1579736456 + md5(token))
static final Logger _logger = LoggerFactory.getLogger(ExtendApiCndnsApiMailAdapter.class);
//sign no parameter
//sign=md5(action=getDomainInfo&appid=***&time=1579736456 + md5(token))
//sign with parameter
//sign=md5(action=getUserInfo&appid=***&email=admin@maxkey.org&time=1579736456 + md5(token))
Accounts account;
static String SIGN_STRING ="action=getDomainInfo&appid=%s%s";
static String SIGN_EMAIL_STRING ="action=getUserInfo&appid=%s&email=%s&time=%s%s";
static String ADMIN_AUTHKEY_URI ="https://www.cndnsapi.com/email/clientmanagement?action=getDomailUrl&appid=%s&sign=%s&time=%s";
static String AUTHKEY_URI ="https://www.cndnsapi.com/email/clientmanagement?action=getWebMailUrl&appid=%s&sign=%s&time=%s";
@Override
public Object generateInfo() {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@SuppressWarnings("unchecked")
Accounts account;
static String SIGN_STRING ="action=getDomainInfo&appid=%s%s";
static String SIGN_EMAIL_STRING ="action=getUserInfo&appid=%s&email=%s&time=%s%s";
static String ADMIN_AUTHKEY_URI ="https://www.cndnsapi.com/email/clientmanagement?action=getDomailUrl&appid=%s&sign=%s&time=%s";
static String AUTHKEY_URI ="https://www.cndnsapi.com/email/clientmanagement?action=getWebMailUrl&appid=%s&sign=%s&time=%s";
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
HttpsTrusts.beforeConnection();
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
String action = "getWebMailUrl";
String domain = null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
if(extraAttrs.get("action")==null || extraAttrs.get("action").equalsIgnoreCase("getWebMailUrl")) {
action = "getWebMailUrl";
}else if(extraAttrs.get("action").equalsIgnoreCase("getDomailUrl")){
action = "getDomailUrl";
domain = extraAttrs.get("domain");
}
}
String timestamp = ""+Instant.now().getEpochSecond();
String tokenMd5 =DigestUtils.md5Hex(details.getCredentials());
HashMap<String,Object > requestParamenter =new HashMap<String,Object >();
String redirect_uri = "";
if(action.equalsIgnoreCase("getDomailUrl")) {
String sign =DigestUtils.md5Hex
(String.format(
SIGN_STRING,
details.getPrincipal(),timestamp,tokenMd5));
requestParamenter.put("domain", domain);
String responseBody = new HttpRequestAdapter().post(
String.format(ADMIN_AUTHKEY_URI,details.getPrincipal(),sign,timestamp),requestParamenter);
HashMap<String, String> authKey=JsonUtils.gsonStringToObject(responseBody, HashMap.class);
redirect_uri = authKey.get("adminUrl");
}else {
String sign =DigestUtils.md5Hex
(String.format(
SIGN_EMAIL_STRING,
details.getPrincipal(),userInfo.getEmail(),timestamp,tokenMd5));
requestParamenter.put("email", userInfo.getWorkEmail());
String responseBody = new HttpRequestAdapter().post(
String.format(AUTHKEY_URI,details.getPrincipal(),sign,timestamp),requestParamenter);
HashMap<String, String> authKey=JsonUtils.gsonStringToObject(responseBody, HashMap.class);
redirect_uri=authKey.get("webmailUrl");
}
_logger.debug("redirect_uri : "+redirect_uri);
public Object generateInfo() {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@SuppressWarnings("unchecked")
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
HttpsTrusts.beforeConnection();
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
String action = "getWebMailUrl";
String domain = null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
if(extraAttrs.get("action")==null || extraAttrs.get("action").equalsIgnoreCase("getWebMailUrl")) {
action = "getWebMailUrl";
}else if(extraAttrs.get("action").equalsIgnoreCase("getDomailUrl")){
action = "getDomailUrl";
domain = extraAttrs.get("domain");
}
}
String timestamp = ""+Instant.now().getEpochSecond();
String tokenMd5 =DigestUtils.md5Hex(details.getCredentials());
HashMap<String,Object > requestParamenter =new HashMap<String,Object >();
String redirect_uri = "";
if(action.equalsIgnoreCase("getDomailUrl")) {
String sign =DigestUtils.md5Hex
(String.format(
SIGN_STRING,
details.getPrincipal(),timestamp,tokenMd5));
requestParamenter.put("domain", domain);
String responseBody = new HttpRequestAdapter().post(
String.format(ADMIN_AUTHKEY_URI,details.getPrincipal(),sign,timestamp),requestParamenter);
HashMap<String, String> authKey=JsonUtils.gsonStringToObject(responseBody, HashMap.class);
redirect_uri = authKey.get("adminUrl");
}else {
String sign =DigestUtils.md5Hex
(String.format(
SIGN_EMAIL_STRING,
details.getPrincipal(),userInfo.getEmail(),timestamp,tokenMd5));
requestParamenter.put("email", userInfo.getWorkEmail());
String responseBody = new HttpRequestAdapter().post(
String.format(AUTHKEY_URI,details.getPrincipal(),sign,timestamp),requestParamenter);
HashMap<String, String> authKey=JsonUtils.gsonStringToObject(responseBody, HashMap.class);
redirect_uri=authKey.get("webmailUrl");
}
_logger.debug("redirect_uri : "+redirect_uri);
modelAndView.addObject("redirect_uri", redirect_uri);
return modelAndView;
}
}
}

View File

@@ -35,60 +35,60 @@ import org.springframework.web.servlet.ModelAndView;
*
*/
public class ExtendApiNeteaseQiyeMailAdapter extends AbstractAuthorizeAdapter {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiNeteaseQiyeMailAdapter.class);
//https://entryhz.qiye.163.com
static String REDIRECT_PARAMETER = "domain=%s&account_name=%s&time=%s&enc=%s&lang=%s";
static String DEFAULT_REDIRECT_URI ="https://entryhz.qiye.163.com/domain/oa/Entry";
static final Logger _logger = LoggerFactory.getLogger(ExtendApiNeteaseQiyeMailAdapter.class);
//https://entryhz.qiye.163.com
static String REDIRECT_PARAMETER = "domain=%s&account_name=%s&time=%s&enc=%s&lang=%s";
static String DEFAULT_REDIRECT_URI ="https://entryhz.qiye.163.com/domain/oa/Entry";
Accounts account;
@Override
public Object generateInfo() {
return null;
}
Accounts account;
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
StringBuffer redirect_uri = new StringBuffer(details.getLoginUrl());
if(StringUtils.isNotBlank(redirect_uri)) {
if(redirect_uri.indexOf("?")>-1) {
redirect_uri.append("").append( REDIRECT_PARAMETER);
}else {
redirect_uri.append("?").append( REDIRECT_PARAMETER);
}
}
//extraAttrs from App
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr() == 1){
extraAttrs = new ExtraAttrs(details.getExtendAttr());
for(ExtraAttr attr : extraAttrs.getExtraAttrs()) {
redirect_uri.append("&").append(attr.getAttr()).append("=").append(attr.getValue());
}
}
String time = System.currentTimeMillis() + "";
//域名,请使用企业自己的域名
String domain = details.getPrincipal();
String account_name = this.userInfo.getEmail().substring(0, this.userInfo.getEmail().indexOf("@"));
String lang = "0";
String src = account_name + domain + time;
String privateKey = details.getCredentials();
_logger.debug("Private Key {} " , privateKey);
String enc = new NeteaseRSATool().generateSHA1withRSASigature(src, privateKey);
String loginUrl = String.format(redirect_uri.toString(), domain,account_name,time,enc,lang);
_logger.debug("LoginUrl {} " , loginUrl);
modelAndView.addObject("redirect_uri", loginUrl);
public Object generateInfo() {
return null;
}
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
StringBuffer redirect_uri = new StringBuffer(details.getLoginUrl());
if(StringUtils.isNotBlank(redirect_uri)) {
if(redirect_uri.indexOf("?")>-1) {
redirect_uri.append("").append( REDIRECT_PARAMETER);
}else {
redirect_uri.append("?").append( REDIRECT_PARAMETER);
}
}
//extraAttrs from App
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr() == 1){
extraAttrs = new ExtraAttrs(details.getExtendAttr());
for(ExtraAttr attr : extraAttrs.getExtraAttrs()) {
redirect_uri.append("&").append(attr.getAttr()).append("=").append(attr.getValue());
}
}
String time = System.currentTimeMillis() + "";
//域名,请使用企业自己的域名
String domain = details.getPrincipal();
String account_name = this.userInfo.getEmail().substring(0, this.userInfo.getEmail().indexOf("@"));
String lang = "0";
String src = account_name + domain + time;
String privateKey = details.getCredentials();
_logger.debug("Private Key {} " , privateKey);
String enc = new NeteaseRSATool().generateSHA1withRSASigature(src, privateKey);
String loginUrl = String.format(redirect_uri.toString(), domain,account_name,time,enc,lang);
_logger.debug("LoginUrl {} " , loginUrl);
modelAndView.addObject("redirect_uri", loginUrl);
return modelAndView;
}
}
}

View File

@@ -41,276 +41,276 @@ import com.github.benmanes.caffeine.cache.Caffeine;
*
*/
public class ExtendApiQQExmailAdapter extends AbstractAuthorizeAdapter {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiQQExmailAdapter.class);
//https://exmail.qq.com/qy_mng_logic/doc#10003
static String TOKEN_URI = "https://api.exmail.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
//https://exmail.qq.com/qy_mng_logic/doc#10036
static String AUTHKEY_URI = "https://api.exmail.qq.com/cgi-bin/service/get_login_url?access_token=%s&userid=%s";
static final Cache<String, String> tokenCache = Caffeine.newBuilder()
.expireAfterWrite(7200, TimeUnit.SECONDS)
.build();
Accounts account;
@Override
public Object generateInfo() {
return null;
}
static final Logger _logger = LoggerFactory.getLogger(ExtendApiQQExmailAdapter.class);
//https://exmail.qq.com/qy_mng_logic/doc#10003
static String TOKEN_URI = "https://api.exmail.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
//https://exmail.qq.com/qy_mng_logic/doc#10036
static String AUTHKEY_URI = "https://api.exmail.qq.com/cgi-bin/service/get_login_url?access_token=%s&userid=%s";
static final Cache<String, String> tokenCache = Caffeine.newBuilder()
.expireAfterWrite(7200, TimeUnit.SECONDS)
.build();
Accounts account;
@Override
public Object generateInfo() {
return null;
}
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
HttpsTrusts.beforeConnection();
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
}
_logger.debug("Extra Attrs {}",extraAttrs);
String accessToken = getToken(details.getPrincipal(),details.getCredentials());
ExMailLoginUrl exMailLoginUrl = getLoginUrl(accessToken,userInfo.getUsername());
if(exMailLoginUrl.errcode == 0) {
modelAndView.addObject("redirect_uri", exMailLoginUrl.getLogin_url());
}else {
_logger.error("Exception code {} , message {} , mapping message {} ,",
exMailLoginUrl.getErrcode(),
exMailLoginUrl.getErrmsg(),
exMailMsgMapper.get(exMailLoginUrl.getErrcode())
);
//remove accessToken
tokenCache.invalidate(details.getPrincipal());
modelAndView.addObject("errorCode", exMailLoginUrl.getErrcode());
modelAndView.addObject("errorMessage", exMailMsgMapper.get(exMailLoginUrl.getErrcode()));
}
public ModelAndView authorize(ModelAndView modelAndView) {
HttpsTrusts.beforeConnection();
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
}
_logger.debug("Extra Attrs {}",extraAttrs);
String accessToken = getToken(details.getPrincipal(),details.getCredentials());
ExMailLoginUrl exMailLoginUrl = getLoginUrl(accessToken,userInfo.getUsername());
if(exMailLoginUrl.errcode == 0) {
modelAndView.addObject("redirect_uri", exMailLoginUrl.getLogin_url());
}else {
_logger.error("Exception code {} , message {} , mapping message {} ,",
exMailLoginUrl.getErrcode(),
exMailLoginUrl.getErrmsg(),
exMailMsgMapper.get(exMailLoginUrl.getErrcode())
);
//remove accessToken
tokenCache.invalidate(details.getPrincipal());
modelAndView.addObject("errorCode", exMailLoginUrl.getErrcode());
modelAndView.addObject("errorMessage", exMailMsgMapper.get(exMailLoginUrl.getErrcode()));
}
return modelAndView;
}
}
public String getToken(String corpid , String corpsecret) {
String accessToken = tokenCache.getIfPresent(corpid);
if(accessToken == null) {
_logger.debug("corpid {} , corpsecret {}" , corpid , corpsecret);
_logger.debug("get token url {}" , String.format(TOKEN_URI,corpid,corpsecret));
String responseBody = new HttpRequestAdapter().get(String.format(TOKEN_URI,corpid,corpsecret),null);
_logger.debug("Response Body {}" , responseBody);
Token token = JsonUtils.gsonStringToObject(responseBody,Token.class);
if(token.getErrcode() == 0 ) {
_logger.debug("access_token {}" , token);
accessToken = token.getAccess_token();
tokenCache.put(corpid, accessToken);
}else {
_logger.debug("Error Code {}" , exMailMsgMapper.get(token.getErrcode()));;
}
}
return accessToken;
String accessToken = tokenCache.getIfPresent(corpid);
if(accessToken == null) {
_logger.debug("corpid {} , corpsecret {}" , corpid , corpsecret);
_logger.debug("get token url {}" , String.format(TOKEN_URI,corpid,corpsecret));
String responseBody = new HttpRequestAdapter().get(String.format(TOKEN_URI,corpid,corpsecret),null);
_logger.debug("Response Body {}" , responseBody);
Token token = JsonUtils.gsonStringToObject(responseBody,Token.class);
if(token.getErrcode() == 0 ) {
_logger.debug("access_token {}" , token);
accessToken = token.getAccess_token();
tokenCache.put(corpid, accessToken);
}else {
_logger.debug("Error Code {}" , exMailMsgMapper.get(token.getErrcode()));;
}
}
return accessToken;
}
public ExMailLoginUrl getLoginUrl(String accessToken,String userId) {
if(accessToken != null) {
_logger.debug("userId {}" , userId);
String authKeyBody = new HttpRequestAdapter().get(String.format(AUTHKEY_URI,accessToken,userId),null);
ExMailLoginUrl exMailLoginUrl = JsonUtils.gsonStringToObject(authKeyBody, ExMailLoginUrl.class);
_logger.debug("LoginUrl {} " , exMailLoginUrl);
return exMailLoginUrl;
}
return new ExMailLoginUrl(-1,"access_token is null .");
if(accessToken != null) {
_logger.debug("userId {}" , userId);
String authKeyBody = new HttpRequestAdapter().get(String.format(AUTHKEY_URI,accessToken,userId),null);
ExMailLoginUrl exMailLoginUrl = JsonUtils.gsonStringToObject(authKeyBody, ExMailLoginUrl.class);
_logger.debug("LoginUrl {} " , exMailLoginUrl);
return exMailLoginUrl;
}
return new ExMailLoginUrl(-1,"access_token is null .");
}
class ExMailMsg{
protected long expires_in;
protected String errmsg;
protected Integer errcode;
class ExMailMsg{
protected long expires_in;
protected String errmsg;
protected Integer errcode;
public ExMailMsg() {
}
public ExMailMsg() {
}
public long getExpires_in() {
return expires_in;
}
public long getExpires_in() {
return expires_in;
}
public void setExpires_in(long expires_in) {
this.expires_in = expires_in;
}
public void setExpires_in(long expires_in) {
this.expires_in = expires_in;
}
public String getErrmsg() {
return errmsg;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
public Integer getErrcode() {
return errcode;
}
public Integer getErrcode() {
return errcode;
}
public void setErrcode(Integer errcode) {
this.errcode = errcode;
}
}
class Token extends ExMailMsg implements Serializable {
private static final long serialVersionUID = 275756585220635542L;
public void setErrcode(Integer errcode) {
this.errcode = errcode;
}
}
class Token extends ExMailMsg implements Serializable {
private static final long serialVersionUID = 275756585220635542L;
/**
* access_token
*/
private String access_token;
public String getAccess_token() {
return access_token;
}
/**
* access_token
*/
private String access_token;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Token [access_token = ");
builder.append(access_token);
builder.append("]");
return builder.toString();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Token [access_token = ");
builder.append(access_token);
builder.append("]");
return builder.toString();
}
}
class ExMailLoginUrl extends ExMailMsg implements Serializable {
private static final long serialVersionUID = 3033047757268214198L;
private String login_url;
public String getLogin_url() {
return login_url;
}
}
class ExMailLoginUrl extends ExMailMsg implements Serializable {
private static final long serialVersionUID = 3033047757268214198L;
private String login_url;
public String getLogin_url() {
return login_url;
}
public void setLogin_url(String login_url) {
this.login_url = login_url;
}
public ExMailLoginUrl() {
}
public ExMailLoginUrl(Integer errcode,String errmsg) {
super.errcode = errcode;
super.errmsg = errmsg;
}
public void setLogin_url(String login_url) {
this.login_url = login_url;
}
public ExMailLoginUrl() {
}
public ExMailLoginUrl(Integer errcode,String errmsg) {
super.errcode = errcode;
super.errmsg = errmsg;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("LoginUrl [login_url=");
builder.append(login_url);
builder.append("]");
return builder.toString();
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("LoginUrl [login_url=");
builder.append(login_url);
builder.append("]");
return builder.toString();
}
}
public static HashMap<Integer,String> exMailMsgMapper = new HashMap<Integer,String>();
static {
exMailMsgMapper.put(-1, "系统繁忙");
exMailMsgMapper.put(0, "请求成功");
exMailMsgMapper.put(40001, "获取access_token时CorpSecret错误或者access_token无效");
exMailMsgMapper.put(40003, "不合法的UserID");
exMailMsgMapper.put(40013, "不合法的corpid");
exMailMsgMapper.put(40014, "不合法的access_token");
exMailMsgMapper.put(40057, "不合法的callbackurl或者callbackurl验证失败");
exMailMsgMapper.put(40091, "无效secert");
exMailMsgMapper.put(40092, "参数不合法");
exMailMsgMapper.put(40093, "请求并发过大,请降低并发并重试");
exMailMsgMapper.put(45009, "接口调用超过限制");
exMailMsgMapper.put(45024, "帐号数量超过上限");
exMailMsgMapper.put(50005, "企业已禁用");
exMailMsgMapper.put(60001, "部门长度不符合限制");
exMailMsgMapper.put(60002, "部门层级深度超过限制");
exMailMsgMapper.put(60003, "部门不存在");
exMailMsgMapper.put(60004, "父部门不存在");
exMailMsgMapper.put(60005, "不允许删除有成员的部门");
exMailMsgMapper.put(60006, "不允许删除有子部门的部门");
exMailMsgMapper.put(60007, "不允许删除根部门");
exMailMsgMapper.put(60008, "部门名称已存在");
exMailMsgMapper.put(60009, "部门名称含有非法字符");
exMailMsgMapper.put(60010, "部门存在循环关系");
exMailMsgMapper.put(60102, "UserID已存在");
exMailMsgMapper.put(60103, "手机号码不合法");
exMailMsgMapper.put(60104, "不合法的position参数");
exMailMsgMapper.put(60105, "部门ID数量超过上限");
exMailMsgMapper.put(60106, "不合法的userlist参数");
exMailMsgMapper.put(60111, "UserID不存在");
exMailMsgMapper.put(60112, "成员姓名不合法");
exMailMsgMapper.put(60114, "性别不合法");
exMailMsgMapper.put(60115, "激活码格式错误");
exMailMsgMapper.put(60116, "邮箱回收站内已存在相同的帐号");
exMailMsgMapper.put(60118, "userid在企业微信重复");
exMailMsgMapper.put(60119, "用户未绑定(删除、回收站状态)");
exMailMsgMapper.put(60120, "密码和手机号不能同时为空");
exMailMsgMapper.put(60123, "无效的部门id");
exMailMsgMapper.put(60124, "无效的父部门id");
exMailMsgMapper.put(60125, "非法部门名字长度超过限制、重名等重名包括与csv文件中同级部门重名或者与旧组织架构包含成员的同级部门重名");
exMailMsgMapper.put(60126, "创建部门失败");
exMailMsgMapper.put(60127, "缺少部门id");
exMailMsgMapper.put(60128, "帐号已绑定手机或微信,需员工修改密码");
exMailMsgMapper.put(60201, "不合法的标签id");
exMailMsgMapper.put(60202, "缺少标签id");
exMailMsgMapper.put(60203, "不合法的标签名");
exMailMsgMapper.put(60204, "标签名已存在");
exMailMsgMapper.put(60205, "所有参数都非法");
exMailMsgMapper.put(60301, "不合法的type参数");
exMailMsgMapper.put(60302, "不合法的option参数");
exMailMsgMapper.put(600001, "Userid与别名冲突");
exMailMsgMapper.put(600002, "Userid与Groupid冲突");
exMailMsgMapper.put(600003, "无效密码或者是弱密码");
exMailMsgMapper.put(600004, "别名无效");
exMailMsgMapper.put(600005, "别名与userid或者Groupid冲突");
exMailMsgMapper.put(600006, "别名数量达到上限");
exMailMsgMapper.put(600007, "Groupid无效");
exMailMsgMapper.put(600008, "邮件群组不存在");
exMailMsgMapper.put(600009, "群组成员为空");
exMailMsgMapper.put(600010, "Userlist无效可能是个别成员无效");
exMailMsgMapper.put(600011, "Grouplist无效可能是个别成员无效");
exMailMsgMapper.put(600012, "Partylist无效可能是个别成员无效");
exMailMsgMapper.put(600013, "群发权限类型无效");
exMailMsgMapper.put(600014, "群发权限成员无效");
exMailMsgMapper.put(600015, "邮件群组已存在");
exMailMsgMapper.put(600016, "Userlist部分成员未找到");
exMailMsgMapper.put(600017, "Partylist部分成员未找到");
exMailMsgMapper.put(600018, "Grouplist部分成员未找到");
exMailMsgMapper.put(600019, "邮件群组名称含有非法字符");
exMailMsgMapper.put(600020, "邮件群组存在循环");
exMailMsgMapper.put(600021, "邮件群组嵌套超过层数");
exMailMsgMapper.put(600023, "群发权限成员缺失");
exMailMsgMapper.put(600024, "Groupid与userid或者别名冲突");
exMailMsgMapper.put(600025, "座机号码无效");
exMailMsgMapper.put(600026, "编号无效");
exMailMsgMapper.put(600027, "批量检查的成员数超过限额");
exMailMsgMapper.put(600034, "不合法的fuzzy参数");
exMailMsgMapper.put(600035, "不合法的业务邮箱账号");
exMailMsgMapper.put(600036, "密码不允许修改");
exMailMsgMapper.put(600037, "不合法的业务邮箱ID");
exMailMsgMapper.put(600038, "不合法的业务邮箱名称");
exMailMsgMapper.put(600039, "业务邮箱账号已存在");
exMailMsgMapper.put(600040, "业务邮箱名称已存在");
exMailMsgMapper.put(600041, "业务邮箱ID不存在");
exMailMsgMapper.put(601001, "日志查询的时间无效");
exMailMsgMapper.put(601002, "日志查询的时间超过限制");
exMailMsgMapper.put(601003, "日志查询的域名无效");
exMailMsgMapper.put(601004, "日志查询的域名不存在");
exMailMsgMapper.put(601005, "不合法的mailtype参数");
exMailMsgMapper.put(601006, "不合法的type参数");
exMailMsgMapper.put(602005, "应用没有访问此API的权限");
exMailMsgMapper.put(604001, "基础版企业无权限操作专业版功能");
exMailMsgMapper.put(604002, "企业VIP人员达到上限");
exMailMsgMapper.put(604003, "不合法的setvip参数");
}
public static HashMap<Integer,String> exMailMsgMapper = new HashMap<Integer,String>();
static {
exMailMsgMapper.put(-1, "系统繁忙");
exMailMsgMapper.put(0, "请求成功");
exMailMsgMapper.put(40001, "获取access_token时CorpSecret错误或者access_token无效");
exMailMsgMapper.put(40003, "不合法的UserID");
exMailMsgMapper.put(40013, "不合法的corpid");
exMailMsgMapper.put(40014, "不合法的access_token");
exMailMsgMapper.put(40057, "不合法的callbackurl或者callbackurl验证失败");
exMailMsgMapper.put(40091, "无效secert");
exMailMsgMapper.put(40092, "参数不合法");
exMailMsgMapper.put(40093, "请求并发过大,请降低并发并重试");
exMailMsgMapper.put(45009, "接口调用超过限制");
exMailMsgMapper.put(45024, "帐号数量超过上限");
exMailMsgMapper.put(50005, "企业已禁用");
exMailMsgMapper.put(60001, "部门长度不符合限制");
exMailMsgMapper.put(60002, "部门层级深度超过限制");
exMailMsgMapper.put(60003, "部门不存在");
exMailMsgMapper.put(60004, "父部门不存在");
exMailMsgMapper.put(60005, "不允许删除有成员的部门");
exMailMsgMapper.put(60006, "不允许删除有子部门的部门");
exMailMsgMapper.put(60007, "不允许删除根部门");
exMailMsgMapper.put(60008, "部门名称已存在");
exMailMsgMapper.put(60009, "部门名称含有非法字符");
exMailMsgMapper.put(60010, "部门存在循环关系");
exMailMsgMapper.put(60102, "UserID已存在");
exMailMsgMapper.put(60103, "手机号码不合法");
exMailMsgMapper.put(60104, "不合法的position参数");
exMailMsgMapper.put(60105, "部门ID数量超过上限");
exMailMsgMapper.put(60106, "不合法的userlist参数");
exMailMsgMapper.put(60111, "UserID不存在");
exMailMsgMapper.put(60112, "成员姓名不合法");
exMailMsgMapper.put(60114, "性别不合法");
exMailMsgMapper.put(60115, "激活码格式错误");
exMailMsgMapper.put(60116, "邮箱回收站内已存在相同的帐号");
exMailMsgMapper.put(60118, "userid在企业微信重复");
exMailMsgMapper.put(60119, "用户未绑定(删除、回收站状态)");
exMailMsgMapper.put(60120, "密码和手机号不能同时为空");
exMailMsgMapper.put(60123, "无效的部门id");
exMailMsgMapper.put(60124, "无效的父部门id");
exMailMsgMapper.put(60125, "非法部门名字长度超过限制、重名等重名包括与csv文件中同级部门重名或者与旧组织架构包含成员的同级部门重名");
exMailMsgMapper.put(60126, "创建部门失败");
exMailMsgMapper.put(60127, "缺少部门id");
exMailMsgMapper.put(60128, "帐号已绑定手机或微信,需员工修改密码");
exMailMsgMapper.put(60201, "不合法的标签id");
exMailMsgMapper.put(60202, "缺少标签id");
exMailMsgMapper.put(60203, "不合法的标签名");
exMailMsgMapper.put(60204, "标签名已存在");
exMailMsgMapper.put(60205, "所有参数都非法");
exMailMsgMapper.put(60301, "不合法的type参数");
exMailMsgMapper.put(60302, "不合法的option参数");
exMailMsgMapper.put(600001, "Userid与别名冲突");
exMailMsgMapper.put(600002, "Userid与Groupid冲突");
exMailMsgMapper.put(600003, "无效密码或者是弱密码");
exMailMsgMapper.put(600004, "别名无效");
exMailMsgMapper.put(600005, "别名与userid或者Groupid冲突");
exMailMsgMapper.put(600006, "别名数量达到上限");
exMailMsgMapper.put(600007, "Groupid无效");
exMailMsgMapper.put(600008, "邮件群组不存在");
exMailMsgMapper.put(600009, "群组成员为空");
exMailMsgMapper.put(600010, "Userlist无效可能是个别成员无效");
exMailMsgMapper.put(600011, "Grouplist无效可能是个别成员无效");
exMailMsgMapper.put(600012, "Partylist无效可能是个别成员无效");
exMailMsgMapper.put(600013, "群发权限类型无效");
exMailMsgMapper.put(600014, "群发权限成员无效");
exMailMsgMapper.put(600015, "邮件群组已存在");
exMailMsgMapper.put(600016, "Userlist部分成员未找到");
exMailMsgMapper.put(600017, "Partylist部分成员未找到");
exMailMsgMapper.put(600018, "Grouplist部分成员未找到");
exMailMsgMapper.put(600019, "邮件群组名称含有非法字符");
exMailMsgMapper.put(600020, "邮件群组存在循环");
exMailMsgMapper.put(600021, "邮件群组嵌套超过层数");
exMailMsgMapper.put(600023, "群发权限成员缺失");
exMailMsgMapper.put(600024, "Groupid与userid或者别名冲突");
exMailMsgMapper.put(600025, "座机号码无效");
exMailMsgMapper.put(600026, "编号无效");
exMailMsgMapper.put(600027, "批量检查的成员数超过限额");
exMailMsgMapper.put(600034, "不合法的fuzzy参数");
exMailMsgMapper.put(600035, "不合法的业务邮箱账号");
exMailMsgMapper.put(600036, "密码不允许修改");
exMailMsgMapper.put(600037, "不合法的业务邮箱ID");
exMailMsgMapper.put(600038, "不合法的业务邮箱名称");
exMailMsgMapper.put(600039, "业务邮箱账号已存在");
exMailMsgMapper.put(600040, "业务邮箱名称已存在");
exMailMsgMapper.put(600041, "业务邮箱ID不存在");
exMailMsgMapper.put(601001, "日志查询的时间无效");
exMailMsgMapper.put(601002, "日志查询的时间超过限制");
exMailMsgMapper.put(601003, "日志查询的域名无效");
exMailMsgMapper.put(601004, "日志查询的域名不存在");
exMailMsgMapper.put(601005, "不合法的mailtype参数");
exMailMsgMapper.put(601006, "不合法的type参数");
exMailMsgMapper.put(602005, "应用没有访问此API的权限");
exMailMsgMapper.put(604001, "基础版企业无权限操作专业版功能");
exMailMsgMapper.put(604002, "企业VIP人员达到上限");
exMailMsgMapper.put(604003, "不合法的setvip参数");
}
}

View File

@@ -32,66 +32,66 @@ import org.springframework.web.servlet.ModelAndView;
* http://target.maxkey.org/demo/login?code=maxkey&time=timestamp&token=token
* login url http://target.maxkey.org/demo/login?code=%s&timestamp=%s&token=%s
*
* $code = 'maxkey';
* $key = 'a5246932b0f371263c252384076cd3f0';
* $timestamp = '1557034496';
* $token = md5($code . $key . $time);
* $code = 'maxkey';
* $key = 'a5246932b0f371263c252384076cd3f0';
* $timestamp = '1557034496';
* $token = md5($code . $key . $time);
*
* @author shimingxy
*
*/
public class ExtendApiTimestampSignAdapter extends AbstractAuthorizeAdapter {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiTimestampSignAdapter.class);
Accounts account;
@Override
public Object generateInfo() {
return null;
}
static final Logger _logger = LoggerFactory.getLogger(ExtendApiTimestampSignAdapter.class);
Accounts account;
@Override
public Object generateInfo() {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
String code = details.getPrincipal();
String key = details.getCredentials();
String timestamp = ""+Instant.now().getEpochSecond();
String token =DigestUtils.md5Hex(code+key+timestamp);
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
if(extraAttrs.get("sign") == null || extraAttrs.get("sign").equalsIgnoreCase("md5")) {
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha") || extraAttrs.get("sign").equalsIgnoreCase("sha1")) {
token =DigestUtils.shaHex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha256")) {
token =DigestUtils.sha256Hex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha384")) {
token =DigestUtils.sha384Hex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha512")) {
token =DigestUtils.sha512Hex(code+key+timestamp);
}
}
_logger.debug(""+token);
String account = userInfo.getUsername();
String redirect_uri = String.format(details.getLoginUrl(),account,code,timestamp,token);
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
String code = details.getPrincipal();
String key = details.getCredentials();
String timestamp = ""+Instant.now().getEpochSecond();
String token =DigestUtils.md5Hex(code+key+timestamp);
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
if(extraAttrs.get("sign") == null || extraAttrs.get("sign").equalsIgnoreCase("md5")) {
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha") || extraAttrs.get("sign").equalsIgnoreCase("sha1")) {
token =DigestUtils.shaHex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha256")) {
token =DigestUtils.sha256Hex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha384")) {
token =DigestUtils.sha384Hex(code+key+timestamp);
}else if(extraAttrs.get("sign").equalsIgnoreCase("sha512")) {
token =DigestUtils.sha512Hex(code+key+timestamp);
}
}
_logger.debug(""+token);
String account = userInfo.getUsername();
String redirect_uri = String.format(details.getLoginUrl(),account,code,timestamp,token);
_logger.debug("redirect_uri : "+redirect_uri);
_logger.debug("redirect_uri : "+redirect_uri);
modelAndView.addObject("redirect_uri", redirect_uri);
return modelAndView;
}
}
}

View File

@@ -40,59 +40,59 @@ import org.springframework.web.servlet.ModelAndView;
*
*/
public class ExtendApiZentaoAdapter extends AbstractAuthorizeAdapter {
static final Logger _logger = LoggerFactory.getLogger(ExtendApiZentaoAdapter.class);
static String login_url_template="api.php?m=user&f=apilogin&account=%s&code=%s&time=%s&token=%s";
static String login_url_m_template="account=%s&code=%s&time=%s&token=%s";
Accounts account;
@Override
public Object generateInfo() {
return null;
}
static final Logger _logger = LoggerFactory.getLogger(ExtendApiZentaoAdapter.class);
static String login_url_template="api.php?m=user&f=apilogin&account=%s&code=%s&time=%s&token=%s";
static String login_url_m_template="account=%s&code=%s&time=%s&token=%s";
Accounts account;
@Override
public Object generateInfo() {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@Override
public Object encrypt(Object data, String algorithmKey, String algorithm) {
return null;
}
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
}
_logger.trace("Extra Attrs " + extraAttrs);
String code = details.getPrincipal();
String key = details.getCredentials();
String time = ""+Instant.now().getEpochSecond();
@Override
public ModelAndView authorize(ModelAndView modelAndView) {
Apps details=(Apps)app;
//extraAttrs from Applications
ExtraAttrs extraAttrs=null;
if(details.getIsExtendAttr()==1){
extraAttrs=new ExtraAttrs(details.getExtendAttr());
}
_logger.trace("Extra Attrs " + extraAttrs);
String code = details.getPrincipal();
String key = details.getCredentials();
String time = ""+Instant.now().getEpochSecond();
String token =DigestUtils.md5Hex(code+key+time);
_logger.debug(""+token);
String account = userInfo.getUsername();
String redirect_uri = details.getLoginUrl();
if(redirect_uri.indexOf("api.php?")<0) {
if(redirect_uri.endsWith("/")) {
redirect_uri += String.format(login_url_template,account,code,time,token);
}else {
redirect_uri +="/" + String.format(login_url_template,account,code,time,token);
}
}else if(redirect_uri.endsWith("&")){
redirect_uri += String.format(login_url_m_template,account,code,time,token);
}else {
redirect_uri += "&" +String.format(login_url_m_template,account,code,time,token);
}
_logger.debug("redirect_uri : "+redirect_uri);
modelAndView=new ModelAndView("authorize/redirect_sso_submit");
String token =DigestUtils.md5Hex(code+key+time);
_logger.debug(""+token);
String account = userInfo.getUsername();
String redirect_uri = details.getLoginUrl();
if(redirect_uri.indexOf("api.php?")<0) {
if(redirect_uri.endsWith("/")) {
redirect_uri += String.format(login_url_template,account,code,time,token);
}else {
redirect_uri +="/" + String.format(login_url_template,account,code,time,token);
}
}else if(redirect_uri.endsWith("&")){
redirect_uri += String.format(login_url_m_template,account,code,time,token);
}else {
redirect_uri += "&" +String.format(login_url_m_template,account,code,time,token);
}
_logger.debug("redirect_uri : "+redirect_uri);
modelAndView=new ModelAndView("authorize/redirect_sso_submit");
modelAndView.addObject("redirect_uri", redirect_uri);
return modelAndView;
}
return modelAndView;
}
}

View File

@@ -39,289 +39,289 @@ import org.slf4j.LoggerFactory;
public class NeteaseRSATool {
static final Logger _logger = LoggerFactory.getLogger(NeteaseRSATool.class);
private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
static final Logger _logger = LoggerFactory.getLogger(NeteaseRSATool.class);
private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private String description = "1024-bit RSA key";
private String priKey = null;
private String pubKey = null;
private String description = "1024-bit RSA key";
private String priKey = null;
private String pubKey = null;
public String bytesToHexStr(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);
public String bytesToHexStr(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);
for (int i = 0; i < bcd.length; i++) {
s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
s.append(bcdLookup[bcd[i] & 0x0f]);
}
for (int i = 0; i < bcd.length; i++) {
s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
s.append(bcdLookup[bcd[i] & 0x0f]);
}
return s.toString();
}
return s.toString();
}
public byte[] hexStrToBytes(String s) {
byte[] bytes;
public byte[] hexStrToBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
return bytes;
}
public void genRSAKeyPair() {
KeyPairGenerator rsaKeyGen = null;
KeyPair rsaKeyPair = null;
try {
_logger.trace("Generating a pair of RSA key ... ");
rsaKeyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
random.setSeed(System.currentTimeMillis());
public void genRSAKeyPair() {
KeyPairGenerator rsaKeyGen = null;
KeyPair rsaKeyPair = null;
try {
_logger.trace("Generating a pair of RSA key ... ");
rsaKeyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
random.setSeed(System.currentTimeMillis());
rsaKeyGen.initialize(1024, random);
rsaKeyGen.initialize(1024, random);
rsaKeyPair = rsaKeyGen.genKeyPair();
PublicKey rsaPublic = rsaKeyPair.getPublic();
PrivateKey rsaPrivate = rsaKeyPair.getPrivate();
rsaKeyPair = rsaKeyGen.genKeyPair();
PublicKey rsaPublic = rsaKeyPair.getPublic();
PrivateKey rsaPrivate = rsaKeyPair.getPrivate();
pubKey = bytesToHexStr(rsaPublic.getEncoded());
priKey = bytesToHexStr(rsaPrivate.getEncoded());
_logger.trace("pubKey: {}" , pubKey);
_logger.trace("priKey: {}" , priKey);
_logger.trace("1024-bit RSA key GENERATED.");
} catch (Exception e) {
_logger.error("Exception genRSAKeyPair:" + e);
}
}
pubKey = bytesToHexStr(rsaPublic.getEncoded());
priKey = bytesToHexStr(rsaPrivate.getEncoded());
_logger.trace("pubKey: {}" , pubKey);
_logger.trace("priKey: {}" , priKey);
_logger.trace("1024-bit RSA key GENERATED.");
} catch (Exception e) {
_logger.error("Exception genRSAKeyPair:" + e);
}
}
public String generateSHA1withRSASigature(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) fac.generatePrivate(keySpec);
Signature sigEng = Signature.getInstance("SHA1withRSA");
sigEng.initSign(privateKey);
sigEng.update(src.getBytes());
byte[] signature = sigEng.sign();
return bytesToHexStr(signature);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String encryptWithPriKey(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key privateKey = fac.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] bytes = src.getBytes();
byte[] encodedByteArray = new byte[] {};
for (int i = 0; i < bytes.length; i += 102){
byte[] subarray = ArrayUtils.subarray(bytes, i, i + 102);
byte[] doFinal = cipher.doFinal(subarray);
encodedByteArray = ArrayUtils.addAll(encodedByteArray, doFinal);
}
return bytesToHexStr(encodedByteArray);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String generateSHA1withRSASigature(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) fac.generatePrivate(keySpec);
Signature sigEng = Signature.getInstance("SHA1withRSA");
sigEng.initSign(privateKey);
sigEng.update(src.getBytes());
byte[] signature = sigEng.sign();
return bytesToHexStr(signature);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String encryptWithPriKey(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key privateKey = fac.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] bytes = src.getBytes();
byte[] encodedByteArray = new byte[] {};
for (int i = 0; i < bytes.length; i += 102){
byte[] subarray = ArrayUtils.subarray(bytes, i, i + 102);
byte[] doFinal = cipher.doFinal(subarray);
encodedByteArray = ArrayUtils.addAll(encodedByteArray, doFinal);
}
return bytesToHexStr(encodedByteArray);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean verifySHA1withRSASigature(String sign, String src,
String pubKeyStr) {
try {
public boolean verifySHA1withRSASigature(String sign, String src,
String pubKeyStr) {
try {
Signature sigEng = Signature.getInstance("SHA1withRSA");
Signature sigEng = Signature.getInstance("SHA1withRSA");
byte[] pubbyte = hexStrToBytes(pubKeyStr.trim());
byte[] pubbyte = hexStrToBytes(pubKeyStr.trim());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) fac.generatePublic(keySpec);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) fac.generatePublic(keySpec);
sigEng.initVerify(pubKey);
sigEng.update(src.getBytes());
sigEng.initVerify(pubKey);
sigEng.update(src.getBytes());
byte[] sign1 = hexStrToBytes(sign);
return sigEng.verify(sign1);
byte[] sign1 = hexStrToBytes(sign);
return sigEng.verify(sign1);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String encryptLongTextWithPriKey(String src, String priKey) {
final int ENCRYPT_LENGTH = 117;
if (src.length() <= ENCRYPT_LENGTH) {
return encryptWithPriKey(src, priKey);
}
StringBuffer sb = new StringBuffer();
int idx = 0;
while (idx < src.length()) {
int end = idx + ENCRYPT_LENGTH > src.length() ? src.length() : idx + ENCRYPT_LENGTH;
String sub = src.substring(idx, end);
String encSub = encryptWithPriKey(sub, priKey);
sb.append(encSub);
idx += ENCRYPT_LENGTH;
}
return sb.toString();
}
public String encryptLongTextWithPriKey(String src, String priKey) {
final int ENCRYPT_LENGTH = 117;
if (src.length() <= ENCRYPT_LENGTH) {
return encryptWithPriKey(src, priKey);
}
StringBuffer sb = new StringBuffer();
int idx = 0;
while (idx < src.length()) {
int end = idx + ENCRYPT_LENGTH > src.length() ? src.length() : idx + ENCRYPT_LENGTH;
String sub = src.substring(idx, end);
String encSub = encryptWithPriKey(sub, priKey);
sb.append(encSub);
idx += ENCRYPT_LENGTH;
}
return sb.toString();
}
public String encryptWithPriKeyWithBase64(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key privateKey = fac.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] signature = cipher.doFinal(src.getBytes());
public String encryptWithPriKeyWithBase64(String src, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key privateKey = fac.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] signature = cipher.doFinal(src.getBytes());
return Base64.getEncoder().encodeToString(signature).replaceAll("[^a-zA-Z0-9+/=]", "");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return Base64.getEncoder().encodeToString(signature).replaceAll("[^a-zA-Z0-9+/=]", "");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String encryptWithPubKey(String src, String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
public String encryptWithPubKey(String src, String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key publicKey = fac.generatePublic(keySpec);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key publicKey = fac.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] signature = cipher.doFinal(src.getBytes());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] signature = cipher.doFinal(src.getBytes());
return bytesToHexStr(signature);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return bytesToHexStr(signature);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String decryptWithPriKey(String enc, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
public String decryptWithPriKey(String enc, String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) fac
.generatePrivate(keySpec);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) fac
.generatePrivate(keySpec);
// privateKey.getModulus() + privateKey.getPrivateExponent() +
// privateKey.getAlgorithm();
// privateKey.getModulus() + privateKey.getPrivateExponent() +
// privateKey.getAlgorithm();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] forumcookie = hexStrToBytes(enc);
byte[] forumcookie = hexStrToBytes(enc);
byte[] plainText = cipher.doFinal(forumcookie);
byte[] plainText = cipher.doFinal(forumcookie);
return bytesToHexStr(plainText);
return bytesToHexStr(plainText);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String decryptWithPubKey(String enc, String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
public String decryptWithPubKey(String enc, String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key publicKey = fac.generatePublic(keySpec);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
Key publicKey = fac.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] forumcookie = hexStrToBytes(enc);
byte[] forumcookie = hexStrToBytes(enc);
byte[] plainText = cipher.doFinal(forumcookie);
byte[] plainText = cipher.doFinal(forumcookie);
return new String(plainText);
return new String(plainText);
} catch (Exception e) {
System.err.println(e);
e.printStackTrace(System.err);
return null;
}
}
} catch (Exception e) {
System.err.println(e);
e.printStackTrace(System.err);
return null;
}
}
public RSAPrivateKey getPriKey(String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
public RSAPrivateKey getPriKey(String priKey) {
try {
byte[] pribyte = hexStrToBytes(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey key = (RSAPrivateKey) fac.generatePrivate(keySpec);
return key;
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPrivateKey key = (RSAPrivateKey) fac.generatePrivate(keySpec);
return key;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public RSAPublicKey getPubKey(String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
public RSAPublicKey getPubKey(String pubKey) {
try {
byte[] pubbyte = hexStrToBytes(pubKey.trim());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPublicKey key = (RSAPublicKey) fac.generatePublic(keySpec);
return key;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte);
KeyFactory fac = KeyFactory.getInstance("RSA");
RSAPublicKey key = (RSAPublicKey) fac.generatePublic(keySpec);
return key;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String getPri() {
return priKey;
}
public String getPri() {
return priKey;
}
public void setPri(String pri) {
this.priKey = pri;
}
public void setPri(String pri) {
this.priKey = pri;
}
public String getPub() {
return pubKey;
}
public String getPub() {
return pubKey;
}
public void setPub(String pub) {
this.pubKey = pub;
}
public void setPub(String pub) {
this.pubKey = pub;
}
public String getDescription() {
return description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setDescription(String description) {
this.description = description;
}
}