博客分类:* 技巧
阅读更多
前不久有个需求,需要在后台查看所有已登录的用户,系统使用的是spring mvc3.1 + spring security 3.1+ jpa 2.0 。
按官方文档中的方式去获取已登录的用户一直返加为0,经无数次折腾,终于有了可行的方案。先看下java代码部份。
public String queryLoginUser(int start,int limit){
List<Object> slist =sessionRegistry.getAllPrincipals();
int totalCount=slist.size();
if(slist.size()==0){
return "{totalCount:" + totalCount + ",data:\[\]}";
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Object> pageList=slist.subList(start,limit>slist.size()?slist.size():limit);
StringBuffer retVal=new StringBuffer("\[");
int k=0;
for(int i=0;i<pageList.size();i++){
List<SessionInformation> sessionList = sessionRegistry.getAllSessions(pageList.get(i),true);
User user=(User)pageList.get(i);
for(SessionInformation t:sessionList){
if(k!=0){
retVal.append(",");
}
retVal.append("{\\"id\\":\\""+k+"\\",\\"userName\\":\\""+user.getUsername()+"\\",\\"sessionId\\":\\""+t.getSessionId()+"\\",\\"lastRequest\\":\\""+sdf.format(t.getLastRequest())+"\\"}");
k=k+1;
}
}
retVal.append("\]");
return "{totalCount:" + totalCount + ",data:"+ retVal.toString() + "}";
}
该方法实现了对当前登录用户的分页查询,并返回Json数据格式。
以下是xml配置的关键部份
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
<beans:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>
</beans:bean>
<!-- 登录验证器 -->
<beans:bean id="loginFilter" class="com.verysoft.baseframework.security.MyUsernamePasswordAuthenticationFilter">
<beans:property name="sessionAuthenticationStrategy" ref="sas"/><!--此配置可实现获取所有登录用户信息 -->
<beans:property name="filterProcessesUrl" value="/j\_spring\_security_check"></beans:property>
<beans:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"></beans:property>
<beans:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"></beans:property>
<beans:property name="authenticationManager" ref="myAuthenticationManager"></beans:property>
<beans:property name="userDao" ref="userDao"></beans:property>
</beans:bean>
</http>
<http use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint">
<logout delete-cookies="JSESSIONID" invalidate-session="true" />
<!\-\- 实现免登陆验证
<remember-me /> -->
<!\-\- <custom-filter ref="concurrencyFilter" position="CONCURRENT\_SESSION\_FILTER" /> -->
<custom-filter ref="loginFilter" position="FORM\_LOGIN\_FILTER" />
<custom-filter ref="securityFilter" before="FILTER\_SECURITY\_INTERCEPTOR"/>
<session-management session-fixation-protection="none" />
</http>
此方案经本人验证通过,配置文件在附件中,有其它问题可联系本人QQ:359709421
全部代码在云盘 http://yunpan.cn/csQyg47f3gBkX (提取码:35a9)
我的网店,有劳各位参观参观 http://mrs-x.taobao.com/
[
资深Java项目团队历时1年打造,实战精髓大揭秘!
Java系统学习全案:五大阶段学习系统规划、8大企业级项目贯穿全程。限时2折秒杀。立省4688元!
java 获取windows系统安装的证书或证书链 | js获取URL中的请求参数
楼主是好人 还给了我源码,非常感谢 http://yunpan.cn/csQyg47f3gBkX (提取码:35a9)
2 楼 a958048434 2015-02-27 引用
代码部分太少了
1 楼 a958048434 2015-02-26 引用
楼主能不能把文件完整发过来啊,本人刚接触security,就遇到一个难题
字体颜色: 标准深红红色橙色棕色黄色绿色橄榄青色蓝色深蓝靛蓝紫色灰色白色黑色 字体大小: 标准1 (xx-small)2 (x-small)3 (small)4 (medium)5 (large)6 (x-large)7 (xx-large) 对齐: 标准居左居中居右
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签
(快捷键 Alt+S / Ctrl+Enter)
Spring _Security_框架sessionRegistry.getAllPrincipals()返回空数组的问题。
_Security_Context sc = _Security_ContextHolder.getContext(); Authentication auth = sc.getAuthentication(); // 登入_用户_信息 User user = (User) auth.getPrincipal(); log.info("_用户_user...
在使用_spring_ _security_框架的好处之一就是 对_用户_管理很方便 例如,本文所说的对当前在线_用户_数目的显示和_所有__用户_名显示 主要用到的是 sessionRegistry这个,它所在的包是 import org._spring_framework._security_.core.session.SessionRegistry;首先我们在controller里面autowire下 @A
Spring _Security_ - _获取_当前_登录__用户_的详细信息 在_Spring_框架里面,可以通过以下几种方式_获取_到当前_登录__用户_的详细信息: 1. 在Bean中_获取__用户_信息 Authentication authentication = _Security_ContextHolder.getContext().getAuthentication(); if (!(authentication insta...
在实际开发中,我们保存_用户_的密码在数据库中都是经过加密的,通常都是使用md5加密,这是为了安全起见,否则别人一进入数据库就能看到_所有_人的密码,这样太不安全了,_spring_ _security_提供了密码加密的一个配置,这样我们在_登录_时,它会自动的将_用户_输入的密码和数据库里面的密码进行匹配,而_用户_是毫无感知的。 很简单,只需要加上这样的一条配置即可。 <passw...
在web.xml中将原先的那个监听器替换为自己写的这个就可以了,检测在线_用户_的只有一个表,里面只有一个id字段。如果_用户_不是很多,这个表可以是一个MySQL的内存表,或者Oralce的表存储修改为内存。 package com.yourcompany.service._security_; import javax.servlet.http.HttpSessionEvent; impor...
参考: 文章<_spring__Security_五session并发控制>
目录下面主要介绍如何在其他模块中_获取_当前_用户_1. 添加依赖2. 进行session及redis的配置3. 进行_spring__security_配置4. _获取_当前_用户_5. 错误调试6. 关于每次都要创建相同包和对象的解决_方案_1. 将_用户_管理放到一个共通的模块中,然后打成一个jar包,在新的模块中直接引入此包进行使用。2. 序列化有可以忽略包路径的方式,具体方法我也忘记了。可以自行查找。 在分布...
spring _security_ _获取_当前_用户_信息及权限 一、在程序中_获取__用户_信息 UserDetails userDetails = (UserDetails); _Security_ContextHolder.getContext().getAuthentication().getPrincipal(); 在程序中_获取_权限信息 Collection&lt;? extends GrantedAut...
目录 _Spring__Security_3.X--一个简单实现 _Spring__Security_3.X--前台与后台_登录_认证 _Spring__Security_3.X--remember-me _Spring__Security_3.X--验证码 前面给出了一个简单的应用 _Spring__Security_3.X--一个简单实现 不过一般我们在管理系统时都会分前台与后台,也就是说,前台与后台的_登录_入...
使用_Spring_ _Security_来实现权限管理 首先实现org._spring_framework._security_.core.userdetails.UserDetailsService 接口中 loadUserByUsername 方法. 方法返回org._spring_framework._security_.core.userdetails.UserDetails接口,也可以返回Spri
_Security_ContextImpl _security_ContextImpl = (_Security_ContextImpl) request.getSession().getAttribute("_SPRING___SECURITY__CONTEXT");// _登录_名 if (_security_ContextImpl != null) { System.out.println("Username...
记录一下,方便之后查阅 在页面中显示,使用session_获取_: ${session._SPRING___SECURITY__CONTEXT.authentication.principal.username}
_spring__security_扩展自定义会话管理(一)控制_用户_重复登陆 _spring__security_有控制单账号只能在一个地方登陆的功能,后登陆_用户_将踢掉前登陆_用户_;或者限制账号重复登陆,一个账号没有退出,另外一个人想用这个账号就登陆不上。 现在我想根据原有的这些功能参考,做一个管理员可以将某正在session中的_用户_踢出系统的功能。便于管理员修改了登陆_用户_的权限或者部门设置后,强制...
1、页面<div> INDEX页面 <p>_用户_名:${_SPRING___SECURITY__CONTEXT.authentication.principal.username}&lt;/p&gt; &lt;p&gt;_用户_姓名:${_SPRING___SECURITY__CONTEXT.authentication.principal.name} </p...
@RequestMapping("/login") public String login(){ Authentication auth = _Security_ContextHolder.getContext().getAuthentication(); if(auth instanceof AnonymousAuthenticationToken){ return...
Java后台_获取_比较容易; 页面中_获取_网上用jstl的比较多,使用_spring__security_标签在我项目里报错; 直接session需要这样取: ${Session._SPRING___SECURITY__CONTEXT.authentication.principal.username!'xxx'}
spring _security_ 获取__用户_信息 本文描述在_spring _security_中如何_获取__用户_信息。分别介绍几种常用的方法实现。 通过Bean_获取__用户_ _获取_当前认证_用户_(authenticated principal)最简单的方式是通过_Security_ContextHolder类的静态方法: Authentication authentication = _Security_Co...
问题描述:项目的权限是用_Spring__Security_实现的,项目中有个需求,实现需求的逻辑如下: 启动_Spring_Boot后启动一个线程去监听redis消息队列,对数据进行加工并保存到库里,需要保存创建人(createBy),而创建人正是当前_登录__用户_,但一直_获取_不到当前_登录__用户_。 解释如下: 我们将当前应用_security_上下文的_所有_数据保存在_Security_ContextHolder里面,...
一般主要_获取_两种信息:_获取_Authentication也可以写成:_获取_结果:具体信息说明可以参看我的另一篇博客:https://blog.csdn.net/dongguabai/article/details/80932225_获取_UserDetails这里_获取_的UserDetails其实就相当于是上面的Authentication中的principal。当然,也可以封装成一个工具类:/* ...
Spring _Security_的源码更清楚的了解它的认证逻辑流程。 本篇文章主要围绕下面几个问题来深入源码: 1._用户_认证流程 2.认证结果如何在多个请求之间共享 3._获取_认证_用户_信息 一、_用户_认证流程 提到_Spring_ _Security_核心就是一系列的过滤器链,当一个请求来的时候,首先要通过过滤器链的校验,校验通过之后才会访问_用户_各种信息。 这里要说明的是在过滤器的最前端...
1.在session中取得_spring_ security_的_登录__用户_名如下: ${session._SPRING___SECURITY__CONTEXT.authentication.principal.username} _spring _security_ 把_SPRING___SECURITY__CONTEXT 放入了session 没有直接把username 放进去。下面一段代码主要描述的是se
//通过此方法,可以直接_获取_当当前登陆_用户_的_所有_信息 User user=(User) _Security_ContextHolder.getContext().getAuthentication().getPrincipal(); 查到当前登陆的_用户_后,自然就可以为所欲为了...
问题描述: 登录__用户_的session在redis失效、清除或者_用户_退出系统后,系统中的sessionRegistry中session依然存在,并未同步失效 系统架构: _Spring_boot2 + _Spring__Security + _spring_-session-data-redis + Redis 1. Redis配置(当然可以只加注解,使用默认配置) /* redis配置 / @Confi...
(一) 在当前页面显示 1. maven引入 &lt;dependency&gt; &lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt; &lt;artifactId&gt;thymeleaf-extras-_spring__security_4&lt;/a
常规配置 增加监听web.XML <listener> <listener-class> org._spring_framework._security_.ui.session.HttpSessionEventPublisher </listener-class> </listener> _spring_-_security_.XML <...
原理:在认证成功通过后,在显示_登录_成功页面之前,也就是在SavedRequestAwareAuthenticationSuccessHandler类中操作。 添加一个集合sessionMap用于保存认证成功的会话,键名为会话ID, 每次有_用户__登录_认证通过都要判断一下是否重复_登录_,如果不是继续执行,将会话保存在集合sessionMap里。 如果是就踢除之前_登录_过的_用户_的会话,将旧的会话从集合...
最近在学习使用_Spring_ Security 3,搜索了一些有用的东西,记录下来 引用来源:http://blog.163.com/tarcy_tw/blog/static/317235320108118119601/ 如果只是想从页面上显示当前登陆的_用户_名,可以直接使用Sp
使用_Spring_ _security_如何防止_用户_的重复_登录_呢?如果_用户_账号已_登录_,这时再进行第二次或多次_登录_,需要阻止这样的多次_登录_。 一。在web.xml中配置listener <listener> <listener-class>org._spring_framework._security_.web.session.HttpSessio...
网上搜索一大圈,最后发现http://www.ke-cai.net/2010/11/auto-login-after-successful.html里讲述的是最接近答案的,但一测试,发现验证是通过了,session却没有保存,接着再搜,发现了一篇文章说到了这个问题,http://stackoverflow.com/questions/5428654/_spring_-_security_-auto-log
1、如果在jsp页面中_获取_可以使用_spring_ _security_的标签 页面引入标签 使用: username : 即可显示当前_用户_。 2.java代码中使用 UserDetails userDetails = (UserDetails) _Security_ContextHolder.getContext().getAuthentication() .getPrincipal(
thymeleaf_获取__spring_ _security_当前_用户_信息
Spring _Security_使用一个Authentication对象来描述当前_用户_的相关信息。_Security_ContextHolder中持有的是当前_用户_的_Security_Context,而_Security_Context持有的是代表当前_用户_相关信息的Authentication的引用。这个Authentication对象不需要我们自己去创建,在与系统交互的过程中,_Spring_ _Security_会...
声明一下,这篇文章不是基于acegi spring _security_2.0写的, 我发现很多文章都是基于老版本写的, 并不适用最新版。 下面跟大家分享一下在_spring_ _security_3.0里如何正宗的做法达到控制多个账号请求的经验。 步骤1 [xhtml] view plaincopy http auto-confi
spring _security_ 实现免登陆功能大体也是基于COOKIE来实现的。 主要配置信息: 1.首先登陆表单要Post URL: /j__spring___security__check 同时_spring___security_remember_me要等于yes,这时登陆后会记录cookie到数据库中; /j__spring___security__check?_spri...
1.在验证已经通过后,在java代码中 Object principal = _Security_ContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { System.out.println("_用户_名:" + (Use...
上滑加载更多
Original url: Access
Created at: 2019-06-24 12:12:09
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论