2023-05-12 15:32 API 인증

This commit is contained in:
myname 2023-05-12 15:23:04 +09:00
parent 100a9e3011
commit a525a5d8b6
5 changed files with 50 additions and 33 deletions

View File

@ -30,7 +30,9 @@ public class WebConfig implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(certificationInterceptor()) registry.addInterceptor(certificationInterceptor())
.addPathPatterns("/api/**"); .addPathPatterns("/api/accessKey/**")
.addPathPatterns("/api/accessKey/**")
;
//.excludePathPatterns("/css/**", "/images/**", "/js/**"); //.excludePathPatterns("/css/**", "/images/**", "/js/**");
} }

View File

@ -28,5 +28,8 @@ public class AccessKeyVO implements Serializable {
private String accessToken; //access_token 고유번호 private String accessToken; //access_token 고유번호
private String tokenObj; //access_token 고유번호 private String tokenObj; //access_token 고유번호
private String expirePnttm; //access_token 파기일시 private String expirePnttm; //access_token 파기일시
//for call_info
private String callInfo; //access_token 고유번호
} }

View File

@ -9,6 +9,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -42,19 +43,10 @@ public class CertificationInterceptor implements HandlerInterceptor{
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception { throws Exception {
//HttpSession session = request.getSession();
// URL 접속정보
System.out.println("=====preHandle=4=apikey=="); String referer = request.getHeader("Referer");
System.out.println("=====preHandle=4=apikey=="+request.getRequestURI()); //System.out.println("=====preHandle=5=apikey=refer="+referer);
System.out.println("=====preHandle=4=apikey=="+request.getRemoteAddr());
System.out.println("=====preHandle=4=apikey=="+request.getRemoteHost());
System.out.println("=====preHandle=4=apikey=="+request.getRemotePort());
System.out.println("=====preHandle=4=apikey=="+request.getRemoteUser());
System.out.println("=====preHandle=4=apikey=="+request.getRequestedSessionId());
System.out.println("=====preHandle=4=apikey==");
//step0-1.log 남기기 //step0-1.log 남기기
//step0-2.IP 체크 //step0-2.IP 체크
@ -77,7 +69,7 @@ public class CertificationInterceptor implements HandlerInterceptor{
lettnLoginLogVO.setConectId(request.getParameter("mberId")); lettnLoginLogVO.setConectId(request.getParameter("mberId"));
//device type //device type
if(isMobile(request)){ if(_isMobile(request)){
lettnLoginLogVO.setDeviceType("M"); lettnLoginLogVO.setDeviceType("M");
}else{ }else{
lettnLoginLogVO.setDeviceType("P"); lettnLoginLogVO.setDeviceType("P");
@ -98,22 +90,23 @@ public class CertificationInterceptor implements HandlerInterceptor{
lettnLoginLogService.insert(lettnLoginLogVO); lettnLoginLogService.insert(lettnLoginLogVO);
//step0-2.IP 체크 //step0-2.IP 체크(호출IP체크)
lettnLoginLogVO = lettnLoginLogService.selectIgnoreIpCnt(lettnLoginLogVO); lettnLoginLogVO = lettnLoginLogService.selectIgnoreIpCnt(lettnLoginLogVO);
int i_ignoreCnt = lettnLoginLogVO.getCnt(); int i_ignoreCnt = lettnLoginLogVO.getCnt();
//IP 접근 제어
if (i_ignoreCnt>0) { if (i_ignoreCnt>0) {
//제한 아이피인 경우는 화면 안나옴 처리 //제한 아이피인 경우는 화면 안나옴 처리
response.setContentType("application/json"); this._jsonResult(response, HttpStatus.FORBIDDEN);
response.setCharacterEncoding("UTF-8");
response.getWriter().write("{\"resultCode\":\"403\",\"message\":\"Forbidden\"}");
return false; return false;
} }
}catch(Exception ex) { }catch(Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
//내부 오류
this._jsonResult(response, HttpStatus.INTERNAL_SERVER_ERROR);
return false;
} }
} }
@ -124,27 +117,26 @@ public class CertificationInterceptor implements HandlerInterceptor{
AccessKeyVO accessKeyVO = new AccessKeyVO(); AccessKeyVO accessKeyVO = new AccessKeyVO();
accessKeyVO.setAccessKey(request.getParameter("accessKey")); accessKeyVO.setAccessKey(request.getParameter("accessKey"));
accessKeyVO.setMberId(request.getParameter("mberId")); accessKeyVO.setMberId(request.getParameter("mberId"));
accessKeyVO.setCallInfo(referer);
accessKeyVO = accessKeyService.selectRKey(accessKeyVO); accessKeyVO = accessKeyService.selectRKey(accessKeyVO);
//인증 실패
if (accessKeyVO ==null) { if (accessKeyVO ==null) {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("{\"resultCode\":\"401\",\"message\":\"Unauthorized\"}");
this._jsonResult(response, HttpStatus.UNAUTHORIZED);
return false; return false;
} }
}catch(Exception ex) { }catch(Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
response.setContentType("application/json"); //내부 오류
response.setCharacterEncoding("UTF-8"); this._jsonResult(response, HttpStatus.INTERNAL_SERVER_ERROR);
response.getWriter().write("{\"resultCode\":\"401\",\"message\":\"Unauthorized\"}");
return false; return false;
} }
} }
return true; return true;
} }
@ -165,8 +157,11 @@ public class CertificationInterceptor implements HandlerInterceptor{
} }
///////////////////////////////////////////////////////////////////////////////////////////////////
/*모바일접속 유무*/ /*모바일접속 유무*/
private boolean isMobile(HttpServletRequest request) { ///////////////////////////////////////////////////////////////////////////////////////////////////
private boolean _isMobile(HttpServletRequest request) throws Exception{
String userAgent = request.getHeader("user-agent"); String userAgent = request.getHeader("user-agent");
boolean mobile1 = userAgent.matches(".*(iPhone|iPod|Android|Windows CE|BlackBerry|Symbian|Windows Phone|webOS|Opera Mini|Opera Mobi|POLARIS|IEMobile|lgtelecom|nokia|SonyEricsson).*"); boolean mobile1 = userAgent.matches(".*(iPhone|iPod|Android|Windows CE|BlackBerry|Symbian|Windows Phone|webOS|Opera Mini|Opera Mobi|POLARIS|IEMobile|lgtelecom|nokia|SonyEricsson).*");
boolean mobile2 = userAgent.matches(".*(LG|SAMSUNG|Samsung).*"); boolean mobile2 = userAgent.matches(".*(LG|SAMSUNG|Samsung).*");
@ -175,5 +170,16 @@ public class CertificationInterceptor implements HandlerInterceptor{
} }
return false; return false;
} }
private void _jsonResult(
HttpServletResponse p_response
, HttpStatus p_HttpStatus
) throws Exception{
p_response.setContentType("application/json");
p_response.setCharacterEncoding("UTF-8");
p_response.getWriter().write("{\"resultCode\":\""+p_HttpStatus.value()+"\",\"message\":\""+p_HttpStatus.getReasonPhrase()+"\"}");
//return "{\"resultCode\":\""+p_HttpStatus.value()+"\",\"message\":\""+p_HttpStatus.getReasonPhrase()+"\"}";
}
} }

