느림보 개발
5. 로그인 후 출력할 페이지 설정 본문
CustomLoginSuccessHandler
- 로그인 후 권한에 따라서 이동할 페이지 설정해주기
package com.ddd.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.jaas.AuthorityGranter;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import lombok.extern.log4j.Log4j;
@Log4j
public class CustomLoginSuccessHandler implements AuthenticationSuccessHandler{
private static final Logger log = LoggerFactory.getLogger(CustomLoginSuccessHandler.class);
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication auth) throws IOException, ServletException {
log.info("로그인 성공");
List<String> roleNames = new ArrayList<>();
auth.getAuthorities().forEach(authority -> {
roleNames.add(authority.getAuthority());
});
log.info("Role Names : " + roleNames);
if(roleNames.contains("ROLE_ADMIN")) {
response.sendRedirect("/");
return;
}
if(roleNames.contains("ROLE_MEMBER")) {
response.sendRedirect("/");
return;
}
response.sendRedirect("/");
}
}
security-context
- 빈으로 등록해준다.
<bean id="customLoginSuccess"
class="com.ddd.security.CustomLoginSuccessHandler"></bean>
<!-- <security:form-login /> -->
<security:form-login login-page="/member/login" authentication-success-handler-ref="customLoginSuccess"/>
'코드 정리 > Spring Security' 카테고리의 다른 글
6. 로그아웃 (0) | 2023.01.21 |
---|---|
4. 커스텀 로그인 페이지 (0) | 2023.01.18 |
3. 접근 제한 메시지의 처리 (0) | 2023.01.17 |
2. 접근 제한 설정 및 여러 권한을 가지는 사용자 설정 (0) | 2023.01.16 |
1. 스프링 시큐리티 기본 설정 (0) | 2023.01.14 |
Comments