struts2,登录功能模块实现

时间:2023-02-07 11:46:13

功能:

·UserLogin作为控制登录的Action,校验密码成功后记录session,可以选择记住登陆状态,登陆成功后自动跳转到登陆前的URL;

·UserLogout作为控制登录推出的Action,移除session,删除cookie;

·MainInfo和HeadInfo模拟了两个相对独立的Action用于展示页面内容;

·LoginInterceptor作为检查登录状态的拦截器,先检查session,后检查本地cookie;

·mainInfo.action和headInfo.action被配置通过LoginInterceptor拦截器检查。

struts.xml配置文件

<struts>
<package name="common-web" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="loginInterceptor" /> <interceptor-stack name="loginDefaultStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors> <default-interceptor-ref name="loginDefaultStack" /> <global-results>
<result name="login" type="redirect">userLogin.action</result>
</global-results> <action name="userLogin" class="userLoginAction">
<result type="redirect">${goingToURL}</result>
<result name="input">/page/user_login.jsp</result>
<interceptor-ref name="defaultStack" />
</action> <action name="userLogout" class="userLogoutAction"></action> <action name="mainInfo" class="mainInfoAction">
<result name="success">/page/main.jsp</result>
</action> <action name="headInfo" class="headInfoAction">
<result name="success">/page/head.jsp</result>
</action>
</package>
</struts>

struts.xml遇到的问题:

1、拦截器与Action必须配置在一个package下,否则拦截器不会对其他package下的Action生效。

2、暂无。

UserLogin.java主要源码

public class UserLogin extends ActionSupport implements ServletResponseAware, SessionAware {

    private String              name;
private String password;
private boolean rememberMe; private HttpServletResponse response;
private Map<String, Object> session; private String goingToURL;//登录前的URL public String execute() throws Exception { //... if (isLoginSucc) { //成功登录后记录session和cookie
if (rememberMe) {
String t = name + "," + password; Cookie cookie = new Cookie(CommonConstants.COOKIE_KEY_REMEMBER_LOGIN, t); cookie.setMaxAge(CommonConstants.COOKIE_AGE);//设置cookie存活时间
response.addCookie(cookie); } //设置session中的登录用户信息
session.put(CommonConstants.SESSION_KEY_USER_NAME, name); //从session中获取登陆前URL,获取后移除session中的这个值
String goingToURL = (String) session.get(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN);
setGoingToURL(goingToURL);
session.remove(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN); logger.info("登录成功[" + name + "]");
return SUCCESS;
} else {
logger.error("登录失败[" + name + "][" + password + "]");
return INPUT;
}
} //... getter & setter methods
}

UserLogin.java遇到的问题:

1、cookie.setDomain(),cookie.setPath()设置错误会导致cookie写入失败;

2、cookie.Value中有分号“;”时,会导致cookie写入失败,改为逗号解决;

LoginInterceptor.java主要源码

public class LoginInterceptor extends AbstractInterceptor {

