Java Code Examples for org.springframework.web.util.WebUtils#getSessionAttribute()

The following examples show how to use org.springframework.web.util.WebUtils#getSessionAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignonInterceptor.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	if (userSession == null) {
		String url = request.getServletPath();
		String query = request.getQueryString();
		ModelAndView modelAndView = new ModelAndView("SignonForm");
		if (query != null) {
			modelAndView.addObject("signonForwardAction", url+"?"+query);
		}
		else {
			modelAndView.addObject("signonForwardAction", url);
		}
		throw new ModelAndViewDefiningException(modelAndView);
	}
	else {
		return true;
	}
}
 
Example 2
Source File: SessionLocaleResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
Example 3
Source File: UserSessionInterceptor.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER_KEY);
    if (user != null) {
        return true;
    }
    Object obj = WebUtils.getSessionMutex(request.getSession());
    synchronized (obj) {
        log.debug("WorkflowUser not found in session under key: " + SESSION_USER_KEY);
        String currentUser = request.getParameter(PARAMETER_USER_KEY);
        if (Strings.isNullOrEmpty(currentUser)) {
            throw new InvalidAccessException("No user found");
        }
        user = identityService.createUserQuery().userId(currentUser).singleResult();
        if (user == null) {
            throw new InvalidAccessException("WorkflowUser: " + currentUser + " is unknown");
        }
        WebUtils.setSessionAttribute(request, SESSION_USER_KEY, user);
        return true;
    }
}
 
Example 4
Source File: WebHelper.java    From java-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否是手机访问
 */
public static boolean isMobileAccess(HttpServletRequest request) {
	boolean isFromMobile = false;
	// 检查是否已经记录访问方式(移动端或pc端)
	Object ua = WebUtils.getSessionAttribute(request, "ua");
	if (null == ua) {
		try {
			String userAgent = request.getHeader("USER-AGENT").toLowerCase();
			if (null == userAgent) {
				userAgent = "";
			}
			isFromMobile = checkUserAgent(userAgent);
			// 判断是否为移动端访问
			if (isFromMobile) {
				WebUtils.setSessionAttribute(request, "ua", "mobile");
			} else {
				WebUtils.setSessionAttribute(request, "ua", "pc");
			}
		} catch (Exception e) {
		}
	} else {
		isFromMobile = ua.equals("mobile");
	}

	return isFromMobile;
}
 
Example 5
Source File: SessionLocaleResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		@Nullable
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
Example 6
Source File: ViewCartController.java    From jpetstore-kubernetes with Apache License 2.0 6 votes vote down vote up
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	Cart cart = (Cart) WebUtils.getOrCreateSessionAttribute(request.getSession(), "sessionCart", Cart.class);
	String page = request.getParameter("page");
	if (userSession != null) {
		if ("next".equals(page)) {
			userSession.getMyList().nextPage();
		}
		else if ("previous".equals(page)) {
			userSession.getMyList().previousPage();
		}
	}
	if ("nextCart".equals(page)) {
		cart.getCartItemList().nextPage();
	}
	else if ("previousCart".equals(page)) {
		cart.getCartItemList().previousPage();
	}
	return new ModelAndView(this.successView, "cart", cart);
}
 
Example 7
Source File: SessionLocaleResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
Example 8
Source File: SignonInterceptor.java    From jpetstore-kubernetes with Apache License 2.0 6 votes vote down vote up
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	if (userSession == null) {
		String url = request.getServletPath();
		String query = request.getQueryString();
		ModelAndView modelAndView = new ModelAndView("SignonForm");
		if (query != null) {
			modelAndView.addObject("signonForwardAction", url+"?"+query);
		}
		else {
			modelAndView.addObject("signonForwardAction", url);
		}
		throw new ModelAndViewDefiningException(modelAndView);
	}
	else {
		return true;
	}
}
 
