- 过滤器Filter
- 快速入门
- 详情
- 登录校验-Filter
-
package com.itheima.filter; import com.alibaba.fastjson.JSONObject; import com.itheima.pojo.Result; import com.itheima.utils.JwtUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author Mr.jia * @version 1.0 */ @Slf4j @WebFilter(urlPatterns = "/*") public class LoginCheckFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; //1.获取请求url String url = req.getRequestURI().toString(); log.info("请求的url:{}",url); //2.判断请求url中是否包含login,如果包含,说明是登录操作,放行 if(url.contains("login")){ log.info("登录操作,放行..."); filterChain.doFilter(servletRequest,servletResponse); return; } //3.获取请求头中的令牌 String jwt = req.getHeader("token"); //4.判断令牌是否存在,如果不存在,返回错误结果(未登录) if(!StringUtils.hasLength(jwt)){ log.info("请求头token为空,返回登录的信息"); Result error = Result.error("NOT_LOGIN"); //手动转换 对象---json----->阿里巴巴fastJSON String notLogin = JSONObject.toJSONString(error); resp.getWriter().write(notLogin); return; } //5.解析token,如果解析失败,返回错误结果(未登录) try { JwtUtils.parseJWT(jwt); } catch (Exception e) {//解析失败 e.printStackTrace(); log.info("解析令牌失败,返回登录的信息"); Result error = Result.error("NOT_LOGIN"); //手动转换 对象---json----->阿里巴巴fastJSON String notLogin = JSONObject.toJSONString(error); resp.getWriter().write(notLogin); return; } //6.放行 log.info("令牌合法,放行"); filterChain.doFilter(servletRequest,servletResponse); } }
- 快速入门
- 拦截器Interceptor
- 简介&快速入门
-
package com.itheima.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author Mr.jia * @version 1.0 */ @Component public class LoginCheckInterceptor implements HandlerInterceptor { @Override //目标资源方法运行前运行,返回true:放行,false:不放行 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override //目标资源方法运行后运行 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } @Override //视图渲染完毕后运行,最后运行 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
定义拦截器文章来源:https://www.toymoban.com/news/detail-465242.html
-
package com.itheima.config; import com.itheima.interceptor.LoginCheckInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author Mr.jia * @version 1.0 */ @Configuration//配置类 public class WebConfig implements WebMvcConfigurer { @Autowired private LoginCheckInterceptor loginCheckInterceptor; @Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**"); } }
配置拦截器文章来源地址https://www.toymoban.com/news/detail-465242.html
- 详解(拦截路径,执行流程)
- 登录校验-Interceptor
-
/** * @author Mr.jia * @version 1.0 */ @Slf4j @Component public class LoginCheckInterceptor implements HandlerInterceptor { @Override //目标资源方法运行前运行,返回true:放行,false:不放行 public boolean preHandle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler) throws Exception { //1.获取请求url String url = servletRequest.getRequestURI().toString(); log.info("请求的url:{}",url); //2.判断请求url中是否包含login,如果包含,说明是登录操作,放行 if(url.contains("login")){ log.info("登录操作,放行..."); return true; } //3.获取请求头中的令牌 String jwt = servletRequest.getHeader("token"); //4.判断令牌是否存在,如果不存在,返回错误结果(未登录) if(!StringUtils.hasLength(jwt)){ log.info("请求头token为空,返回登录的信息"); Result error = Result.error("NOT_LOGIN"); //手动转换 对象---json----->阿里巴巴fastJSON String notLogin = JSONObject.toJSONString(error); servletResponse.getWriter().write(notLogin); return false; } //5.解析token,如果解析失败,返回错误结果(未登录) try { JwtUtils.parseJWT(jwt); } catch (Exception e) {//解析失败 e.printStackTrace(); log.info("解析令牌失败,返回登录的信息"); Result error = Result.error("NOT_LOGIN"); //手动转换 对象---json----->阿里巴巴fastJSON String notLogin = JSONObject.toJSONString(error); servletResponse.getWriter().write(notLogin); return false; } //6.放行 log.info("令牌合法,放行"); return true; } @Override //目标资源方法运行后运行 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } @Override //视图渲染完毕后运行,最后运行 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
- 简介&快速入门
到了这里,关于过滤器Filter,拦截器Interceptor的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!