    /* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception { ActionContext actionContext = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) actionContext
.get(StrutsStatics.HTTP_REQUEST);
Map<String, Object> session = actionContext.getSession(); //首先判断session,查找是否登录成功,通过拦截器
if (session != null && session.get(CommonConstants.SESSION_KEY_USER_NAME) != null) {
logger.info("通过拦截器,session中有记录[" + session.get(CommonConstants.SESSION_KEY_USER_NAME)
+ "]");
return invocation.invoke();
} //其次cookie验证,是否有记住的登录状态
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (logger.isDebugEnabled())
logger.debug("读取cookie项[" + cookie.getName() + "]"); if (CommonConstants.COOKIE_KEY_REMEMBER_LOGIN.equals(cookie.getName())) {
String value = cookie.getValue();
if (StringUtils.isNotBlank(value)) {
String[] split = value.split(",");
String name = split[0];
String password = split[1]; if (userLoginManager.checkLogin(name, password)) {
//check name/password from cookie success
logger.info("通过拦截器,cookie中有记录[" + name + "]");
session.put(CommonConstants.SESSION_KEY_USER_NAME, name);
return invocation.invoke();
} else {
//check name/password from cookie failure
setGoingToURL(session, invocation);
return Action.LOGIN;
}
} else {
setGoingToURL(session, invocation);
return Action.LOGIN;
}
}
}
} setGoingToURL(session, invocation);
return Action.LOGIN;
} private void setGoingToURL(Map<String, Object> session, ActionInvocation invocation) {
String url = "";
String namespace = invocation.getProxy().getNamespace(); if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")) {
url = url + namespace;
} String actionName = invocation.getProxy().getActionName();
if (StringUtils.isNotBlank(actionName)) {
url = url + "/" + actionName + ".action";
} if (logger.isDebugEnabled())
logger.debug("拼接登录前URL,结果:" + CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN + "[" + url
+ "]");
session.put(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN, url);
} //... getter & setter methods
}

LoginInterceptor.java遇到的问题:

转载:http://blog.csdn.net/welken/article/details/5587068

struts2,登录功能模块实现的更多相关文章

  1. Struts2整合Hibernate3实现用户登录功能

    所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...

  2. Struts2&plus;Spring&plus;Hibernate实现员工管理增删改查功能(一)之登录功能

    昨天的博客中我分享了个人关于ssh实现员工管理的框架整合,今天我在分享管理员登录功能的实现.  转载请注明出处"http://www.cnblogs.com/smfx1314/p/78013 ...

  3. 使用struts2进行登录功能的开发

    使用struts2进行登录功能的开发 一. 设计需求 使用idea和maven开发具有登录功能的web应用,java语言,使用struts2框架. 二. 设计步骤 1.使用idea创建maven应用, ...

  4. Spring&plus;Hibernate&plus;Struts2整合之实现登录功能

    前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...

  5. Struts&plus;Hibernate&plus;Spring实现用户登录功能

    通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...

  6. 一步步开发自己的博客 &period;NET版(3、注册登录功能)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  7. iOS开发一个用户登录注册模块需要解决的坑

    最近和另外一位同事负责公司登录和用户中心模块的开发工作,开发周期计划两周,减去和产品和接口的协调时间,再减去由于原型图和接口的问题,导致强迫症纠结症状高发,情绪不稳定耗费的时间,能在两周基本完成也算是 ...

  8. 10天学会phpWeChat——第二天:hello world!我的第一个功能模块

    今天我们开始进入<10天学会phpWeChat>系列教程的第二天:创建我的第一个hello world! 功能模块. 1.登录后台,进入 系统设置--自定义模块,如图: 自定义模块参数说明 ...

  9. 火车采集器 帝国CMS7&period;2免登录发布模块

    帝国cms7.2增加了金刚模式,登录发布有难度.免登录发布模块配合火车采集器,完美解决你遇到的问题. 免登录直接获取栏目列表 通过文件内设置密码免登录发布数据 帝国cms7.2免登陆文章发布接口使用说 ...

随机推荐

  1. JS组件系列——开源免费图表组件:Chart&period;js

    前言:最近被开源免费得有点上火了,各种组件首先想到的就是是开源否.是否免费.是否和bootstrap风格一致.想着以后做报表肯定要用到图表组件的,于是在Bootstrap中文网上面找到了Chart.j ...

  2. ueditor插件简单使用

    下载地址:http://ueditor.baidu.com/website/download.html 建议同时下载所需版本及完整源码.   [ 1.4.3 JSP + 完整源码src ] 简单配置说 ...

  3. Codeigniter

    最近准备接手改进一个别人用Codeigniter写的项目,虽然之前也有用过CI,但是是完全按着自己的意思写的,没按CI的一些套路.用在公众的项目,最好还是按框架规范来,所以还是总结一下,免得以后别人再 ...

  4. WPF之 DataGrid分页

    接着上一篇WPF之 DataGrid数据绑定,继续讲述WPF中DataGrid分页. 由于分页经常用到,就做了一个自定义控件,由于当时的局限性,只支持DataTable数据源,不过木关系,网上很多其他 ...

  5. SQLITE 时间字段操作函数

    SQLite中的时间日期函数 这是我学习SQLite时做的笔记,参考并翻译了Chris Newman写的<SQLite>中的<Working with Dates and Times ...

  6. P2342 叠积木

    P2342 叠积木 17通过 66提交 题目提供者wwqk4444 标签树状数组线段树USACO 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 Cube Stacki ...

  7. 关于Jquery 序列化表单的注意事项

    在JQuery 的serialize方法序列化表单的过程中,如果表单的name值最后有空格,会出现“+”号,查源码可见原因.这一小问题就浪费了半小时的时间,记录下来,备忘.

  8. Oracle&lowbar;11gR2&lowbar;概念&lowbar;第06章&lowbar;数据字典和动态性能视图&lowbar;英文词汇

    decode 解码 be intend for  适应 distinguished 显著的,突出的 implied 隐含的 abbreviated     简短的 enabled roles    已 ...

  9. 给电脑插上无线网卡,变成路由器----Windows系统承载网络的使用

    1. 以管理员身份运行命令提示符(PowerShell) 2. 启用并设定虚拟wifi网卡 netsh wlan set hostednetwork mode=allow ssid=wifi名称 ke ...

  10. Windows10 1709正式版WSL安装(以Ubuntu为例)

    因为最近要使用Linux搭服务器,但是用远程的话延迟很烦,用双系统切换很麻烦,用虚拟机又会有点卡,刚好Windows10最近更新了正式版的WSL(windows下的Linux子系统),所以就想尝试一下 ...