SQL injection 대비하여 코드 짜기 : 로그인

728x90

 

 

select *
from table_name
where 1=1
      and user_id = 'id'
      and user_pw = 'pw'

 

보통 이런식으로 로그인을 짜게되면 SQL Injection의 위험이 있다

 

예를들어 로그인을 할 때 1=1 --을 넣게되면

select *
from table_name
where 1=1
      and user_id = 'id'
      or 1=1 -- and user_pw = 'pw'

그냥 로그인이 되어버린다

 

 

그래서 아예 id만 가지고 데이터를 아래와 같이 가져온 뒤

select *
from table_name
where 1=1
      and user_id ='id'

 

 

컨트롤러에서 pw를 검증했다.

HashMap<String, Object> retMap = new HashMap<String, Object>();
HashMap<String, Object> userInfoList = new HashMap<String, Object>();

String id = (String) userInfo.get("id");
			String password = (String) userInfo.get("password");
            
userInfoList = (HashMap<String, Object>) Oracledao.selectOne("getUserInfo", id);

			if (userInfoList != null) {
				if (password.equals(userInfoList.get("PW"))) {
					String getId = (String) userInfoList.get("ID");
					String getName = (String) userInfoList.get("NM");
					session.setAttribute("id", getId); // 로그인 정보 o -> 로그인 세션 정보 추가
					session.setAttribute("name", getName); // 로그인 정보 o -> 로그인 세션 정보 추가

					retMap.put("returnCode", "success");
					
				} else {
					retMap.put("returnCode", "fail");
				}
			} else {
				retMap.put("returnCode", "fail");
			}
728x90

'Java' 카테고리의 다른 글

[JAVA] 라이브러리 수동으로 다운로드해서 넣기  (0) 2025.04.09