Browse Source

feat: session helper 구현

간단한 security의 principal 가져오는 util 구성
develop
지대한 7 months ago
parent
commit
322103ad9b
  1. 128
      app/kac-app/src/main/java/kr/co/palnet/kac/app/ping/controller/TestErrorContoller.java
  2. 31
      app/kac-app/src/main/java/kr/co/palnet/kac/app/ping/controller/TestSessionContrller.java
  3. 3
      http-client/http/test.http
  4. 3
      web/security/src/main/java/kr/co/palnet/kac/config/security/util/JwtUtil.java
  5. 48
      web/security/src/main/java/kr/co/palnet/kac/config/security/util/SessionHelper.java

128
app/kac-app/src/main/java/kr/co/palnet/kac/app/ping/controller/TestErrorContoller.java

@ -0,0 +1,128 @@
package kr.co.palnet.kac.app.ping.controller;
import kr.co.palnet.kac.core.exception.BaseErrorCode;
import kr.co.palnet.kac.core.exception.BaseException;
import kr.co.palnet.kac.util.EncryptUtil;
import kr.co.palnet.kac.util.KisaEncryptUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/test")
@RestController
public class TestErrorContoller {
@Qualifier("errorMessageSource")
private final MessageSource errorMessageSource;
@Qualifier("errorMessageSourceAccessor")
private final MessageSourceAccessor errorMessageSourceAccessor;
@GetMapping("/encrypt/{plainText}")
public String ecrypt(@PathVariable String plainText) {
String encrypt = KisaEncryptUtil.CbcEncrypt.encrypt(plainText);
log.info("e : {}", encrypt);
String decrypt = KisaEncryptUtil.CbcEncrypt.decrypt(encrypt);
log.info("d : {}", decrypt);
String encrypt1 = EncryptUtil.encrypt(plainText);
log.info("e : {}", encrypt1);
String decrypt1 = EncryptUtil.decrypt(encrypt1);
log.info("d : {}", decrypt1);
return decrypt;
}
@GetMapping("/error/{caseNumber}")
public String errorTest(@PathVariable("caseNumber") Integer caseNumber) throws Exception {
log.info("{}", caseNumber);
return switch (caseNumber) {
case 1 -> throw new BaseException(BaseErrorCode.IO_ERROR);
case 2 -> throw new Exception("test");
default -> "success";
};
}
@GetMapping("/message/locale")
public Map<String, String> locale() {
Map<String, String> map = new HashMap<>();
map.put("message", errorMessageSourceAccessor.getMessage("TEST001"));
map.put("locale", LocaleContextHolder.getLocale().toString());
return map;
}
@GetMapping("/message/error")
public void messageSource() {
Map<String, String> map = new HashMap<>();
try {
String ko_KR = errorMessageSource.getMessage("TEST001", null, Locale.KOREA);
map.put("ko_KR", ko_KR);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String ko = errorMessageSource.getMessage("TEST001", null, Locale.KOREAN);
map.put("ko", ko);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String en = errorMessageSource.getMessage("TEST001", null, Locale.ENGLISH);
map.put("en", en);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String en_US = errorMessageSource.getMessage("TEST001", null, Locale.US);
map.put("en_US", en_US);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
log.info("==========================================================================================");
try {
String df = errorMessageSourceAccessor.getMessage("TEST001");
map.put("df", df);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String str = errorMessageSourceAccessor.getMessage("TEST001", Locale.KOREAN);
map.put("s_ko", str);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String str = errorMessageSourceAccessor.getMessage("TEST001", Locale.KOREA);
map.put("s_ko_kr", str);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String str = errorMessageSourceAccessor.getMessage("TEST001", Locale.ENGLISH);
map.put("s_en", str);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
try {
String str = errorMessageSourceAccessor.getMessage("TEST001", Locale.US);
map.put("s_en_us", str);
} catch (Exception e) {
log.error("{}", e.getMessage());
}
log.info("{}", map);
}
}

31
app/kac-app/src/main/java/kr/co/palnet/kac/app/ping/controller/TestSessionContrller.java

@ -0,0 +1,31 @@
package kr.co.palnet.kac.app.ping.controller;
import kr.co.palnet.kac.config.security.model.BaseUserDetails;
import kr.co.palnet.kac.config.security.util.SessionHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/test/session")
public class TestSessionContrller {
@GetMapping("/auth")
public void test() {
BaseUserDetails userInfo = SessionHelper.getUserInfo();
log.info("userInfo ::: {}", userInfo);
Long userNo = SessionHelper.getCstmrSno();
log.info("userNo ::: {}", userNo);
String userId = SessionHelper.getUserId();
log.info("userId ::: {}", userId);
boolean isRole1 = SessionHelper.hasRole("USER");
log.info("isRole1 ::: {}", isRole1);
boolean isRole2 = SessionHelper.hasRole("ADMIN");
log.info("isRole2 ::: {}", isRole2);
boolean isRole3 = SessionHelper.hasRole("ADMINasdfasdf");
log.info("isRole3 ::: {}", isRole3);
}
}

3
http-client/http/test.http

@ -0,0 +1,3 @@
### session helper test
GET {{host}}/test/session/auth
#Authorization: Bearer {{authToken}}

3
web/security/src/main/java/kr/co/palnet/kac/config/security/util/JwtUtil.java

@ -15,8 +15,7 @@ public class JwtUtil {
// TODO key는 properties에서 가져올수 있도록 처리
private static final Algorithm ALGORITHM = Algorithm.HMAC512("pal-networks");
// 1시간
// private static final long AUTH_TIME = 60 * 60;
private static final long AUTH_TIME = 10;
private static final long AUTH_TIME = 60 * 60;
// 7일
private static final long REFRESH_TIME = 60 * 60 * 24 * 7;

48
web/security/src/main/java/kr/co/palnet/kac/config/security/util/SessionHelper.java

@ -0,0 +1,48 @@
package kr.co.palnet.kac.config.security.util;
import kr.co.palnet.kac.config.security.model.BaseUserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Collection;
import java.util.List;
public class SessionHelper {
public static BaseUserDetails getUserInfo() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(principal.equals("anonymousUser")) {
// 비인증 사용자
return null;
}
try {
BaseUserDetails user = (BaseUserDetails) principal;
return user;
} catch (Exception e){
return null;
}
}
public static Long getCstmrSno() {
BaseUserDetails userInfo = getUserInfo();
if(userInfo == null) return null;
return userInfo.getCstmrSno();
}
public static String getUserId() {
BaseUserDetails userInfo = getUserInfo();
if(userInfo == null) return null;
return userInfo.getUserId();
}
public static boolean hasRole(String role) {
BaseUserDetails userInfo = getUserInfo();
if(userInfo == null) return false;
List<SimpleGrantedAuthority> authorities = userInfo.getAuthorities();
String roleStr = String.format("ROLE_%s", role);
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleStr);
return authorities.contains(authority);
}
}
Loading…
Cancel
Save