会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132420个问题
Python 全系列/第二十三阶段:人工智能基础_机器学习理论与实战/KNN与交叉检验 1007楼
Python 全系列/第一阶段:Python入门/面向对象 1008楼
Python 全系列/第二十一阶段:Python数据分析项目/二手车价格预测 1009楼

ssm-shiro2.zip

老师我已经找了一天的bug了还是没找到错误,麻烦老师帮我看一下,谢谢老师

image.png

JAVA 全系列/第九阶段:权限控制与安全认证/Shiro(旧) 1012楼
Python 全系列/第十三阶段:高并发性能怪兽-Tornado 框架/Tornado项目(旧) 1013楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫 1014楼
Python 全系列/第一阶段:Python入门/面向对象 1015楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 1017楼

老师我想问一下,filter自定义的过滤器没有错,web.xml也配置了,没问题。但是怎么就拦截不了呢?过滤器和配置文件都整好了,但是还是不用登陆就能直接访问。

package com.bjsxt.web.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.HttpSession;

import com.bjsxt.pojo.Users;
/**
 * 判断用户是否登录
 * @author Administrator
 *
 */
public class UserLoginFilter implements Filter {

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

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
            throws IOException, ServletException {
        //获取用户访问的URI
        HttpServletRequest req = (HttpServletRequest)arg0;
        String uri = req.getRequestURI();

        //判断当前访问的URI是否是用户登录资源,如果是则放行
        if(uri.indexOf("login") != -1 || uri.indexOf("userLogin") != -1){
            chain.doFilter(arg0, arg1);
        }else{
            //用户是否登录的判断
            HttpSession session = req.getSession();
            Users user = (Users)session.getAttribute("user");
            if(user != null && user.getUsername().length() > 0){
                chain.doFilter(arg0, arg1);
            }else{
                req.setAttribute("msg", "请登录");
                req.getRequestDispatcher("/login").forward(arg0, arg1);
            }
        }

    }

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

    }

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>userDengLu</display-name>

    <!--解析applicationContext-*.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>

    <!--启动spring-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--定位springmvc的配置文件,并进行解析-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--对网址请求访问的路径进行拦截-->
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <!--除了web目录下的jsp之外的所有请求资源-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--过滤器拦截所有请求资源,对资源在CharacterEncodingFilter里做处理后再响应-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

    <!--自定义的过滤器,先拦截用户的请求资源,将资源在自定义的过滤内进行判断以后再做响应-->
    <filter>
        <filter-name>UserLoginFilter</filter-name>
        <filter-class>com.bjsxt.web.filter.UserLoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UserLoginFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>


JAVA 全系列/第六阶段:项目管理与SSM框架/RBAC实战 1019楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 1020楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637