authenticationDao -> policyMapper, authenticationBean

master
mjkhan21 2 years ago
parent 32296dccae
commit 35caa909ee

@ -1,166 +0,0 @@
package cokr.xit.base.security.authentication.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.security.core.Authentication;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.foundation.User;
import cokr.xit.foundation.component.AbstractDao;
import cokr.xit.foundation.data.DataObject;
/** DAO
* @author mjkhan
*/
public class AuthenticationDao extends AbstractDao {
/** AuthenticationDao .
*/
public AuthenticationDao() {
setNamespace("authentication");
}
/** .
* @param params
* @return
*/
public User getUser(Map<String, Object> params) {
HashMap<String, Object> copy = new HashMap<>(params);
if (isEmpty(copy.get("userIDs")))
copy.put("userIDs", new Object[] {params.get("userID")});
copy.put("password", true);
List<User> users = selectList(
sqlID("user.getUsers"),
copy
);
return !users.isEmpty() ? users.get(0) : null;
}
/** .
* @param userID
* @return
*/
public User getUser(String userID) {
return getUser(
params().set("userIDs", new String[] {userID})
);
}
/**
* @param authentication
* @return
*/
public int onSuccess(Authentication authentication) {
return 0;
}
/**
* @param params
* @return
*/
public int onFailure(Map<String, String> params) {
return 0;
}
/**
* @param authentication
* @return
*/
public int onLogout(Authentication authentication) {
return 0;
}
/** .
* @param by
* <ul><li>userName - </li>
* <li> </li>
* </ul>
* @param term
* @param pageNum
* @param fetchSize
* @return
*/
public List<DataObject> getPolicyList(String by, String term, int pageNum, int fetchSize) {
switch (ifEmpty(by, () -> "")) {
case "userName": by = "USER_NM"; break;
default: by = "USER_ID"; break;
}
DataObject params = params()
.set("by", by)
.set("term", ifEmpty(term, () -> null))
.set("pageNum", pageNum)
.set("fetchSize", fetchSize);
return selectList(sqlID("getPolicyList"), params);
}
/** .
* @param userIDs
* @return
*/
public List<AuthenticationPolicy> getPolicies(String... userIDs) {
DataObject params = params()
.set("userIDs", ifEmpty(userIDs, () -> null))
.set("pageNum", 1)
.set("fetchSize", userIDs.length);
return selectList(sqlID("getPolicies"), params);
}
/** .
* @param userID
* @return
*/
public AuthenticationPolicy getPolicy(String userID) {
List<AuthenticationPolicy> found = getPolicies(userID);
return !found.isEmpty() ? found.get(0) : null;
}
/** .
* @param policy
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean create(AuthenticationPolicy policy) {
if (policy == null)
return false;
AuthenticationPolicy found = getPolicy(policy.getUserID());
if (found != null)
throw applicationException(null);
DataObject params = params()
.set("policy", policy)
.set("currentUser", currentUser());
return insert(sqlID("insertPolicy"), params) > 0;
}
/** .
* @param policy
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean update(AuthenticationPolicy policy) {
if (policy == null)
return false;
DataObject params = params()
.set("policy", policy)
.set("currentUser", currentUser());
return insert(sqlID("updatePolicy"), params) > 0;
}
/** .
* @param userIDs
* @return
*/
public int removePolicies(String... userIDs) {
if (isEmpty(userIDs)) return 0;
return delete(sqlID("removePolicy"), params().set("userIDs", userIDs));
}
}