Example 9
Source File: SessionLocaleResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public LocaleContext resolveLocaleContext(final HttpServletRequest request) {
	return new TimeZoneAwareLocaleContext() {
		@Override
		public Locale getLocale() {
			Locale locale = (Locale) WebUtils.getSessionAttribute(request, localeAttributeName);
			if (locale == null) {
				locale = determineDefaultLocale(request);
			}
			return locale;
		}
		@Override
		public TimeZone getTimeZone() {
			TimeZone timeZone = (TimeZone) WebUtils.getSessionAttribute(request, timeZoneAttributeName);
			if (timeZone == null) {
				timeZone = determineDefaultTimeZone(request);
			}
			return timeZone;
		}
	};
}
 
Example 10
Source File: SessionLocaleResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Locale resolveLocale(HttpServletRequest request) {
	Locale locale = (Locale) WebUtils.getSessionAttribute(request, this.localeAttributeName);
	if (locale == null) {
		locale = determineDefaultLocale(request);
	}
	return locale;
}
 
Example 11
Source File: AccountFormController.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected Object formBackingObject(HttpServletRequest request) throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	if (userSession != null) {
		return new AccountForm(this.petStore.getAccount(userSession.getAccount().getUsername()));
	}
	else {
		return new AccountForm();
	}
}
 
Example 12
Source File: SessionLocaleResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Locale resolveLocale(HttpServletRequest request) {
	Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
	if (locale == null) {
		locale = determineDefaultLocale(request);
	}
	return locale;
}
 
Example 13
Source File: SessionLocaleResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Locale resolveLocale(HttpServletRequest request) {
	Locale locale = (Locale) WebUtils.getSessionAttribute(request, this.localeAttributeName);
	if (locale == null) {
		locale = determineDefaultLocale(request);
	}
	return locale;
}
 
Example 14
Source File: AccountFormController.java    From jpetstore-kubernetes with Apache License 2.0 5 votes vote down vote up
protected Object formBackingObject(HttpServletRequest request) throws Exception {
	UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
	if (userSession != null) {
		return new AccountForm(this.petStore.getAccount(userSession.getAccount().getUsername()));
	}
	else {
		return new AccountForm();
	}
}
 
Example 15
Source File: SessionLocaleResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Locale resolveLocale(HttpServletRequest request) {
	Locale locale = (Locale) WebUtils.getSessionAttribute(request, this.localeAttributeName);
	if (locale == null) {
		locale = determineDefaultLocale(request);
	}
	return locale;
}
 
Example 16
Source File: LogonUtils.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static LogonStateBundle getBundle(HttpServletRequest request, boolean create) {
    Object object = WebUtils.getSessionAttribute(request, BUNDLE_KEY);

    if (object instanceof LogonStateBundle) {
        return (LogonStateBundle) object;
    }

    if (create) {
        LogonStateBundle bundle = new LogonStateBundle(LogonState.PENDING);
        WebUtils.setSessionAttribute(request, BUNDLE_KEY, bundle);
        return bundle;
    }

    return null;
}
 
Example 17
Source File: SessionThemeResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public String resolveThemeName(HttpServletRequest request) {
	String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
	// A specific theme indicated, or do we need to fallback to the default?
	return (themeName != null ? themeName : getDefaultThemeName());
}
 
Example 18
Source File: SessionThemeResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String resolveThemeName(HttpServletRequest request) {
	String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
	// A specific theme indicated, or do we need to fallback to the default?
	return (themeName != null ? themeName : getDefaultThemeName());
}
 
Example 19
Source File: UtilsController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping("/other")
public String other(HttpServletRequest request, Model model) {
    String param = (String) WebUtils.getSessionAttribute(request, "parameter");
    model.addAttribute("parameter", param);
    return "other";
}
 
Example 20
Source File: SessionThemeResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public String resolveThemeName(HttpServletRequest request) {
	String themeName = (String) WebUtils.getSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME);
	// A specific theme indicated, or do we need to fallback to the default?
	return (themeName != null ? themeName : getDefaultThemeName());
}