org.apache.shiro.subject.Subject.getSession()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(240)

本文整理了Java中org.apache.shiro.subject.Subject.getSession()方法的一些代码示例,展示了Subject.getSession()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Subject.getSession()方法的具体详情如下:
包路径:org.apache.shiro.subject.Subject
类名称:Subject
方法名:getSession

Subject.getSession介绍

[英]Returns the application Session associated with this Subject. If no session exists when this method is called, a new session will be created, associated with this Subject, and then returned.
[中]返回与此主题关联的应用程序会话。如果调用此方法时不存在会话,将创建一个与此主题关联的新会话,然后返回。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public boolean isSessionStorageEnabled(Subject subject) {
    // save to session if we already have a session. do not create on just for saving the subject
    return subject.getSession(false) != null;
  }
};

代码示例来源:origin: killbill/killbill

@Override
  public boolean isSessionStorageEnabled(final Subject subject) {
    // Use what already exists
    return subject.getSession(false) != null;
  }
});

代码示例来源:origin: apache/shiro

protected void stopSession(Subject subject) {
  Session s = subject.getSession(false);
  if (s != null) {
    s.stop();
  }
}

代码示例来源:origin: apache/shiro

public static SavedRequest getSavedRequest(ServletRequest request) {
  SavedRequest savedRequest = null;
  Subject subject = SecurityUtils.getSubject();
  Session session = subject.getSession(false);
  if (session != null) {
    savedRequest = (SavedRequest) session.getAttribute(SAVED_REQUEST_KEY);
  }
  return savedRequest;
}

代码示例来源:origin: stylefeng/Guns

/**
 * 从shiro获取session
 */
public static Session getSession() {
  return getSubject().getSession();
}

代码示例来源:origin: shuzheng/zheng

@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
  Session session = getSubject(request, response).getSession(false);
  if(session == null) {
    return true;
  }
  boolean forceout = session.getAttribute("FORCE_LOGOUT") == null;
  return  forceout;
}

代码示例来源:origin: wuyouzhuguli/FEBS-Shiro

protected Session getSession() {
  return getSubject().getSession();
}

代码示例来源:origin: wuyouzhuguli/FEBS-Shiro

protected Session getSession(Boolean flag) {
  return getSubject().getSession(flag);
}

代码示例来源:origin: apache/shiro

public static SavedRequest getAndClearSavedRequest(ServletRequest request) {
  SavedRequest savedRequest = getSavedRequest(request);
  if (savedRequest != null) {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    session.removeAttribute(SAVED_REQUEST_KEY);
  }
  return savedRequest;
}

代码示例来源:origin: apache/shiro

public Session resolveSession() {
  Session session = getSession();
  if (session == null) {
    //try the Subject if it exists:
    Subject existingSubject = getSubject();
    if (existingSubject != null) {
      session = existingSubject.getSession(false);
    }
  }
  return session;
}

代码示例来源:origin: apache/shiro

/**
 * Removes any existing subject state from the Subject's session (if the session exists).  If the session
 * does not exist, this method does not do anything.
 *
 * @param subject the subject for which any existing subject state will be removed from its session.
 */
protected void removeFromSession(Subject subject) {
  Session session = subject.getSession(false);
  if (session != null) {
    session.removeAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY);
    session.removeAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
  }
}

代码示例来源:origin: apache/shiro

public T get() {
  Subject subject = ThreadContext.getSubject();
  if (subject == null) {
    throw new OutOfScopeException("There is no Shiro Session currently in scope.");
  }
  Session session = subject.getSession();
  T scoped = castSessionAttribute(session);
  if (scoped == null) {
    scoped = unscoped.get();
  }
  return scoped;
}

代码示例来源:origin: apache/shiro

public static void saveRequest(ServletRequest request) {
  Subject subject = SecurityUtils.getSubject();
  Session session = subject.getSession();
  HttpServletRequest httpRequest = toHttp(request);
  SavedRequest savedRequest = new SavedRequest(httpRequest);
  session.setAttribute(SAVED_REQUEST_KEY, savedRequest);
}

代码示例来源:origin: shuzheng/zheng

