springsecurity3 登录后在extjs中使用安全实体的信息,不同页面共享数据

编程入门 行业动态 更新时间:2024-10-28 08:24:04

以前使用springsecurity3的时候登录后都是用页面标签来显示当前的用户的username,<sec:authentication property='principal.username'/>,

查询当前user的信息时都是用username来做索引查找数据库数据,使用的相当别扭,因为一般都是使用id来做索引。还有个问题就是如何在不同jsp页面共享数据。

现在把两个问题并在一起解决。

1、先说不同页面之间如何共享数据。其实就是利用session,把需要共享的数据以键值对得方式set到session里面,别的页面要用的时候再根据键get出来,

request.getSession().setAttribute("currentUserId", user.getId());
request.getSession().setAttribute("currentUserName", user.getName());
取出来

	Integer	temp= (Integer)request.getSession().getAttribute("currentUserId");
	String	temp2= (String)request.getSession().getAttribute("currentUserName");

2、springsecurity3的问题,先要继承一个SavedRequestAwareAuthenticationSuccessHandler类,也可以实现AuthenticationSuccessHandler接口,我是用前者。继承要重写一个onAuthenticationSuccess方法,登录成功后就会调用这个方法,然后你可以在这个方法里写你的业务,比如更新当前用户最后登录的时间、ip、登录次数。

同时,把成功登陆的安全实体的信息set到session里面,让jsp页面可以直接从session中get出来,这样就可以不用ss3的页面标签来显示username,还可以显示这个安全实体的其他属性。

说面一下,这个成功登陆的安全实体(user)是从数据中取出来的数据,且ss3把认证后的权限信息set给了这个user的authorities成员变量。

下面上代码

SavedRequestAwareAuthenticationSuccessHandler的继承类

/**
 * @Description : 描述
 * @author YangXuan
 * @email 364105996@qq
 * @date Aug 18, 2013 11:21:19 PM
 */
public class AuthenticationSuccessHandler extends
		SavedRequestAwareAuthenticationSuccessHandler {
	protected final Log logger = LogFactory.getLog(this.getClass());
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@SuppressWarnings("unused")
	private RequestCache requestCache = new HttpSessionRequestCache();

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws ServletException, IOException {
		System.out
				.println("---------------login successfully , you can extends this class to achive what you want !");

		User user = (User) new SecurityUtilImpl().getUserDetails();

		User temp = this.updateUser(user, request);
		this.userService.modify(temp);
		response.setContentType("text/javascript");
		response.getWriter().print(user.getPassword());
		request.getSession().setAttribute("currentUserId", user.getId());
		request.getSession().setAttribute("currentUserName", user.getName());
		super.onAuthenticationSuccess(request, response, authentication);
	}

	public User updateUser(User user, HttpServletRequest request) {
		String lastIp = new CommonUtilImpl().getClientIP(request);
		int count = user.getLoginCount() + 1;
		user.setLoginCount(count);
		user.setLastLoginIp("aaa");
		user.setLastLoginIp(lastIp);
		user.setLastLoginTime(new Date());
		return user;
	}
}

jsp页面,也是extjs的入口文件,需要注意的是从session中get出来的数据复制给js变量一定要在引用extjs之前,extjs里面的js才可以使用这个变量,因为解析式从上到下。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="sec"
	uri="http://www.springframework/security/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
		Integer	temp= (Integer)request.getSession().getAttribute("currentUserId");
		String	temp2= (String)request.getSession().getAttribute("currentUserName");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<script type="text/javascript">
	var basePath='<%=basePath%>';
	var adminFolder = basePath + 'admin/';
	var imagePath = basePath + 'extjs/images/';
	var currentUser = "<sec:authentication property='principal.username'/>";
	var currentUserId='<%=temp%>';
	var currentUserName='<%=temp2%>';
</script>
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>/extjs/resources/css/ext-all.css">
<script type="text/javascript"
	src="<%=basePath%>/extjs/ext-all-debug.js"></script>
<script type="text/javascript"
	src="<%=basePath%>/extjs/ext-lang-zh_CN.js"></script>
<!-- app应用的入口 -->
<script type="text/javascript" src="<%=basePath%>admin/app.js"></script>

</head>

<body>
</body>
</html>

extjs 里的js应用变量

items : [{
				xtype : 'toolbar',
				items : [{
					id : 'currentUser',
					scale : 'medium',
					text : '当前用户 : ' + currentUserName,
					icon : imagePath + 'user.png',
					menu : [{
								xtype : 'button',
								text : '个人信息',
								scale : 'medium',
								icon : imagePath + 'user.png',
								handler : function() {
									Ext.create('yang.view.sysManage.SelfInfo',
											{
												title : ' ' + currentUserName
														+ ' 的个人信息'
											});
								}
							}

显示效果:




更多推荐

springsecurity3 登录后在extjs中使用安全实体的信息,不同页面共享数据

本文发布于:2023-06-14 01:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   页面   数据   信息   extjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!