判断用户是否登录的过滤器AuthFilter,登录页显示不了

判断用户是否登录的过滤器AuthFilter,登录页显示不了

编写了AuthFilter,配置后运行,登录页面显示不了

package com.imooc.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 com.imooc.domain.User;


public class AuthFilter implements Filter {


@Override

public void destroy() {

// TODO Auto-generated method stub


}


@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

HttpServletResponse res = (HttpServletResponse) response;

User user = (User) req.getSession().getAttribute("existUser");

if (user == null) {

res.sendRedirect(req.getContextPath() + "/login.jsp");

} else {

chain.doFilter(request, response);

}

}


@Override

public void init(FilterConfig filterConfig) 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://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

id="WebApp_ID" version="3.1">

<display-name>librarymanagement</display-name>


<servlet>

<servlet-name>KaptchaServlet</servlet-name>

<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>

<!-- 修改随机的字母或数字的长度为4 -->

<init-param>

<param-name>kaptcha.textproducer.char.length</param-name>

<param-value>4</param-value>

</init-param>

<!-- 修改随机的字母或数字的间距为20 -->

<init-param>

<param-name>kaptcha.textproducer.char.space</param-name>

<param-value>20</param-value>

</init-param>

<!-- 修改随机的字母或数字的图片样式 -->

<init-param>

<param-name>kaptcha.obscurificator.impl</param-name>

<param-value>com.google.code.kaptcha.impl.WaterRipple</param-value>


</init-param>

</servlet>

<servlet-mapping>

<servlet-name>KaptchaServlet</servlet-name>

<url-pattern>/KaptchaServlet</url-pattern>

</servlet-mapping>


<!-- 使用字符过滤器 EncodingFilter -->

<filter>

<filter-name>EncodingFilter</filter-name>

<filter-class>com.imooc.filter.EncodingFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>EncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


<!-- 使用登录判断过滤器AuthFilter,未登录则跳转至登录页面 -->

<filter>

<filter-name>AuthFilter</filter-name>

<filter-class>com.imooc.filter.AuthFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>AuthFilter</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>

+++++++++++++++++++++

http://img1.sycdn.imooc.com//climg/5f0ac8d909eaa72510120574.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿满 2020-07-12 17:34:16

同学你好,报错提示重定向次数太多,如:

http://img1.sycdn.imooc.com//climg/5f0ad68909f66cff03650198.jpg

在web.xml中配置如下,将所有的请求都进行了拦截,如:

http://img1.sycdn.imooc.com//climg/5f0ad6cf0931572805960368.jpg

在过滤器中判断是否登录,如:

http://img1.sycdn.imooc.com//climg/5f0ad6f4091370e406930453.jpg

这里访问login.jsp时拦截,过滤器中判断没有登录,重定向到login.jsp,然后又拦截,又重定向,如此循环往复,造成的报错。

建议在AuthFilter过滤器中增加不拦截的路径,比如登录页面不拦截,js,css,png等不拦截,如:

http://img1.sycdn.imooc.com//climg/5f0ad906098bd8f810080478.jpg

判断如果是不拦截的路径,则直接放行。

祝:学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师