@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
  Subject subject = getSubject(request, response);
  Session session = subject.getSession();
  // 判断请求类型
  String upmsType = PropertiesFileUtil.getInstance("zheng-upms-client").get("zheng.upms.type");
  session.setAttribute(UpmsConstant.UPMS_TYPE, upmsType);
  if ("client".equals(upmsType)) {
    return validateClient(request, response);
  }
  if ("server".equals(upmsType)) {
    return subject.isAuthenticated();
  }
  return false;
}

代码示例来源:origin: killbill/killbill

@Override
protected void saveToSession(final Subject subject) {
  boolean updatesDisabled = false;
  Session session = subject.getSession(false);
  if (session == null && !CollectionUtils.isEmpty(subject.getPrincipals())) {
    // Force the creation of the session here to get the id
    session = subject.getSession();
    // Optimize the session creation path: the default saveToSession implementation
    // will call setAttribute() several times in a row, causing unnecessary DAO UPDATE queries
    updatesDisabled = disableUpdatesForSession(subject, session);
  }
  super.saveToSession(subject);
  if (updatesDisabled) {
    enableUpdatesForSession(subject, session);
  }
}

代码示例来源:origin: apache/usergrid

public static BiMap<UUID, String> getOrganizations() {
  Subject currentUser = getSubject();
  if ( !isOrganizationAdmin() ) {
    return null;
  }
  Session session = currentUser.getSession();
  BiMap<UUID, String> organizations = HashBiMap.create();
  Map map = (Map)session.getAttribute( "organizations" );
  organizations.putAll(map);
  return organizations;
}

代码示例来源:origin: apache/usergrid

public static OrganizationInfo getOrganization() {
  Subject currentUser = getSubject();
  if ( currentUser == null ) {
    return null;
  }
  if ( !currentUser.hasRole( ROLE_ORGANIZATION_ADMIN ) ) {
    return null;
  }
  Session session = currentUser.getSession();
  OrganizationInfo organization = ( OrganizationInfo ) session.getAttribute( "organization" );
  return organization;
}

代码示例来源:origin: linlinjava/litemall

@PostMapping("/login")
public Object login(@RequestBody String body) {
  String username = JacksonUtil.parseString(body, "username");
  String password = JacksonUtil.parseString(body, "password");
  if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
    return ResponseUtil.badArgument();
  }
  Subject currentUser = SecurityUtils.getSubject();
  try {
    currentUser.login(new UsernamePasswordToken(username, password));
  } catch (UnknownAccountException uae) {
    return ResponseUtil.fail(ADMIN_INVALID_ACCOUNT, "用户帐号或密码不正确");
  } catch (LockedAccountException lae) {
    return ResponseUtil.fail(ADMIN_INVALID_ACCOUNT, "用户帐号已锁定不可用");
  } catch (AuthenticationException ae) {
    return ResponseUtil.fail(ADMIN_INVALID_ACCOUNT, ae.getMessage());
  }
  return ResponseUtil.ok(currentUser.getSession().getId());
}

代码示例来源:origin: apache/geode

@Before
public void before() throws Exception {
 this.mockSecurityManager = mock(SecurityManager.class);
 this.shiroManager = mock(org.apache.shiro.mgt.SecurityManager.class);
 this.provider = mock(SecurityManagerProvider.class);
 this.mockSubject = mock(Subject.class);
 when(provider.getShiroSecurityManager()).thenReturn(shiroManager);
 when(provider.getSecurityManager()).thenReturn(mockSecurityManager);
 when(shiroManager.createSubject(any(SubjectContext.class))).thenReturn(mockSubject);
 when(mockSubject.getPrincipal()).thenReturn("principal");
 when(mockSubject.getSession()).thenReturn(mock(Session.class));
 this.securityService = new IntegratedSecurityService(provider, null);
}

代码示例来源:origin: apache/shiro

@Test
public void testDefaultConfig() {
  Subject subject = SecurityUtils.getSubject();
  AuthenticationToken token = new UsernamePasswordToken("guest", "guest");
  subject.login(token);
  assertTrue(subject.isAuthenticated());
  assertTrue("guest".equals(subject.getPrincipal()));
  assertTrue(subject.hasRole("guest"));
  Session session = subject.getSession();
  session.setAttribute("key", "value");
  assertEquals(session.getAttribute("key"), "value");
  subject.logout();
  assertNull(subject.getSession(false));
  assertNull(subject.getPrincipal());
  assertNull(subject.getPrincipals());
}

相关文章