本文整理汇总了Java中org.springframework.security.core.session.SessionInformation类的典型用法代码示例。如果您正苦于以下问题:Java SessionInformation类的具体用法?Java SessionInformation怎么用?Java SessionInformation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SessionInformation类属于org.springframework.security.core.session包,在下文中一共展示了SessionInformation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: allowableSessionsExceeded
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Override
protected void allowableSessionsExceeded(List<SessionInformation> sessions, int allowableSessions,
SessionRegistry registry) throws SessionAuthenticationException {
SessionInformation leastRecentlyUsed = null;
for (SessionInformation session : sessions) {
if ((leastRecentlyUsed == null)
|| session.getLastRequest().before(leastRecentlyUsed.getLastRequest())) {
leastRecentlyUsed = session;
}
}
if(leastRecentlyUsed instanceof SessionInformationObject){
SessionInformationObject sessionObject=(SessionInformationObject)leastRecentlyUsed;
sessionObject.setKickAway(true);
}
leastRecentlyUsed.expireNow();
}
示例2: testFindProfileActiveSessions
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Test
@WithMockUser("user123")
public void testFindProfileActiveSessions() throws Exception {
final UserEntity user = new UserEntity().setUsername("user123");
when(sessionRegistry.getAllPrincipals()).thenReturn(Collections.singletonList(user));
final SessionInformation sessionInformation = new SessionInformation("1", "1", new Date());
when(sessionRegistry.getAllSessions(user, true))
.thenReturn(Collections.singletonList(sessionInformation));
MockHttpServletRequestBuilder request = get("/api/profile/sessions")
.contentType(MediaType.APPLICATION_JSON);
MockHttpServletResponse response = mockMvc.perform(request)
.andDo(document("user-profile-sessions-list"))
.andReturn()
.getResponse();
assertThat(response.getStatus()).isEqualTo(200);
List<SessionInformation> expectedValue = Collections
.singletonList(new SessionInformation("user123", "1", sessionInformation.getLastRequest()));
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expectedValue));
verify(sessionRegistry).getAllPrincipals();
verify(sessionRegistry).getAllSessions(user, true);
}
示例3: kickUser
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Override
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void kickUser(String ssoId){
final List<Object> allPrincipals = sessionRegistry.getAllPrincipals();
for (final Object principal : allPrincipals) {
if (principal instanceof UserDetails && ((UserDetails) principal).getUsername().equals(ssoId)) {
List<SessionInformation> activeUserSessions =
sessionRegistry.getAllSessions(principal, false);
if (!activeUserSessions.isEmpty()) {
for(SessionInformation i : activeUserSessions){
i.expireNow();
}
}
}
}
}
示例4: getAllSessions
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
public List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions) {
final Set<String> sessionsUsedByPrincipal = principals.get(principal);
if (sessionsUsedByPrincipal == null) {
return Collections.emptyList();
}
List<SessionInformation> list = new ArrayList<SessionInformation>(sessionsUsedByPrincipal.size());
for (String sessionId : sessionsUsedByPrincipal) {
SessionInformation sessionInformation = getSessionInformation(sessionId);
if (sessionInformation == null) {
continue;
}
if (includeExpiredSessions || !sessionInformation.isExpired()) {
list.add(sessionInformation);
}
}
return list;
}
示例5: expireSession
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@RequestMapping(value = "/expire.xhtml", method = RequestMethod.GET)
public String expireSession(Model model, @RequestParam("session") String sessionid, RedirectAttributes redirectAttributes) {
boolean expire = true;
Object[] principals = cojSessionRegistryImpl.getAllPrincipals().toArray();
for (int i = 0; i < principals.length && expire; i++) {
Object[] sessions = cojSessionRegistryImpl.getAllSessions(principals[i], true).toArray();
for (int j = 0; j < sessions.length && expire; j++) {
SessionInformation s = (SessionInformation) sessions[j];
if (sessionid.equals(s.getSessionId())) {
s.expireNow();
s.refreshLastRequest();
expire = false;
cojSessionRegistryImpl.removeSessionInformation(sessionid);
}
}
}
redirectAttributes.addFlashAttribute("message", Notification.getSuccesfullDelete());
return "redirect:/admin/sessions.xhtml";
}
示例6: listActiveUsers
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@ModelAttribute("activeUsers")
public Map<Object, Date> listActiveUsers(Model model) {
Map<Object, Date> lastActivityDates = new HashMap<Object, Date>();
for (Object principal : sessionRegistry.getAllPrincipals()) {
for (SessionInformation session : sessionRegistry.getAllSessions(principal, false)) {
if (lastActivityDates.get(principal) == null) {
lastActivityDates.put(principal, session.getLastRequest());
} else {
Date prevLastRequest = lastActivityDates.get(principal);
if (session.getLastRequest().after(prevLastRequest)) {
lastActivityDates.put(principal, session.getLastRequest());
}
}
}
}
return lastActivityDates;
}
示例7: logoutUsers
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@RequestMapping(value = "/logoutUsers", method = RequestMethod.GET)
@ResponseBody
public String logoutUsers(HttpServletRequest req,HttpServletResponse res) {
String sessionId = req.getRequestedSessionId();
System.out.print("sessionId;:"+sessionId);
JSONObject resJson = new JSONObject();
LOGGER.log(Level.INFO,"EXPIRE SESSION::::::"+sessionId);
logout(req);
if(sessionId!=null) {
//List<Object> principals = sessionRegistry.getAllPrincipals();
sessionRegistry.removeSessionInformation(sessionId);
if(sessionRegistry.getSessionInformation(sessionId) != null){
SessionInformation session = sessionRegistry.getSessionInformation(sessionId);
boolean isexpired = sessionRegistry.getSessionInformation(sessionId).isExpired();
resJson.put("isexpired",isexpired);
if(!isexpired) {
sessionRegistry.getSessionInformation(sessionId).expireNow();
}
}
resJson.put("Result",0);
resJson.put("msg","Successfully logged out on LeafSchool");
}
return resJson.toString();
}
示例8: getAllSessions
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Override
public List<SessionInformation> getAllSessions(Object principal,
boolean includeExpiredSessions) {
Collection<S> sessions = this.sessionRepository.findByIndexNameAndIndexValue(
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME,
name(principal)).values();
List<SessionInformation> infos = new ArrayList<>();
for (S session : sessions) {
if (includeExpiredSessions || !Boolean.TRUE.equals(session
.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))) {
infos.add(new SpringSessionBackedSessionInformation<>(session,
this.sessionRepository));
}
}
return infos;
}
示例9: expireNow
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Test
public void expireNow() {
Session session = createSession(SESSION_ID, USER_NAME, NOW);
when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session);
SessionInformation sessionInfo = this.sessionRegistry
.getSessionInformation(SESSION_ID);
assertThat(sessionInfo.isExpired()).isFalse();
sessionInfo.expireNow();
assertThat(sessionInfo.isExpired()).isTrue();
ArgumentCaptor<Session> captor = ArgumentCaptor.forClass(Session.class);
verify(this.sessionRepository).save(captor.capture());
assertThat(captor.getValue().<Boolean>getAttribute(
SpringSessionBackedSessionInformation.EXPIRED_ATTR))
.isEqualTo(Boolean.TRUE);
}
示例10: getAllPrincipals
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Override
public List<Object> getAllPrincipals() {
logger.debug("getAllPrincipals");
Map allSessions = sessionIds.getAllWithLoader(sessionIds.getKeys(), null);
Set<Object> principals = new HashSet<Object>();
for (Object valObj : allSessions.values()) {
SessionInformation info = (SessionInformation)valObj;
principals.add(info.getPrincipal());
}
List<Object> principalsList = new ArrayList<Object>(principals.size());
principalsList.addAll(principals);
return principalsList;
}
示例11: getAllSessions
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@Override
public List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions) {
logger.debug("Called with includeExpiredSession "+includeExpiredSessions);
Set<String> sessionsUsedByPrincipal = getSessionIds(principal);
List<SessionInformation> list = new ArrayList<SessionInformation>();
Iterator<String> iter = sessionsUsedByPrincipal.iterator();
while (iter.hasNext()) {
String sessionId = iter.next();
SessionInformation sessionInformation = getSessionInformation(sessionId);
if (includeExpiredSessions || !sessionInformation.isExpired()) {
list.add(sessionInformation);
}
}
logger.debug("List size "+list.size());
return list;
}
示例12: findProfileActiveSessions
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@GetMapping("/sessions")
public List<SessionInformation> findProfileActiveSessions(Authentication authentication) {
if (authentication == null || authentication.getPrincipal() == null)
return Collections.emptyList();
List<SessionInformation> sessions = new ArrayList<>();
for (Object principal : sessionRegistry.getAllPrincipals()) {
UserEntity user = (UserEntity) principal;
if (!user.getUsername().equals(authentication.getName()))
continue;
sessions.addAll(sessionRegistry.getAllSessions(user, true));
}
return sessions.stream()
.map(i -> new SessionInformation(authentication.getName(), i.getSessionId(), i.getLastRequest()))
.collect(Collectors.toList());
}
示例13: list
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@RequestMapping(value = "list", method = RequestMethod.GET)
public ModelMap list() {
ModelMap map = new ModelMap();
List<SessionInformation> sessions = new ArrayList<>();
for(Object principal : this.sessionRegistry.getAllPrincipals()) {
sessions.addAll(this.sessionRegistry.getAllSessions(principal, true));
}
map.put("timestamp", System.currentTimeMillis());
map.put("numberOfSessions", sessions.size());
map.put("sessions", sessions);
return map;
}
示例14: removeSession
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@DeleteMapping(value="/user/sessions/{sessionId}")
public String removeSession(@PathVariable String sessionId, RedirectAttributes redirectAttrs) {
SessionInformation sessionInformation = sessionRegistry.getSessionInformation(sessionId);
if(sessionInformation != null) {
sessionInformation.expireNow();
}
redirectAttrs.addFlashAttribute("message", "Session was removed");
return "redirect:/user/sessions/";
}
示例15: removeSession
import org.springframework.security.core.session.SessionInformation; //导入依赖的package包/类
@RequestMapping(value="/user/sessions/{sessionId}", method = RequestMethod.DELETE)
public String removeSession(@PathVariable String sessionId, RedirectAttributes redirectAttrs) {
SessionInformation sessionInformation = sessionRegistry.getSessionInformation(sessionId);
if(sessionInformation != null) {
sessionInformation.expireNow();
}
redirectAttrs.addFlashAttribute("message", "Session was removed");
return "redirect:/user/sessions/";
}