Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

CustomLoginSuccessHandler.java 본문

SPRING/chapter08_Security

CustomLoginSuccessHandler.java

GAWON 2023. 7. 13. 10:48
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("/");
	}
}

'SPRING > chapter08_Security' 카테고리의 다른 글

security-context.xml  (0) 2023.07.13
servlet-context.xml  (0) 2023.07.13
CustomAccessDeniedHandler.java  (0) 2023.07.13
SampleController.java  (0) 2023.07.13
CommonController.java  (0) 2023.07.13