①得到底层HttpSession
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;public class WebUtils {
/**
* 得到requset
*/
public static HttpServletRequest getRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
return request;
}
/**
* 得到session
*/
public static HttpSession getSession() {
return getRequest().getSession();
}}
------------------------------------------------------------------------------------------------------------
②通过Shiro的SecurityUtils对象
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;public class ShiroService {
public void testMethod(){
Session session = SecurityUtils.getSubject().getSession();
Object val = session.getAttribute("key"); }
}