您的位置:首页 > 编程语言 > Java开发

Struts2实现登录权限访问控制

2015-04-26 14:07 525 查看
目录:

Ⅰ 条件

Ⅱ 目的

Ⅲ 分析

Ⅳ 实现

Ⅴ 具体代码实现

--------------------------------------------------------------------------------------

一、条件
  1. 假设有个项目的目录如图一所示。(图一)
  2. 其中login.jsp和index.jsp是放在/WebRoot目录下的。其余的放在相应的文件夹里。
  3. 现在要求用户在未登录的情况下,只能进入login.jsp页面,如果登录了,就可以进入index.jsp和其它页面。
  4. 如果用户直接在地址栏上输入URI进行非法请求,既绕过登录验证直接访问其它内容页的话,将重定向页面到login.jsp页面。


(图一)
二、目的
  1. 防止用户直接输入URL绕过登录进行访问;

三、分析
  1. URL的形式有两种,一种是通过Struts2的*.action,另一种是jsp的*.jsp,都要对其进行控制。
  2. *.action形式的访问使用struts2的拦截器进行验证。
  3. *.jsp形式的访问使用Filter过滤器进行验证。
四、实现
  1. Filter的实现:
    ①. 获取请求的URI;并把该URI截取到访问的相对路径,可以通过这个来进行相应的权限控制。把最后的地址保存为uri;

      如:/ElectiveSystem/login.jsp ---截取---> /login.jsp
    ②. 判断uri是否为"login.jsp",如果是,直接放行;如果不是,那么就通过request.getSession()获取session,在通过session

      获取用户登录信息,如果登录信息为空,则重定向到login.jsp页面;如果不为空,那么就直接放行。

  2. Interceptor的实现(Struts2 的拦截器):
   ①. 通过invocation获取ActionContext对象。 (ActionContext context = invocation.getInvocationContext())
   ②. 再通过ActionContext对象获取HttpServletRequest对象。

(HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST))

   ③. 获取请求的URI;并把该URI截取到访问的相对路径,可以通过这个来进行相应的权限控制。把最后的地址保存为uri;

     如: /ElectiveSystem/admin/admin_login.action -->/admin/admin_login.action
   ④. 判断uri。
五、具体代码实现:

PowerFilter:

@Override

public
void doFilter(ServletRequest req, ServletResponse resp,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) resp;


//取得根目录的绝对路径

String currentURL = request.getRequestURI();

//        System.out.println("当前请求的路径: " + currentURL);

//截取到访问的相对路径,可以通过这个来进行相应的权限控制

currentURL =currentURL.substring(currentURL.indexOf("/", 1), currentURL.length());

//        System.out.println("当前请求的路径: " + currentURL);

if("/login.jsp".equals(currentURL)||"/login.jsp"==currentURL) {

//所有人都能请求到的URI,放行

chain.doFilter(request, response);

} else {    //下面是判断是否有session,也就是用户是否已登录状态;

HttpSession session = request.getSession();


Object obj = session.getAttribute("user");

if(obj == null) {

//System.out.println("URI:" + currentURL + ">>>>访问被拒绝!");

response.sendRedirect("/ElectiveSystem/login.jsp");

} else {

    chain.doFilter(request, response);

}

}

}

PowerInterceptor:

@Override

public String intercept(ActionInvocation invocation) throws Exception {

ActionContext context = invocation.getInvocationContext();

//通过ActionContext来获取httpRequest

HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);

//也可以通过ServletActionContext来获取httpRequest

//HttpServletRequest request = ServletActionContext.getRequest();

//取得根目录的绝对路径

String currentURL = request.getRequestURI();

//截取到访问的相对路径,可以通过这个来进行相应的权限控制

//      System.out.println("当前请求的路径: " + currentURL); // 如:  /ElectiveSystem/admin/admin_login.action

String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length());

//      System.out.println("当前请求的路径: " + currentURL); //如:  /admin/admin_login.action

if("/admin/admin_login.action".equals(targetURL)||"/stu/stu_login.action".equals(targetURL)||"/th/th_login.action".equals(targetURL)){

//执行方法,获取返回值

String result = invocation.invoke();

//System.out.println(result);

return result;

}else{

//通过ActionContext获取session的信息。

Object obj = context.getSession().get("user");

//获取容器里面的user值,如果存在说明该用户已经登录,让他执行操作,如果未登录让他进行登录

if(obj!=null){

return invocation.invoke();

}

}

return "input";


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: