java sessionid_Java SessionKey.getSessionId方法代码示例

import org.apache.shiro.session.mgt.SessionKey; //导入方法依赖的package包/类

/**

* Creates a {@link RemoteInvocation} with the current session ID as an

* {@link RemoteInvocation#getAttribute(String) attribute}.

*

* @param mi the method invocation that the remote invocation should be based on.

* @return a remote invocation object containing the current session ID as an attribute.

*/

public RemoteInvocation createRemoteInvocation(MethodInvocation mi) {

Serializable sessionId = null;

String host = null;

boolean sessionManagerMethodInvocation = false;

//If the calling MI is for a remoting SessionManager delegate, we need to acquire the session ID from the method

//argument and NOT interact with SecurityUtils/subject.getSession to avoid a stack overflow

Class miDeclaringClass = mi.getMethod().getDeclaringClass();

if (SessionManager.class.equals(miDeclaringClass) || NativeSessionManager.class.equals(miDeclaringClass)) {

sessionManagerMethodInvocation = true;

//for SessionManager calls, all method calls except the 'start' methods require a SessionKey

// as the first argument, so just get it from there:

if (!mi.getMethod().getName().equals("start")) {

SessionKey key = (SessionKey) mi.getArguments()[0];

sessionId = key.getSessionId();

}

}

//tried the delegate. Use the injected session id if given

if (sessionId == null) sessionId = this.sessionId;

// If sessionId is null, only then try the Subject:

if (sessionId == null) {

try {

// HACK Check if can get the securityManager - this'll cause an exception if it's not set

SecurityUtils.getSecurityManager();

if (!sessionManagerMethodInvocation) {

Subject subject = SecurityUtils.getSubject();

Session session = subject.getSession(false);

if (session != null) {

sessionId = session.getId();

host = session.getHost();

}

}

}

catch (Exception e) {

log.trace("No security manager set. Trying next to get session id from system property");

}

}

//No call to the sessionManager, and the Subject doesn't have a session. Try a system property

//as a last result:

if (sessionId == null) {

if (log.isTraceEnabled()) {

log.trace("No Session found for the currently executing subject via subject.getSession(false). " +

"Attempting to revert back to the 'shiro.session.id' system property...");

}

sessionId = System.getProperty(SESSION_ID_SYSTEM_PROPERTY_NAME);

if (sessionId == null && log.isTraceEnabled()) {

log.trace("No 'shiro.session.id' system property found. Heuristics have been exhausted; " +

"RemoteInvocation will not contain a sessionId.");

}

}

RemoteInvocation ri = new RemoteInvocation(mi);

if (sessionId != null) {

ri.addAttribute(SESSION_ID_KEY, sessionId);

}

if (host != null) {

ri.addAttribute(HOST_KEY, host);

}

return ri;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,Session 是一种存储在服务器端的数据结构,用于存储用户在访问 Web 应用时的会话信息。在 Web 应用中,Session 经常用于保持用户登录状态、存储用户购物车信息、存储用户偏好设置等。 以下是创建和获取 Session 的步骤: 创建 Session: 1. 在 Servlet 中,调用 HttpServletRequest 的 getSession() 方法,即可获取当前用户的 Session 对象。如果当前用户没有 Session,则该方法会自动创建一个新的 Session。 HttpSession session = request.getSession(); 2. 如果需要设置 Session 的过期时间,可以调用 setMaxInactiveInterval() 方法。该方法接收一个以秒为单位的整数值,表示 Session 的最大空闲时间。 session.setMaxInactiveInterval(1800); // 设置 Session 的最大空闲时间为 30 分钟 3. 如果需要向 Session 中添加数据,可以使用 setAttribute() 方法。该方法接收两个参数,第一个参数是数据的键名,第二个参数是数据的值。 session.setAttribute("username", "张三"); // 向 Session 中添加一个名为 "username" 的键值对 获取 Session: 1. 在 Servlet 中,调用 HttpServletRequest 的 getSession() 方法,即可获取当前用户的 Session 对象。 HttpSession session = request.getSession(); 2. 如果需要获取 Session 中的数据,可以使用 getAttribute() 方法。该方法接收一个参数,表示要获取数据的键名。如果 Session 中不存在该键名,则返回 null。 String username = (String) session.getAttribute("username"); // 从 Session 中获取名为 "username" 的键对应的值 需要注意的是,Session 中存储的数据通常是敏感信息,因此需要采取一定的安全措施来保护 Session。例如,可以使用 HTTPS 协议来加密通信,使用 SSL/TLS 证书来验证服务器的身份,使用 Cookie 来保存 Session ID 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值