@ -0,0 +1,120 @@
package cokr.xit.base.security.authentication.dao;
import java.util.List;
import java.util.Map;
import org.egovframe.rte.psl.dataaccess.mapper.Mapper;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.foundation.component.AbstractMapper;
import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.data.DataObject;
/** DAO
* @author mjkhan
*/
@Mapper("policyMapper")
public interface PolicyMapper extends AbstractMapper {
/** .
* @param by
* <ul><li>userName - </li>
* <li> </li>
* </ul>
* @param term
* @param pageNum
* @param fetchSize
* @return
*/
List<DataObject> getPolicyList(QueryRequest req);
/** .
* @param userIDs
* @return
*/
List<AuthenticationPolicy> getPolicies(Map<String, ?> params);
/** .
* @param userIDs
* @return
*/
default List<AuthenticationPolicy> getPolicies(String... userIDs) {
DataObject params = params()
.set("userIDs", ifEmpty(userIDs, () -> null))
.set("pageNum", 1)
.set("fetchSize", userIDs.length);
return getPolicies(params);
}
/** .
* @param userID
* @return
*/
default AuthenticationPolicy getPolicy(String userID) {
List<AuthenticationPolicy> found = getPolicies(userID);
return !found.isEmpty() ? found.get(0) : null;
}
int insertPolicy(Map<String, ?> params);
/** .
* @param policy
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
default boolean create(AuthenticationPolicy policy) {
if (policy == null)
return false;
AuthenticationPolicy found = getPolicy(policy.getUserID());
if (found != null)
throw applicationException(null);
DataObject params = params()
.set("policy", policy)
.set("currentUser", currentUser());
return insertPolicy(params) > 0;
}
/** .
* @param policy
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
int updatePolicy(Map<String, ?> params);
/** .
* @param policy
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
default boolean update(AuthenticationPolicy policy) {
return policy != null
&& updatePolicy(
params()
.set("policy", policy)
.set("currentUser", currentUser())
) > 0;
}
/** .
* @param userIDs
* @return
*/
int removePolicy(Map<String, ?> params);
/** .
* @param userIDs
* @return
*/
default int removePolicies(String... userIDs) {
return !isEmpty(userIDs) ?
removePolicy(params().set("userIDs", userIDs)) :
0;
}
}

