웹 개발

spring 에서 sessionValue 사용 ( session정보를 담은 class 사용 )

노루아부지 2019. 9. 6. 11:17
반응형
import java.io.Serializable;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.commonlogic.menu.MenuVO;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Scope(value= WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component("session")
public class SessionValue implements Serializable{
//	private static final long serialVersionUID = 7508111457112535143L;
	private List<MenuVO> menuTop;
	private List<MenuVO> menuSub;
	public String pageType;

	public String getPageType() {
		return this.pageType;
	}
}

 

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.commonlogic.menu.MenuDao;
import com.commonlogic.menu.MenuVO;
import com.commonlogic.value.SessionValue;

@Component
public class LoginSuccess implements AuthenticationSuccessHandler {

	@Autowired
	MenuDao menuDao;

	@Inject
	Provider<SessionValue> provider;

	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	public LoginSuccess() {
	}

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {

		List<MenuVO> menuTopList = menuDao.getMenuTopList();
		List<MenuVO> menuSubList = menuDao.getMenuSubList();
		SessionValue sessionValue = provider.get();
		sessionValue.setMenuTop(menuTopList);
		sessionValue.setMenuSub(menuSubList);
		sessionValue.setPageType("I");
		redirectStrategy.sendRedirect(request, response, "/main");
		System.out.println("loginSuccess");
	}
}
<c:choose>
	<c:when test="${sessionScope['scopedTarget.session'].pageType == 'I' }">
		<div class="gnb inside">
	</c:when>
	<c:otherwise>
		<div class="gnb outside">
	</c:otherwise>
</c:choose>

 

728x90
반응형
loading