From c6ae1eb828ce99914129ef6b47c00e4dd1686b89 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Sun, 26 Jun 2022 18:28:10 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20login=20Security=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=20=20=20=20=20=20=20mpower=20vo=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xit/Application.java | 1 - .../biz/ctgy/v2/service/IBoardService.java | 2 +- .../ctgy/v2/service/impl/BoardService.java | 31 ++----- .../com/xit/core/config/SecurityConfig.java | 7 +- .../core/oauth2/api/dao/RefreshTokenDao.java | 43 ++++++++++ .../core/oauth2/api/entity/RefreshToken.java | 2 + .../OAuth2AuthenticationSuccessHandler.java | 14 +++- src/main/java/com/xit/core/util/DBUtils.java | 82 +++++++++++++++++- .../com/xit/core/util/mpower/MpowerUtils.java | 83 +++++++++++++++++++ .../resources/config/application-oauth.yml | 2 +- .../resources/sql/refreshToken-mapper.xml | 30 +++++++ 11 files changed, 260 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/xit/core/oauth2/api/dao/RefreshTokenDao.java create mode 100644 src/main/resources/sql/refreshToken-mapper.xml diff --git a/src/main/java/com/xit/Application.java b/src/main/java/com/xit/Application.java index 5a4c4ec..93d6d10 100644 --- a/src/main/java/com/xit/Application.java +++ b/src/main/java/com/xit/Application.java @@ -46,7 +46,6 @@ import java.io.IOException; ) ) public class Application { //WebApplicationInitializer { - static final String BEAN_GEN_BASE_PACKAGE = "com.xit.**.controller"; /** * WebFlux main application diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java index 7170dbe..0ece33b 100644 --- a/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java +++ b/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java @@ -10,7 +10,7 @@ import java.util.List; public interface IBoardService { - List> findAll(final BoardDto dto, Pageable pageable); + List findAll(final BoardDto dto, Pageable pageable); // Page findAll2(final MinCivBoard680 entity, Pageable pageable); // diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java index d61d8f8..b29ac39 100644 --- a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java +++ b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java @@ -46,36 +46,19 @@ import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatc public class BoardService implements IBoardService { private final PasswordEncoder passwordEncoder; - private final SqlSessionTemplate sqlSessionTemplate; - private static final String selectBoardListSql = "SELECT MCB.ci_code,\n" + - " MU.name,\n" + - " MCB.ci_contentno,\n" + - " MCB.ci_title,\n" + - " MCB.ci_contents,\n" + - " MCB.ci_nalja,\n" + - " MCB.ci_step,\n" + - " MCB.ci_revel,\n" + - " MCB.ci_ref,\n" + - " MCB.ci_hit,\n" + - " MCB.ci_pass,\n" + - " MCB.ci_id\n" + - " FROM min_civ_board680 MCB\n" + - " LEFT OUTER JOIN min_userinfo MU\n" + - " ON MCB.ci_id = MU.userid\n" + - " ORDER BY MCB.ci_ref DESC, MCB.ci_step ASC, MCB.ci_code ASC"; @Transactional(readOnly = true) - public List> findAll(final BoardDto dto, Pageable pageable) { - System.out.println(DBUtils.getXmlSql("sql/board2-mapper", "selectBoardList")); - String sql = DBUtils.getMybatisSql(sqlSessionTemplate, "board.selectBoardList", dto); + public List findAll(final BoardDto dto, Pageable pageable) { + String sql = DBUtils.getXmlSql("sql/board2-mapper", "selectBoardList"); + //String sql = DBUtils.getMybatisSql(sqlSessionTemplate, "board.selectBoardList", dto); MpowerUtils sendXml = new MpowerUtils(); sendXml.setFeilds("ciCode, ciName, ciContentno, ciTitle, ciContents, ciNalja, ciStep, ciRevel, ciRef, ciHit, ciPass, ciId"); - //sendXml.setQuery(sql); - sendXml.setQuery(selectBoardListSql); -return null; - //return sendXml.selectCustomQuery(); + sendXml.setQuery(sql); + + return sendXml.selectCustomQuery(BoardDto.class); + //return DBUtils.convertToValueObjects(sendXml.selectCustomQuery(), BoardDto.class); } // @Transactional(readOnly = true) diff --git a/src/main/java/com/xit/core/config/SecurityConfig.java b/src/main/java/com/xit/core/config/SecurityConfig.java index a434fb1..af39a97 100644 --- a/src/main/java/com/xit/core/config/SecurityConfig.java +++ b/src/main/java/com/xit/core/config/SecurityConfig.java @@ -4,6 +4,7 @@ import com.xit.biz.ctgy.auth.MinPasswordEncoder; import com.xit.biz.ctgy.auth.service.impl.CustomMinOAuth2UserService; import com.xit.biz.ctgy.auth.service.impl.CustomMinUserDetailsService; import com.xit.biz.ctgy.repository.IMinUserRepository; +import com.xit.core.oauth2.api.dao.RefreshTokenDao; import com.xit.core.oauth2.api.repository.RefreshTokenRepository; import com.xit.core.oauth2.config.properties.AppProperties; import com.xit.core.oauth2.config.properties.CorsProperties; @@ -104,7 +105,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { private final CustomMinUserDetailsService userDetailsService; private final CustomMinOAuth2UserService customOAuth2UserService; private final TokenAccessDeniedHandler tokenAccessDeniedHandler; - private final RefreshTokenRepository refreshTokenRepository; + //private final RefreshTokenRepository refreshTokenRepository; + private final RefreshTokenDao refreshTokenDao; private final IMinUserRepository minUserRepository; @@ -270,7 +272,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { return new OAuth2AuthenticationSuccessHandler( authTokenProvider, appProperties, - refreshTokenRepository, + //refreshTokenRepository, + refreshTokenDao, oAuth2AuthorizationRequestBasedOnCookieRepository() ); } diff --git a/src/main/java/com/xit/core/oauth2/api/dao/RefreshTokenDao.java b/src/main/java/com/xit/core/oauth2/api/dao/RefreshTokenDao.java new file mode 100644 index 0000000..d337603 --- /dev/null +++ b/src/main/java/com/xit/core/oauth2/api/dao/RefreshTokenDao.java @@ -0,0 +1,43 @@ +package com.xit.core.oauth2.api.dao; + +import com.xit.core.oauth2.api.entity.RefreshToken; +import com.xit.core.util.DBUtils; +import com.xit.core.util.mpower.MpowerUtils; + +import java.util.Optional; + +public class RefreshTokenDao { + private static final String sqlXmlFile = "sql/refreshToken-mapper"; + + public Optional findByKey(String key){ + String sql = DBUtils.getXmlSql(sqlXmlFile, "selectRefreshToken"); + sql = sql.replaceFirst(":userId", key); + + MpowerUtils sendXml = new MpowerUtils(); + sendXml.setFeilds("key, value"); + sendXml.setQuery(sql); + return Optional.ofNullable(sendXml.selectCustomQuery(RefreshToken.class).get(0)); + } + + public void save(RefreshToken refreshToken){ + String sql = DBUtils.getXmlSql(sqlXmlFile, "saveRefreshToken"); + sql = sql.replaceFirst(":userId", refreshToken.getKey()); + sql = sql.replaceFirst(":tokenValue", refreshToken.getValue()); + + MpowerUtils sendXml = new MpowerUtils(); + //sendXml.setFeilds("key, value"); + //sendXml.setQuery(sql); + //return Optional.ofNullable(sendXml.selectCustomQuery(RefreshToken.class).get(0)); + } + + public void update(RefreshToken refreshToken){ + String sql = DBUtils.getXmlSql(sqlXmlFile, "updateRefreshToken"); + sql = sql.replaceFirst(":userId", refreshToken.getKey()); + sql = sql.replaceFirst(":tokenValue", refreshToken.getValue()); + + MpowerUtils sendXml = new MpowerUtils(); + //sendXml.setFeilds("key, value"); + //sendXml.setQuery(sql); + //return Optional.ofNullable(sendXml.selectCustomQuery(RefreshToken.class).get(0)); + } +} diff --git a/src/main/java/com/xit/core/oauth2/api/entity/RefreshToken.java b/src/main/java/com/xit/core/oauth2/api/entity/RefreshToken.java index ac29b5f..5b2ef07 100644 --- a/src/main/java/com/xit/core/oauth2/api/entity/RefreshToken.java +++ b/src/main/java/com/xit/core/oauth2/api/entity/RefreshToken.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import javax.persistence.Entity; import javax.persistence.Id; @@ -17,6 +18,7 @@ import javax.persistence.Table; @Table(name = "refresh_token") @Entity @Getter +@Setter @NoArgsConstructor public class RefreshToken { diff --git a/src/main/java/com/xit/core/oauth2/oauth/handler/OAuth2AuthenticationSuccessHandler.java b/src/main/java/com/xit/core/oauth2/oauth/handler/OAuth2AuthenticationSuccessHandler.java index 8a11a1a..3915726 100644 --- a/src/main/java/com/xit/core/oauth2/oauth/handler/OAuth2AuthenticationSuccessHandler.java +++ b/src/main/java/com/xit/core/oauth2/oauth/handler/OAuth2AuthenticationSuccessHandler.java @@ -1,5 +1,6 @@ package com.xit.core.oauth2.oauth.handler; +import com.xit.core.oauth2.api.dao.RefreshTokenDao; import com.xit.core.oauth2.api.entity.RefreshToken; import com.xit.core.oauth2.api.repository.RefreshTokenRepository; import com.xit.core.constant.XitConstants; @@ -37,7 +38,8 @@ import static com.xit.core.oauth2.oauth.repository.OAuth2AuthorizationRequestBas public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { private final JwtTokenProvider tokenProvider; private final AppProperties appProperties; - private final RefreshTokenRepository refreshTokenRepository; + //private final RefreshTokenRepository refreshTokenRepository; + private final RefreshTokenDao refreshTokenDao; private final OAuth2AuthorizationRequestBasedOnCookieRepository authorizationRequestRepository; @Override @@ -80,12 +82,16 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS String refreshToken = tokenProvider.generateJwtRefreshToken(userInfo.getId()); // DB 저장 - RefreshToken savedRefreshToken = refreshTokenRepository.findByKey(userInfo.getId()).orElse(null); + //RefreshToken savedRefreshToken = refreshTokenRepository.findByKey(userInfo.getId()).orElse(null); + RefreshToken savedRefreshToken = refreshTokenDao.findByKey(userInfo.getId()).orElse(null); if (savedRefreshToken != null) { //userRefreshToken(refreshToken); - savedRefreshToken.updateValue(Objects.requireNonNull(refreshToken)); + savedRefreshToken.setValue(refreshToken); + refreshTokenDao.update(Objects.requireNonNull(savedRefreshToken)); + //savedRefreshToken.updateValue(Objects.requireNonNull(refreshToken)); + } else { - refreshTokenRepository.save( + refreshTokenDao.save( RefreshToken.builder() .key(userInfo.getId()) .value(refreshToken) diff --git a/src/main/java/com/xit/core/util/DBUtils.java b/src/main/java/com/xit/core/util/DBUtils.java index 349b183..0775d53 100644 --- a/src/main/java/com/xit/core/util/DBUtils.java +++ b/src/main/java/com/xit/core/util/DBUtils.java @@ -11,12 +11,10 @@ import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import java.io.InputStream; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class DBUtils { /** @@ -97,4 +95,80 @@ public class DBUtils { throw new RuntimeException(e); } } + + public static Map convertToMap(Object obj) { + try { + if (Objects.isNull(obj)) { + return Collections.emptyMap(); + } + Map convertMap = new HashMap<>(); + + Field[] fields = obj.getClass().getDeclaredFields(); + + for (Field field : fields) { + field.setAccessible(true); + convertMap.put(field.getName(), field.get(obj)); + } + return convertMap; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static T convertToValueObject(Map map, Class type) { + try { + Objects.requireNonNull(type, "Class cannot be null"); + T instance = type.getConstructor().newInstance(); + + if (map == null || map.isEmpty()) { + return instance; + } + + for (Map.Entry entry : map.entrySet()) { + Field[] fields = type.getDeclaredFields(); + + for (Field field : fields) { + field.setAccessible(true); + String name = field.getName(); + + boolean isSameType = entry.getValue().getClass().equals(field.getType()); + boolean isSameName = entry.getKey().equals(name); + + if (isSameType && isSameName) { + field.set(instance, map.get(name)); + break; + } + } + } + return instance; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static List> convertToMaps(List list) { + if (list == null || list.isEmpty()) { + return Collections.emptyList(); + } + List> convertList = new ArrayList<>(list.size()); + + for (Object obj : list) { + convertList.add(convertToMap(obj)); + } + return convertList; + } + + public static List convertToValueObjects(List> list, Class type) { + Objects.requireNonNull(type, "Class cannot be null"); + + if (list == null || list.isEmpty()) { + return Collections.emptyList(); + } + List convertList = new ArrayList<>(list.size()); + + for (Map map : list) { + convertList.add(convertToValueObject(map, type)); + } + return convertList; + } } diff --git a/src/main/java/com/xit/core/util/mpower/MpowerUtils.java b/src/main/java/com/xit/core/util/mpower/MpowerUtils.java index 8ebc4cd..b6214be 100644 --- a/src/main/java/com/xit/core/util/mpower/MpowerUtils.java +++ b/src/main/java/com/xit/core/util/mpower/MpowerUtils.java @@ -13,6 +13,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; @@ -403,7 +405,88 @@ public class MpowerUtils { return mList; } + public List selectCustomQuery(Class type) { + List list = new ArrayList<>(); + String fileList = ""; + + for(int cnt = 0;cnt mList = new ArrayList(); + + int mListCount = 0; + try { + mpower = new Client(hostip,port); + mpower.setCryptEnable(false); + mpower.getConnection("MPowerXmlToQuery.xmlQuery1"); + mpower.setInput("SQLXML", query); + mpower.Request(); + String result = mpower.getString("result", 0, 0); + + int row; + if (result.equals("true")){ + row = mpower.getMaxRow("list1"); + if(row>0){ + for(int i=0;i m = new HashMap<>(); + + T instance = null; + try { + instance = type.getConstructor().newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + + if(!"".equals(feild) && feild != null){ + //m.put("cbContent", mpower.getString("list1", i, 4)); + continue; + }else{ + Field[] clsFields = type.getDeclaredFields(); + for(int j=0;j + + + /* refreshToken-mapper|selectRefreshToken|julim */ + SELECT key, + value + FROM refresh_token + WHERE key = :userId + + + + /* refreshToken-mapper|insertRefreshToken|julim */ + INSERT + INTO refresh_token ( + key, + value + ) VALUE ( + :userId, + :tokenValue + ) + + + + /* refreshToken-mapper|updateRefreshToken|julim */ + UPDATE refresh_token + SET value = :tokenValue + WHERE key = :userId + + + \ No newline at end of file