Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2290941
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-01-12 10:50:42

系统提升三:系统安全访问
 
数据安全分为两个级别:
用户身份认证: 对用户的访问和操作进行纷纷验证 只允许登陆地 有权限的用户进行合法的操作
用户数据加密: 对网络上传输的用户输入的数据进行加密 防止网络上的窃听者解密
第二种不是J3EE应用系统的编码所能解决的问题 它处于传输层 我们可以通过SSL进行网络数据的加密传输和认证
第一种是开发任何系统都应该考虑的问题
 
 
1 建立公用函数类BaseActionm.java
进行用户权限检查的代码通常都放在Action类的函数中 这种检查的函数被所有的Action类所共有 因此我们首先建立一个基类 BaseAction.java  它继承自Struts的DispatchAction
然后再该类中添加公用的函数 例如 保存Session对象的函数setSession() 从Session中取的对象的函数getSession() 从Session中取得用户名函数getUsername() 判断Session中某一个对象是否存在的函数i***istSession() 用户是否登录的函数isTimeout() 等
 
  1. package com.demo.struts.actions;

  2. import javax.servlet.http.HttpServletRequest;

  3. import org.apache.struts.actions.DispatchAction;

  4. import com.demo.struts.util.Constants;

  5. public class BaseAction extends DispatchAction {
  6.     
  7.     /**
  8.      * get username from session
  9.      * @param request
  10.      * @return
  11.      */
  12.     protected String getUsername(HttpServletRequest request) {
  13.         return (String) request.getSession().getAttribute(
  14.                 Constants.USERNAME_KEY);
  15.     }
  16.     
  17.     /**
  18.      * check if user is timeout
  19.      * @param request
  20.      * @return
  21.      */
  22.     protected boolean isTimeout(HttpServletRequest request) {
  23.         if (request.getSession().getAttribute(Constants.USERNAME_KEY) == null) {
  24.             return true;
  25.         } else {
  26.             return false;
  27.         }
  28.     }
  29.     
  30.     /**
  31.      * save object in session
  32.      * @param request
  33.      * @param key
  34.      * @param obj
  35.      */
  36.     protected void setSession(HttpServletRequest request, String key, Object obj) {
  37.         request.getSession().setAttribute(key, obj);
  38.     }
  39.     
  40.     /*
  41.      * check if obj exists in session
  42.      */
  43.     protected boolean i***istSession(HttpServletRequest request, String key) {
  44.         if (request.getSession().getAttribute(key) != null) {
  45.             return true;
  46.         } else {
  47.             return false;
  48.         }
  49.     }
  50.     
  51.     /**
  52.      * get object from session
  53.      * @param request
  54.      * @param key
  55.      * @return
  56.      */
  57.     protected Object getSession(HttpServletRequest request, String key) {
  58.         return request.getSession().getAttribute(key);
  59.     }
  60. }
将所有的Action类都集成该基类 这样在自己的Action类中即可使用这里的公用函数了
 
 
2 验证用户是否登录
在执行一个Action函数之前 判断用户是否登录  此时我们就可以使用BaseAction.java的isTimeout()函数了
首先在struts-config.xml的全局定义一个别名跳转到login.jsp 用于在用户为登录时跳转到该页面
  1. <global-forwards>
  2.         <forward name="welcome" path="/welcome.do" />
  3.         <forward name="login" path="/login.do" />
  4.         <forward name="index" path="/login.jsp" />
  5.     </global-forwards>
在Constans,java中定义一个Key值 赋值为上面定义的别名
  1. public final static String INDEX_KEY = "index";
在需要检验Session是否有效的Action中添加检查的代码 如在AddressAction和LogoutAction  首先需要修改他们的继承类为BaseAction.java 然后在每一个函数的最开始添加如下的代码 如果判读失败 那么就跳转到前面定义的登陆页
  1. public class AddressAction extends BaseAction {


  2. 。。。。


  3.     public ActionForward back(ActionMapping mapping, ActionForm form,
  4.             HttpServletRequest request, HttpServletResponse response)
  5.             throws Exception {
  6.         if (isTimeout(request)) {
  7.          return mapping.findForward(Constants.INDEX_KEY);
  8.         }

  9. 。。。。
 
阅读(1787) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~