SPRING/chapter04_MVC
CustomLoginSuccessHandler.java
GAWON
2023. 7. 18. 09:22
package org.joonzis.security;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import lombok.extern.log4j.Log4j;
@Log4j
public class CustomLoginSuccessHandler implements AuthenticationSuccessHandler{
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
log.info("Login Success");
List<String> roleNames =new ArrayList<String>();//인증 리스트 전체를 가져오기
//권한 가져오기
for(GrantedAuthority auth : authentication.getAuthorities()) {
roleNames.add(auth.getAuthority());
}
log.warn("ROLE NAME : " + roleNames);
if(roleNames.contains("ROLE_ADMIN")){
response.sendRedirect("/sample/admin");
return;
}
if(roleNames.contains("ROLE_MEMBER")){
response.sendRedirect("/sample/member");
return;
}
response.sendRedirect("/");
}
}