API키 신청
parent
b61cbded64
commit
1be2f3dcfc
|
|
@ -45,21 +45,20 @@ public class ApiController {
|
|||
|
||||
@RequestMapping(value = "createApiKey.do")
|
||||
@ResponseBody
|
||||
public Map<String, Object> UserApiInfo(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String, Object> params) {
|
||||
public Map<String, Object> UserApiInfo(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String, Object> params) throws Exception {
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
|
||||
|
||||
strUtil sUtil = new strUtil();
|
||||
String userType = sUtil.checkNull(parseData.parseData((String)params.get("userType")));
|
||||
|
||||
String apiKey = KeyGenerator.generateUniqueKey();
|
||||
System.out.println("Generated apiKey ==>" + apiKey);
|
||||
log.info("apiKey ==> " + apiKey);
|
||||
log.info("userType ==> " + userType);
|
||||
|
||||
resultMap.put("code", "SUCCESS");
|
||||
resultMap.put("msg", "API 신청이 완료됐습니다.");
|
||||
resultMap.put("data",apiKey);
|
||||
String loginUserId = String.valueOf(request.getSession().getAttribute("USERID"));
|
||||
params.put("userid", loginUserId);
|
||||
params.put("userType", userType);
|
||||
|
||||
apiService.addApiKey(params);
|
||||
//
|
||||
resultMap.put("code", params.get("p_result_code"));
|
||||
resultMap.put("msg", params.get("p_err_msg"));
|
||||
resultMap.put("data",params.get("apiKey"));
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package geoinfo.main.api.service;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -14,4 +15,6 @@ public interface ApiMapper {
|
|||
public List<?> selectInfoList(HashMap<String, Object> params) throws Exception;
|
||||
|
||||
HashMap<String, Object> selectUserLatestKey(HashMap<String, Object> params) throws Exception;
|
||||
|
||||
public HashMap<String, Object> spAddWebApiKey(HashMap<String, Object> params) throws SQLException;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,4 +12,6 @@ public interface ApiService {
|
|||
List<?> selectInfoList(HashMap<String, Object> params) throws Exception;
|
||||
|
||||
HashMap<String, Object> selectUserLatestKey(HashMap<String, Object> params) throws Exception;
|
||||
|
||||
HashMap<String, Object> addApiKey(HashMap<String, Object> params) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
|
|||
import egovframework.rte.psl.dataaccess.util.EgovMap;
|
||||
import geoinfo.main.api.service.ApiMapper;
|
||||
import geoinfo.main.api.service.ApiService;
|
||||
import geoinfo.util.KeyGenerator;
|
||||
|
||||
@Service("apiService")
|
||||
public class ApiServiceImpl implements ApiService{
|
||||
|
|
@ -31,4 +32,50 @@ public class ApiServiceImpl implements ApiService{
|
|||
public HashMap<String, Object> selectUserLatestKey(HashMap<String, Object> params) throws Exception {
|
||||
return ApiMapper.selectUserLatestKey(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, Object> addApiKey(HashMap<String, Object> params) throws Exception {
|
||||
|
||||
HashMap<String, Object> rtnMap = null;
|
||||
String retCode = "";
|
||||
String retMsg = "";
|
||||
final int MAX_RETRY = 10;
|
||||
|
||||
for (int i = 0; i < MAX_RETRY; i++) {
|
||||
// 1. 새 키 생성
|
||||
String apiKey = KeyGenerator.generateUniqueKey();
|
||||
params.put("apiKey", apiKey);
|
||||
|
||||
// 2. SP 호출
|
||||
rtnMap = ApiMapper.spAddWebApiKey(params);
|
||||
retCode = (String) params.get("p_result_code");
|
||||
retMsg = (String) params.get("p_err_msg");
|
||||
|
||||
// 3. 중복(61)이면 새로 생성해서 다시 시도
|
||||
if ("61".equals(retCode)) {
|
||||
System.out.println("중복된 키 감지(" + apiKey + "), 재시도 중...(" + (i + 1) + ")");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 4. 중복이 아니면 루프 탈출
|
||||
break;
|
||||
}
|
||||
|
||||
// 5. 결과에 따라 처리 (switch → if)
|
||||
if ("100".equals(retCode)) {
|
||||
System.out.println("API Key 생성 성공");
|
||||
} else if ("62".equals(retCode)) {
|
||||
System.out.println("관리자 승인 대기: " + retMsg);
|
||||
} else if ("63".equals(retCode)) {
|
||||
System.out.println("유효기간 만료: " + retMsg);
|
||||
} else if ("82".equals(retCode)) {
|
||||
System.out.println("사용자 불가: " + retMsg);
|
||||
} else if ("61".equals(retCode)) {
|
||||
System.out.println("API Key 생성 실패: 중복으로 인해 " + MAX_RETRY + "회 시도 후 중단됨");
|
||||
} else {
|
||||
System.out.println("기타 코드: " + retCode + " (" + retMsg + ")");
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,4 +46,14 @@
|
|||
WHERE ROWNUM = 1
|
||||
</select>
|
||||
|
||||
<!-- API KEY 생성하여 발급 -->
|
||||
<select id="spAddWebApiKey" parameterType="map" statementType="CALLABLE" resultType="HashMap">
|
||||
{ CALL SP_ADD_WEB_API_KEY(
|
||||
#{userid, mode=IN, jdbcType=VARCHAR},
|
||||
#{userType, mode=IN, jdbcType=VARCHAR},
|
||||
#{apiKey, mode=IN, jdbcType=VARCHAR},
|
||||
#{p_result_code, mode=OUT, jdbcType=VARCHAR},
|
||||
#{p_err_msg, mode=OUT, jdbcType=VARCHAR}
|
||||
) }
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -70,7 +70,13 @@
|
|||
data : {userType: userType},
|
||||
dataType :"json",
|
||||
success : function(res){
|
||||
alert("apiKey ==> " + res.data)
|
||||
// alert("apiKey ==> " + res.data)
|
||||
if (res.code == "100") {
|
||||
// alert("신청이 완료됐습니다. \nAPI KEY는 " + res.data + "입니다.")
|
||||
location.reload();
|
||||
} else {
|
||||
alert(res.msg)
|
||||
}
|
||||
},
|
||||
error : function(){
|
||||
alert("오류입니다.");
|
||||
|
|
|
|||
Loading…
Reference in New Issue