Struts2使用过滤器和拦截器进行简单权限校验
2008-10-10 11:21
756 查看
在一些用到用户登录的目录应该进行权限判断,以防非法登录,我在这里进行一个简单的权限校验。
1.使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:
<filter>
<filter-name>access filter</filter-name>
<filter-class>
com.test.news.util.AccessFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>access filter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
下面是过滤的实现类:
package com.test.news.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AccessFilter implements Filter {
/**
* @author chaoyin
*/
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
response.sendRedirect("login.jsp");
return ;
}
filterChain.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
这样/admin 目录下需经过login.jsp这个页面才能正常访问。
2.使用Struts2的拦截器功能进行.action的过滤,主要是校验那些与后台管理相关的action,首先在struts.xml中进行拦截器的配置:
<package name="newsDemo" extends="struts-default"
namespace="/admin">
<interceptors>
<interceptor name="auth" class="com.test.news.util.AccessInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
</interceptor-stack>
</interceptors>
<!-- action -->
<action name="newsAdminView!*" class="newsAction"
method="{1}">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authStack">
</interceptor-ref>
下面是我实现的Interceptor class:
package com.test.news.util;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.news.action.AdminLoginAction;
/**
* @author chaoyin
*/
public class AccessInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -4291195782860785705L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext = actionInvocation.getInvocationContext();
Map session = actionContext.getSession();
//except login action
Object action = actionInvocation.getAction();
if (action instanceof AdminLoginAction) {
return actionInvocation.invoke();
}
//check session
if(session.get("user")==null ){
return "logout";
}
return actionInvocation.invoke();//go on
}
}
这样只保留了login action其它的与管理相关的acton都会被拦截.
最后,输入一些与/admin 下login以外的jsp或是action都将被转到login.jsp页面。
1.使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:
<filter>
<filter-name>access filter</filter-name>
<filter-class>
com.test.news.util.AccessFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>access filter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
下面是过滤的实现类:
package com.test.news.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AccessFilter implements Filter {
/**
* @author chaoyin
*/
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
response.sendRedirect("login.jsp");
return ;
}
filterChain.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
这样/admin 目录下需经过login.jsp这个页面才能正常访问。
2.使用Struts2的拦截器功能进行.action的过滤,主要是校验那些与后台管理相关的action,首先在struts.xml中进行拦截器的配置:
<package name="newsDemo" extends="struts-default"
namespace="/admin">
<interceptors>
<interceptor name="auth" class="com.test.news.util.AccessInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
</interceptor-stack>
</interceptors>
<!-- action -->
<action name="newsAdminView!*" class="newsAction"
method="{1}">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authStack">
</interceptor-ref>
下面是我实现的Interceptor class:
package com.test.news.util;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.news.action.AdminLoginAction;
/**
* @author chaoyin
*/
public class AccessInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -4291195782860785705L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext = actionInvocation.getInvocationContext();
Map session = actionContext.getSession();
//except login action
Object action = actionInvocation.getAction();
if (action instanceof AdminLoginAction) {
return actionInvocation.invoke();
}
//check session
if(session.get("user")==null ){
return "logout";
}
return actionInvocation.invoke();//go on
}
}
这样只保留了login action其它的与管理相关的acton都会被拦截.
最后,输入一些与/admin 下login以外的jsp或是action都将被转到login.jsp页面。
相关文章推荐
- Struts2使用过滤器和拦截器进行简单权限校验
- Struts2中使用拦截器进行权限控制
- 简单的利用Struts2拦截器实现权限校验
- 使用Struts2拦截器进行权限控制
- struts2如何使用拦截器进行用户权限控制实例
- struts2如何使用拦截器进行用户权限控制
- struts2 角色权限 filter(过滤器)和interceptor(拦截器)
- springmvc和struts2拦截器的简单使用以及配置
- 使用struts2拦截器,对session失效的校验
- struts2使用XML框架进行输入校验
- 通过注解在方法级别上使用拦截器做登录权限校验
- struts2中使用拦截器(Interceptor)控制登录和权限
- struts2中定义拦截器对访问权限进行控制
- struts2技巧与解惑——使用包(package)和拦截器(interceptor)分配和处理权限
- Struts2 权限验证 ---拦截器和过滤器
- struts2中使用session和拦截器控制权限
- 拦截器权限控制使用Struts 拦截namespace进行权限控制-java教程
- SSM项目,使用拦截器进行权限控制
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互