filter interceptor

caitianxiwo 2011-12-12 01:41:12
filter
web.xml

<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.augmentum.training.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/admin/*</url-pattern>
<url-pattern>/engineer/*</url-pattern>
<url-pattern>/itstaff/*</url-pattern>
<url-pattern>/manager/*</url-pattern>
<url-pattern>/sdm/*</url-pattern>
</filter-mapping>


package com.augmentum.training.filter;

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;
import com.augmentum.training.bean.User;

public class LoginFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest hreq=(HttpServletRequest) req;
HttpServletResponse hres=(HttpServletResponse) res;
HttpSession session=hreq.getSession();
User user=(User) session.getAttribute("user");
String url=hreq.getRequestURI();
if(user!=null){
chain.doFilter(req, res);
}else {
hres.sendRedirect(hreq.getContextPath()+"/login/login.jsp");
}

}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}



}
...全文
71 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
caitianxiwo 2011-12-12
interceptor
struts.xml

<interceptors>
<interceptor name="sessionNull" class="sessionNull"></interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="sessionNull"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myInterceptor"></default-interceptor-ref>
<global-results>
<result name="error" type="redirect">/login/login.jsp</result>
</global-results>


package com.augmentum.training.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.augmentum.training.bean.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;


public class LoginInterceptorAction implements Interceptor {

/**
*
*/
private static final long serialVersionUID = 2230966740382591730L;

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
User user=(User) session.getAttribute("user");
if (user == null) {
return "error";
} else {
return invocation.invoke();
}
}

}
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2011-12-12 01:41
社区公告
暂无公告