지대한
7 months ago
5 changed files with 211 additions and 2 deletions
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,3 @@
|
||||
### session helper test |
||||
GET {{host}}/test/session/auth |
||||
#Authorization: Bearer {{authToken}} |
@ -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…
Reference in new issue