itsm_ysm/src/main/java/nlib/user/service/impl/LoginServiceImpl.java

264 lines
10 KiB
Java

package nlib.user.service.impl;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import nlib.cmm.service.NlibProperty;
import nlib.cmm.session.SessionConfig;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.security.SecUserVO;
import nlib.user.service.LoginService;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;
@Service("loginService")
public class LoginServiceImpl implements LoginService
{
private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
@Autowired
private AuthenticationManager authenticationManager;
@Resource(name="loginDAO")
private LoginDAO loginDAO;
/*
* 입력된 로그인정보를 받아 로그인 처리한다.
*
* (non-Javadoc)
* @see nlib.user.service.LoginService#login(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)
*/
public String login(HttpServletRequest req, String username, String password) {
// 아이디와 패스워드로, Security 가 알아 볼 수 있는 token 객체로 변경한다.
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
try {
// AuthenticationManager 에 token 을 넘기면 UserDetailsService 가 받아 처리하도록 한다.
Authentication authentication = authenticationManager.authenticate(token);
// AuthKey 등록
SecUserVO userVO = (SecUserVO)authentication.getPrincipal();
userVO.setAuthKey(req.getSession().getId()); // TODO : 임시로 세션ID 넣음, 실제 Authkey 생성 규칙에 따른 해당 키 설정 처리 필요
// 실제 SecurityContext 에 authentication 정보를 등록한다.
SecurityContextHolder.getContext().setAuthentication(authentication);
// TODO : 로그인 로그 정보를 통합자료시스템에 전송한다.
} catch (DisabledException e) {
return "/login/loginForm.do?error=locked";
} catch (LockedException e) {
return "/login/loginForm.do?error=disable";
} catch (BadCredentialsException e) {
return "/login/loginForm.do?error=invalid-password";
} catch (Exception e) {
return "/login/loginForm.do?error=other." + e.toString();
}
return null;
}
/* SNS 연동 SSO를 통한 로그인 처리한다.
*
* return값이 null인 경우, 정상적인 로그인 처리 완료
* null이 아닌 경우, REDIRECT되어야 하는 URL(오류 코드 포함)
*
*/
// public String loginOauth(HttpServletRequest req, String snsUserId) {
//
// // SNS 연동 사용자UID로 사용자정보 조회
// if(StringUtil.isEmpty(snsUserId)) return "/login/loginForm.do?error=req-sns-sso";
//
// NlibLoginVO nLoginVO = loginDAO.selectLoginUserInfo(snsUserId);
//
// if(nLoginVO == null) {
// return "/login/loginForm.do?error=new-membership";
// }
//
// // 아이디와 패스워드로, Security 가 알아 볼 수 있는 token 객체로 변경한다.
// UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(nLoginVO.getLoginUserId(), nLoginVO.getUserPwd());
//
// try {
// // AuthenticationManager 에 token 을 넘기면 UserDetailsService 가 받아 처리하도록 한다.
// Authentication authentication = authenticationManager.authenticate(token);
//
// // AuthKey 등록
// SecUserVO userVO = (SecUserVO)authentication.getPrincipal();
// userVO.getAndCreateAuthKey(req.getSession().getId()); // SET AUTHKEY
//
// // 실제 SecurityContext 에 authentication 정보를 등록한다.
// SecurityContextHolder.getContext().setAuthentication(authentication);
//
// // TODO : 로그인 로그 정보를 통합자료시스템에 전송한다.
// userVO.setAccIp(req.getRemoteAddr());
// userVO.setLoginSucsYn("Y");
// loginDAO.insertLoginLog(userVO);
//
// } catch (DisabledException e) {
// e.printStackTrace();
// return "/login/loginForm.do?error=locked";
// } catch (LockedException e) {
// e.printStackTrace();
// return "/login/loginForm.do?error=disable";
// } catch (BadCredentialsException e) {
// e.printStackTrace();
// return "/login/loginForm.do?error=invalid-password";
// } catch (Exception e) {
// e.printStackTrace();
// return "/login/loginForm.do?error=other&message=" + e.toString();
// }
//
// return null;
// }
//
/* SNS 연동 SSO를 통한 로그인 처리한다.
*
* return값이 null인 경우, 정상적인 로그인 처리 완료
* null이 아닌 경우, 오류 코드
* - REQ-SNS-SSO : SNS 정상적인 로그인이 필요
* - NEW-MEMBERSHIP : 신규회원가입/회원통합 필요
*/
public String loginOauth(HttpServletRequest req, String snsId, String councilJsessionId) {
// SNS 연동 사용자UID로 사용자정보 조회
if(StringUtil.isEmpty(snsId)) return ERR_CODE_REQ_SNSLOGIN;
NlibLoginVO nLoginVO = loginDAO.selectLoginUserInfo(snsId);
if(nLoginVO == null) {
return ERR_CODE_REQ_MEMBERSHIP;
}
// 회원가입정보 존재하는 SNS 아이디임을 확인 완료
return null;
}
public String loginCouncilSSO(HttpServletRequest req, String snsUserId) {
// SNS 연동 사용자UID로 사용자정보 조회
if(StringUtil.isEmpty(snsUserId)) return "/login/loginForm.do?error=req-sns-sso";
NlibLoginVO nLoginVO = loginDAO.selectLoginUserInfo(snsUserId);
if(nLoginVO == null) {
return "/login/loginForm.do?error=new-membership";
}
// 아이디와 패스워드로, Security 가 알아 볼 수 있는 token 객체로 변경한다.
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(nLoginVO.getLoginUserId(), nLoginVO.getPassword());
try {
// AuthenticationManager 에 token 을 넘기면 UserDetailsService 가 받아 처리하도록 한다.
Authentication authentication = authenticationManager.authenticate(token);
// AuthKey 등록
SecUserVO userVO = (SecUserVO)authentication.getPrincipal();
userVO.getOrCreateAuthKey(req.getSession().getId()); // SET AUTHKEY
// 실제 SecurityContext 에 authentication 정보를 등록한다.
SecurityContextHolder.getContext().setAuthentication(authentication);
// TODO : 로그인 로그 정보를 통합자료시스템에 전송한다.
userVO.setIp(req.getRemoteAddr());
loginDAO.insertLoginLog(userVO);
} catch (DisabledException e) {
e.printStackTrace();
return "/login/loginForm.do?error=locked";
} catch (LockedException e) {
e.printStackTrace();
return "/login/loginForm.do?error=disable";
} catch (BadCredentialsException e) {
e.printStackTrace();
return "/login/loginForm.do?error=invalid-password";
} catch (Exception e) {
e.printStackTrace();
return "/login/loginForm.do?error=other&message=" + e.toString();
}
return null;
}
/* SNS 연동 SSO를 통한 로그인 처리한다.
*
* return값이 null인 경우, 정상적인 로그인 처리 완료
* null이 아닌 경우, REDIRECT되어야 하는 URL(오류 코드 포함)
*
*/
public String loginCouncil(HttpServletRequest req, String snsUserId) {
// SNS 연동 사용자UID로 사용자정보 조회
if(StringUtil.isEmpty(snsUserId)) return "/login/loginForm.do?error=req-sns-sso";
NlibLoginVO nLoginVO = loginDAO.selectLoginUserInfo(snsUserId);
if(nLoginVO == null) {
return "/login/loginForm.do?error=new-membership";
}
// 아이디와 패스워드로, Security 가 알아 볼 수 있는 token 객체로 변경한다.
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(nLoginVO.getLoginUserId(), nLoginVO.getPassword());
try {
// AuthenticationManager 에 token 을 넘기면 UserDetailsService 가 받아 처리하도록 한다.
Authentication authentication = authenticationManager.authenticate(token);
// AuthKey 등록
SecUserVO userVO = (SecUserVO)authentication.getPrincipal();
userVO.getOrCreateAuthKey(req.getSession().getId()); // SET AUTHKEY
// 실제 SecurityContext 에 authentication 정보를 등록한다.
SecurityContextHolder.getContext().setAuthentication(authentication);
// TODO : 로그인 로그 정보를 통합자료시스템에 전송한다.
userVO.setIp(req.getRemoteAddr());
loginDAO.insertLoginLog(userVO);
} catch (DisabledException e) {
e.printStackTrace();
return "/login/loginForm.do?error=locked";
} catch (LockedException e) {
e.printStackTrace();
return "/login/loginForm.do?error=disable";
} catch (BadCredentialsException e) {
e.printStackTrace();
return "/login/loginForm.do?error=invalid-password";
} catch (Exception e) {
e.printStackTrace();
return "/login/loginForm.do?error=other&message=" + e.toString();
}
return null;
}
public DataApiResVO logout(DataApiReqVO reqVO) {
return null;
}
public HashMap<String, String> selectCouncilInfoByDnsHost(String councilDnsHost) {
return loginDAO.selectCouncilInfoByDnsHost(councilDnsHost);
}
}