使用filter过滤器实现权限过滤问题

package 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;

public class adminFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
    FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) srequest;
        HttpServletResponse response = (HttpServletResponse) sresponse;
        HttpSession session = request.getSession();

        Boolean isLogin = (Boolean) session.getAttribute("isLogin");
        if (isLogin==null||isLogin == false) { // 如果没有内容直接跳回首页
            response.sendRedirect("index.jsp");
        } else {
            filterChain.doFilter(request, response);
        }
    }
    public void destroy() {
    }

}



    <filter>
        <filter-name>adminFilter</filter-name>
        <filter-class>filter.adminFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>adminFilter</filter-name>
        <url-pattern>/services/*</url-pattern>
    </filter-mapping>


你可能感兴趣的:(session,filter,null,Class)