View File

@ -8,6 +8,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.itn.mjonApi.cmn.idgen.mapper.domain.SendMsgVO; import com.itn.mjonApi.cmn.idgen.mapper.domain.SendMsgVO;
@ -46,7 +47,8 @@ public class AccessKeyRestController {
* 2.plain text * 2.plain text
* *
*/ */
@GetMapping("/api/accessKey/SendMsg") @PostMapping("/api/accessKey/SendMsg")
//@GetMapping("/api/accessKey/SendMsg")
public ResponseEntity<RestResponse> apiaccessKeysendMsg( public ResponseEntity<RestResponse> apiaccessKeysendMsg(
@ModelAttribute SendMsgVO sendMsgVO @ModelAttribute SendMsgVO sendMsgVO
){ ){

View File

@ -89,10 +89,14 @@
<include refid="select_column_name"/> <include refid="select_column_name"/>
FROM FROM
<include refid="table_name"/> a <include refid="table_name"/> a
, lettngnrlmber_access_call_info b
WHERE WHERE
1=1 1=1
AND a.ACCESS_KEY = #{accessKey} AND a.ACCESS_KEY = #{accessKey}
AND a.MBER_ID = #{mberId} AND a.MBER_ID = #{mberId}
AND a.ACCESS_no = b.ACCESS_NO
AND b.CALL_INFO LIKE CONCAT(#{callInfo}, '%')
</select> </select>