초기소스
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.wb.domainserver.security.handler;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); // 1
|
||||
response.setStatus(HttpStatus.FORBIDDEN.value());
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
|
||||
response.getWriter().write("권한 불충분 %s".formatted(accessDeniedException.getMessage())); // 2
|
||||
response.getWriter().flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.wb.domainserver.security.handler;
|
||||
|
||||
import com.wb.domainserver.realm.RealmContextHolder;
|
||||
import com.wb.domainserver.realm.util.UrlExtractor;
|
||||
import com.wb.domainserver.util.CookieUtils;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||
|
||||
@Override
|
||||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
||||
|
||||
String userRole = "";
|
||||
|
||||
//엑세스토큰 유효한 상태에서의 로그아웃
|
||||
if(authentication != null){
|
||||
userRole = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).findFirst().orElse("");
|
||||
}
|
||||
//엑세스토큰 유효하지 않은 상태의 로그아웃
|
||||
else{
|
||||
|
||||
userRole = switch (RealmContextHolder.getSysDvCdRealm()) {
|
||||
case "02" -> "ROLE_ADMIN";
|
||||
case "03" -> "ROLE_MNGR";
|
||||
default -> "ROLE_USER";
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
String originUrlHost = UrlExtractor.getOriginalHost(RealmContextHolder.getOriginalHostRealm());
|
||||
|
||||
log.debug("logout success. userRole : {}", userRole);
|
||||
log.debug("logout success. userRole getRealm : {}", RealmContextHolder.getRealm());
|
||||
log.debug("logout success. userRole getSysDvCdRealm: {}", RealmContextHolder.getSysDvCdRealm());
|
||||
log.debug("logout success. userRole getOriginalHostRealm: {}", originUrlHost);
|
||||
|
||||
// 세션 만료 처리
|
||||
HttpSession session = request.getSession(false);
|
||||
if (session != null) {
|
||||
session.invalidate();
|
||||
}
|
||||
|
||||
// SecurityContextHolder 클리어
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
//로그인 쿠키 경로
|
||||
Map<String, String> cookiePath = CookieUtils.loginCookiePath();
|
||||
|
||||
//관리자 일경우
|
||||
if("ROLE_MNGR".equals(userRole) || "ROLE_ADMIN".equals(userRole)){
|
||||
|
||||
// 쿠키 삭제
|
||||
CookieUtils.deleteCookie("ACT", cookiePath.get("ACTPath"),request, response);
|
||||
CookieUtils.deleteCookie("RFT", cookiePath.get("RFTPath"), request, response);
|
||||
CookieUtils.deleteCookie("token_expire", cookiePath.get("ACTPath"), request, response);
|
||||
response.sendRedirect( "/mngr");
|
||||
|
||||
}else{
|
||||
|
||||
// 쿠키 삭제
|
||||
CookieUtils.deleteCookie("ACT", cookiePath.get("ACTPath"), request, response);
|
||||
CookieUtils.deleteCookie("RFT", cookiePath.get("RFTPath"), request, response);
|
||||
CookieUtils.deleteCookie("token_expire", cookiePath.get("ACTPath"), request, response);
|
||||
|
||||
response.sendRedirect("/sopmal/main");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user