@ -7,6 +7,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetailsService;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.data.DataObject;
/** .
@ -44,7 +45,7 @@ public interface AuthenticationService extends UserDetailsService {
* @param fetchSize
* @return
*/
List<DataObject> getPolicyList(String by, String term, int pageNum, int fetchSize);
List<DataObject> getPolicyList(QueryRequest req);
/** .
* @param userIDs
@ -56,10 +57,7 @@ public interface AuthenticationService extends UserDetailsService {
* @param userID
* @return
*/
default AuthenticationPolicy getPolicy(String userID) {
List<AuthenticationPolicy> list = getPolicies(userID);
return !list.isEmpty() ? list.get(0) : null;
}
AuthenticationPolicy getPolicy(String userID);
/** .
* @param policy

@ -6,20 +6,26 @@ import java.util.Map;
import javax.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.base.security.authentication.dao.PolicyMapper;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.user.service.UserQuery;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.User;
import cokr.xit.foundation.component.AbstractDao;
import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.data.DataObject;
/** DAO
* @author mjkhan
*/
public class AuthenticationBean extends AbstractDao {
@Component("authenticationBean")
public class AuthenticationBean extends AbstractComponent {
@Resource(name = "userMapper")
private UserMapper userMapper;
@Resource(name = "policyMapper")
private PolicyMapper policyMapper;
/** .
* @param params
@ -40,7 +46,7 @@ public class AuthenticationBean extends AbstractDao {
*/
public User getUser(String userID) {
return getUser(
params().set("userIDs", new String[] {userID})
new DataObject().set("userIDs", new String[] {userID})
);
}
@ -78,18 +84,13 @@ public class AuthenticationBean extends AbstractDao {
* @param fetchSize
* @return
*/
public List<DataObject> getPolicyList(String by, String term, int pageNum, int fetchSize) {
switch (ifEmpty(by, () -> "")) {
case "userName": by = "USER_NM"; break;
default: by = "USER_ID"; break;
public List<DataObject> getPolicyList(QueryRequest req) {
switch (ifEmpty(req.getBy(), () -> "")) {
case "userName": req.setBy("USER_NM"); break;
default: req.setBy("USER_ID"); break;
}
DataObject params = params()
.set("by", by)
.set("term", ifEmpty(term, () -> null))
.set("pageNum", pageNum)
.set("fetchSize", fetchSize);
return selectList(sqlID("getPolicyList"), params);
req.setOrderBy(req.getBy());
return policyMapper.getPolicyList(req);
}
/** .
@ -97,11 +98,7 @@ public class AuthenticationBean extends AbstractDao {
* @return
*/
public List<AuthenticationPolicy> getPolicies(String... userIDs) {
DataObject params = params()
.set("userIDs", ifEmpty(userIDs, () -> null))
.set("pageNum", 1)
.set("fetchSize", userIDs.length);
return selectList(sqlID("getPolicies"), params);
return policyMapper.getPolicies(userIDs);
}
/** .
@ -109,8 +106,7 @@ public class AuthenticationBean extends AbstractDao {
* @return
*/
public AuthenticationPolicy getPolicy(String userID) {
List<AuthenticationPolicy> found = getPolicies(userID);
return !found.isEmpty() ? found.get(0) : null;
return policyMapper.getPolicy(userID);
}
/** .
@ -121,17 +117,7 @@ public class AuthenticationBean extends AbstractDao {
* </ul>
*/
public boolean create(AuthenticationPolicy policy) {
if (policy == null)
return false;
AuthenticationPolicy found = getPolicy(policy.getUserID());
if (found != null)
throw applicationException(null);
DataObject params = params()
.set("policy", policy)
.set("currentUser", currentUser());
return insert(sqlID("insertPolicy"), params) > 0;
return policyMapper.create(policy);
}
/** .
@ -142,13 +128,7 @@ public class AuthenticationBean extends AbstractDao {
* </ul>
*/
public boolean update(AuthenticationPolicy policy) {
if (policy == null)
return false;
DataObject params = params()
.set("policy", policy)
.set("currentUser", currentUser());
return insert(sqlID("updatePolicy"), params) > 0;
return policyMapper.update(policy);
}
/** .
@ -156,8 +136,6 @@ public class AuthenticationBean extends AbstractDao {
* @return
*/
public int removePolicies(String... userIDs) {
if (isEmpty(userIDs)) return 0;
return delete(sqlID("removePolicy"), params().set("userIDs", userIDs));
return policyMapper.removePolicies(userIDs);
}
}

@ -19,7 +19,6 @@ import cokr.xit.base.security.Authority;
import cokr.xit.base.security.SecuredUserInfo;
import cokr.xit.base.security.access.dao.AuthorityMapper;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.base.security.authentication.dao.AuthenticationDao;
import cokr.xit.base.security.authentication.service.AuthenticationService;
import cokr.xit.base.security.authentication.web.AuthenticationExtraDetails;
import cokr.xit.foundation.Access;
@ -27,6 +26,7 @@ import cokr.xit.foundation.ApplicationContainer;
import cokr.xit.foundation.Log;
import cokr.xit.foundation.User;
import cokr.xit.foundation.UserInfo;
import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.data.DataObject;
/**
@ -36,9 +36,9 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
/** 애플리케이션 컨테이너 */
@Resource(name="applicationContainer")
protected ApplicationContainer applicationContainer;
/** 사용자 인증 DAO */
@Resource(name="authenticationDao")
private AuthenticationDao authenticationDao;
/** 사용자 인증 Bean */
@Resource(name="authenticationBean")
private AuthenticationBean authenticationBean;
/** 접근 권한 정보 */
@Resource(name="accessContext")
protected AccessContext accessContext;
@ -60,16 +60,9 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
return Log.get(getClass());
}
/** DAO .
* @return DAO
*/
protected AuthenticationDao authenticationDao() {
return authenticationDao;
}
@Override
public SecuredUserInfo loadUserByUsername(String username) throws UsernameNotFoundException {
User user = authenticationDao.getUser(username);
User user = authenticationBean.getUser(username);
if (user == null)
new BadCredentialsException("Bad credentials");
@ -110,7 +103,7 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
* @param user
*/
protected void checkAgainstPolicy(AuthenticationExtraDetails extraDetails, UserInfo user) {
AuthenticationPolicy policy = authenticationDao.getPolicy(user.getId());
AuthenticationPolicy policy = authenticationBean.getPolicy(user.getId());
if (policy == null || !policy.limits()) return;
String clientAddress = Access.getClientAddress(extraDetails.getRemoteAddress(), applicationContainer.getHostAddress());
@ -123,7 +116,7 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
* @return
*/
private SecuredUserInfo loadUser(Map<String, Object> params) {
User user = authenticationDao.getUser(params);
User user = authenticationBean.getUser(params);
if (user == null)
throw new BadCredentialsException("Bad credentials");
@ -182,19 +175,19 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
@Override
public void onSuccess(Authentication authentication) {
log().debug("{} authenticated.", authentication.getPrincipal());
authenticationDao.onSuccess(authentication);
authenticationBean.onSuccess(authentication);
}
@Override
public void onFailure(Map<String, String> params) {
log().debug("Authentication failed: {}", params);
authenticationDao.onFailure(params);
authenticationBean.onFailure(params);
}
@Override
public void onLogout(Authentication authentication) {
log().debug("{} logged out.", authentication.getPrincipal());
authenticationDao.onLogout(authentication);
authenticationBean.onLogout(authentication);
}
@Override
@ -203,27 +196,32 @@ public class AuthenticationServiceBean extends DaoAuthenticationProvider impleme
}
@Override
public List<DataObject> getPolicyList(String by, String term, int pageNum, int fetchSize) {
return authenticationDao.getPolicyList(by, term, pageNum, fetchSize);
public List<DataObject> getPolicyList(QueryRequest req) {
return authenticationBean.getPolicyList(req);
}
@Override
public List<AuthenticationPolicy> getPolicies(String... userIDs) {
return authenticationDao.getPolicies(userIDs);
return authenticationBean.getPolicies(userIDs);
}
@Override
public AuthenticationPolicy getPolicy(String userID) {
return authenticationBean.getPolicy(userID);
}
@Override
public boolean create(AuthenticationPolicy policy) {
return authenticationDao.create(policy);
return authenticationBean.create(policy);
}
@Override
public boolean update(AuthenticationPolicy policy) {
return authenticationDao.update(policy);
return authenticationBean.update(policy);
}
@Override
public int removePolicies(String... userIDs) {
return authenticationDao.removePolicies(userIDs);
return authenticationBean.removePolicies(userIDs);
}
}

@ -16,6 +16,7 @@ import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -27,6 +28,7 @@ import cokr.xit.foundation.data.StringMap;
/** ()
* @author mjkhan
*/
@Component("authenticationFailure")
public class AuthenticationFailure extends SimpleUrlAuthenticationFailureHandler {
@Resource(name="objectMapper")
private ObjectMapper objectMapper;

@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -22,6 +23,7 @@ import cokr.xit.foundation.data.DataObject;
/** ()
* @author mjkhan
*/
@Component("authenticationSuccess")
public class AuthenticationSuccess extends SavedRequestAwareAuthenticationSuccessHandler {
@Resource(name="authenticationService")
private AuthenticationService authenticationService;

@ -18,6 +18,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import cokr.xit.base.security.authentication.service.AuthenticationService;
@ -27,6 +28,7 @@ import cokr.xit.foundation.Log;
/**
* @author mjkhan
*/
@Component("logoutSuccess")
public class LogoutSuccess extends SimpleUrlLogoutSuccessHandler implements ApplicationContextAware, HttpSessionListener {
private String successUrl;

@ -4,11 +4,11 @@ import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import cokr.xit.base.security.authentication.AuthenticationPolicy;
import cokr.xit.base.security.authentication.service.AuthenticationService;
import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.web.AbstractController;
/** <br />
@ -44,11 +44,10 @@ public class PolicyController<T extends AuthenticationPolicy> extends AbstractCo
* }</code></pre>
*/
@RequestMapping(name="인증 정책 조회", value="/list.do")
public ModelAndView getPolicies(String by, String term, @RequestParam(required=false, defaultValue="1") Integer pageNum) {
int fetchSize = properties.getInt("pageSize");
public ModelAndView getPolicies(QueryRequest req) {
return setCollectionInfo(
new ModelAndView("jsonView"),
authenticationService.getPolicyList(by, term, pageNum, fetchSize),
authenticationService.getPolicyList(setFetchSize(req)),
"policy"
);
}

@ -8,7 +8,7 @@
<security:http pattern="/resources/**" security="none"/>
<security:http auto-config="true" create-session="never" use-expressions="true">
<security:http auto-config="true" create-session="never" use-expressions="true" access-decision-manager-ref="accessDecisionManager">
<security:csrf />
<security:headers />
@ -49,12 +49,7 @@
<bean id="authenticationService" class="cokr.xit.base.security.authentication.service.bean.AuthenticationServiceBean">
<property name="passwordEncoder" ref="passwordEncoder" />
</bean>
<bean id="authenticationDao" class="cokr.xit.base.security.authentication.dao.AuthenticationDao" />
<bean id="authenticationDetailsSource" class="cokr.xit.base.security.authentication.web.AuthenticationExtraDetailsSource" />
<bean id="authenticationSuccess" class="cokr.xit.base.security.authentication.web.AuthenticationSuccess" />
<bean id="authenticationFailure" class="cokr.xit.base.security.authentication.web.AuthenticationFailure" />
<bean id="logoutSuccess" class="cokr.xit.base.security.authentication.web.LogoutSuccess" />
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
<constructor-arg>
@ -67,6 +62,5 @@
<bean id="applicationAccess" class="cokr.xit.base.security.access.ApplicationAccess">
<property name="controlAccess" value="false"/>
</bean>
<bean id="accessContext" class="cokr.xit.base.security.access.AccessContext" />
</beans>

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="authentication">
<mapper namespace="cokr.xit.base.security.authentication.dao.PolicyMapper">
<resultMap id="policyRow" type="cokr.xit.base.security.authentication.AuthenticationPolicy">
<result property="userID" column="USER_ID"/>
@ -13,7 +13,7 @@
<result property="lastModified" column="UPD_DT"/>
</resultMap>
<select id="getPolicyList" parameterType="map" resultType="dataobject">/* 로그인 정책 목록 조회(authentication.getPolicyList) */
<select id="getPolicyList" parameterType="map" resultType="dataobject">/* 로그인 정책 목록 조회(policyMapper.getPolicyList) */
<include refid="utility.paging-prefix"/>
SELECT A.USER_ID
, USER_NM
@ -26,13 +26,13 @@ SELECT A.USER_ID
<if test="term != null">WHERE A.${by} LIKE CONCAT('%', #{term}, '%')</if>
<include refid="utility.paging-suffix"/></select>
<select id="getPolicies" parameterType="map" resultMap="policyRow">/* 로그인 정책 가져오기(authentication.getPolicies) */
<select id="getPolicies" parameterType="map" resultMap="policyRow">/* 로그인 정책 가져오기(policyMapper.getPolicies) */
SELECT *
FROM TBL_LOGIN_POLICY
<if test="userIDs != null">WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</if>
ORDER BY USER_ID</select>
<insert id="insertPolicy" parameterType="map">/* 로그인 정책 등록(authentication.insertPolicy) */
<insert id="insertPolicy" parameterType="map">/* 로그인 정책 등록(policyMapper.insertPolicy) */
INSERT INTO TBL_LOGIN_POLICY (
USER_ID
, IP_ADRS
@ -53,7 +53,7 @@ INSERT INTO TBL_LOGIN_POLICY (
, CURRENT_TIMESTAMP()
)</insert>
<update id="updatePolicy" parameterType="map">/* 로그인 정책 수정(authentication.updatePolicy) */
<update id="updatePolicy" parameterType="map">/* 로그인 정책 수정(policyMapper.updatePolicy) */
UPDATE TBL_LOGIN_POLICY SET
IP_ADRS = #{policy.ipAddress}
, DPLCT_YN = #{policy.duplicateYN}
@ -62,7 +62,7 @@ UPDATE TBL_LOGIN_POLICY SET
, UPD_DT = CURRENT_TIMESTAMP()
WHERE USER_ID = #{policy.userID}</update>
<delete id="removePolicy" parameterType="map">/* 로그인 정책 삭제(authentication.removePolicy) */
<delete id="removePolicy" parameterType="map">/* 로그인 정책 삭제(policyMapper.removePolicy) */
DELETE FROM TBL_LOGIN_POLICY
WHERE USER_ID IN (<foreach collection="userIDs" item="userID" separator=",">#{userID}</foreach>)</delete>
Loading…
Cancel
Save