过滤器如何设置登录拦截
# 具体遇到的问题
要如何设置拦截BookController下面的网页进行登录验证
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
在这里输入代码,可通过选择【代码语言】突出显示
AuthFilter.java
package com.imooc.library.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;
/**
* 判断用户是否登录的过滤器
* @author 林文佳
*
*/
public class AuthFilter implements Filter {
private String login_path;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
if( req.getSession().getAttribute( "login" ) == null ) {
res.sendRedirect( login_path );
}else {
chain.doFilter(req, res);
}
}
@Override
public void init(FilterConfig filterConfig ) throws ServletException {
// TODO Auto-generated method stub
login_path = filterConfig.getInitParameter( "login_path" );
}
}
web.xml<?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>library</display-name>
<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>
<filter>
<filter-name>EncodeFilter</filter-name>
<filter-class>com.imooc.library.filter.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.imooc.library.filter.AuthFilter</filter-class>
<init-param>
<param-name>login_path</param-name>
<param-value>/management?method=login</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern></url-pattern>
</filter-mapping>
</web-app>
正在回答
同学你好,1、同学通过默认页面index.html跳转到controller,再由controller跳转到登录页面login.jsp,在这个过程中由index.html跳转到controller中,这个controller在拦截器中有没有放行呐。
这样跳转经过路径更多,建议同学直接跳转到login.jsp再来试试。
2、根据同学的描述,打开过滤器会出现循环重定向,查看同学的AuthFilter 中并没有重定向语句,是同学在其它地方添加了重定向语句吗?
另外,建议同学在对应条件下添加路径跳转语句。
package com.imooc.library.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;
/**
* 判断用户是否登录的过滤器
* @author 林文佳
*
*/
public class AuthFilter implements Filter {
private String login_path;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
boolean flag = false;
String requestURI = req.getRequestURI();
String[] url = { ".js", ".css", ".ico", ".jpg", ".png", "/login", ".html" };
for( String str : url ) {
if( requestURI.indexOf( str ) != -1 ) {
flag = true;
break;
}
}
String login = (String)req.getSession().getAttribute( "login" );
if( flag ) {
chain.doFilter( request, response );
}else if( login == null ) {
System.out.println( "我还没登录!" );
}
System.out.println( login + "我是拦截器的" );
}
@Override
public void init(FilterConfig filterConfig ) throws ServletException {
// TODO Auto-generated method stub
login_path = filterConfig.getInitParameter( "login_path" );
System.out.println( login_path + "我是拦截器" );
}
}
老师,这是我过滤器的代码。使用过滤器后直接连登录页面都出不来,整个网页就是一片空白。
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星