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

The following examples show how to use org.springframework.web.util.WebUtils#setSessionAttribute() . 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: UtilsController.java    From tutorials with MIT License 6 votes vote down vote up
@PostMapping("/setParam")
public String post(HttpServletRequest request, Model model) {
    String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT");

    // Long param = ServletRequestUtils.getLongParameter(request, "param",1L);
    // boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true);
    // double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000);
    // float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00);
    // int param = ServletRequestUtils.getIntParameter(request, "param", 100);

    // try {
    // ServletRequestUtils.getRequiredStringParameter(request, "param");
    // } catch (ServletRequestBindingException e) {
    // e.printStackTrace();
    // }

    WebUtils.setSessionAttribute(request, "parameter", param);
    model.addAttribute("parameter", "You set: " + (String) WebUtils.getSessionAttribute(request, "parameter"));
    return "utils";
}
 
Example 2
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 3
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 4
Source File: SessionThemeResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void setThemeName(
		HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable String themeName) {

	WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
			(StringUtils.hasText(themeName) ? themeName : null));
}
 
Example 5
Source File: SessionLocaleResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
	WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
}
 
Example 6
Source File: SessionLocaleResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 7
Source File: UserController.java    From softservice with MIT License 5 votes vote down vote up
@PostMapping("login")
public SingleResult<String> login(@Valid LoginIn loginIn, BindingResult ret, HttpServletRequest request, HttpServletResponse response){
    validate(ret);
    SingleResult<String> result = userService.login(loginIn.getMobile(),loginIn.getPassword());
    if(result.getCode() == Code.SUCCESS.getStatus()){
        WebUtils.setSessionAttribute(request,sessionOpenIdAttribute,result.getData());
    }
    return result;
}
 
Example 8
Source File: SessionLocaleResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 9
Source File: SessionThemeResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void setThemeName(
		HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable String themeName) {

	WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
			(StringUtils.hasText(themeName) ? themeName : null));
}
 
Example 10
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 11
Source File: SessionLocaleResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 12
Source File: SessionFlashMapManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Saves the given FlashMap instances in the HTTP session.
 */
@Override
protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
	WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
}
 
Example 13
Source File: SessionThemeResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
	WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
			(StringUtils.hasText(themeName) ? themeName : null));
}
 
Example 14
Source File: SessionFlashMapManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Saves the given FlashMap instances in the HTTP session.
 */
@Override
protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
	WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
}
 
Example 15
Source File: SessionThemeResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
	WebUtils.setSessionAttribute(request, THEME_SESSION_ATTRIBUTE_NAME,
			(StringUtils.hasText(themeName) ? themeName : null));
}
 
Example 16
Source File: SessionFlashMapManager.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Saves the given FlashMap instances in the HTTP session.
 */
@Override
protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
	WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
}
 
Example 17
Source File: SessionFlashMapManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Saves the given FlashMap instances in the HTTP session.
 */
@Override
protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
	WebUtils.setSessionAttribute(request, FLASH_MAPS_SESSION_ATTRIBUTE, (!flashMaps.isEmpty() ? flashMaps : null));
}