1.在servlet中判断
@RequestMapping(value={"/","/system/index","/common/login"},method=RequestMethod.GET)
public ModelAndView toIndex() throws DaoException {
if (getSessionAttribute(Constant.SESSION_USER) == null) {
return createSuccessModelAndView("public/login", null);
}
setSessionAttribute(Constant.SESSION_MENU, getHeaderInfo());
SysAdmin admin = getSessionUser();
return createSuccessModelAndView("platform/platform-index",optionUtil.getPlatformStatus());
}
2.使用过滤器判断
public class loginFilter implements Filter{
private String dispatchUrl = "";
private String excludeUrl = "";
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
String servletPath = request.getServletPath();
HttpSession session = request.getSession();
String sessionKey = (String) session.getAttribute(Constant.SESSIONKEY);
/*就是登陆界面不进行过滤*/
if(servletPath.equals(dispatchUrl) || servletPath.equals(excludeUrl)){
arg2.doFilter(arg0, arg1);
}else{
if(!StringUtils.isEmpty(sessionKey)){
arg2.doFilter(arg0, arg1);
}else{
request.getRequestDispatcher(dispatchUrl).forward(arg0, arg1);
}
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
dispatchUrl = arg0.getInitParameter("dispatchUrl");
excludeUrl = arg0.getInitParameter("excludeUrl");
}
个人总结:不知道每种方式更适合哪些场景?