Java教程

过滤器提示未登录

本文主要是介绍过滤器提示未登录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

			package net.xdclass.demoproject.filter;
			
			import com.fasterxml.jackson.databind.ObjectMapper;
			import net.xdclass.demoproject.domain.User;
			import net.xdclass.demoproject.service.impl.UserServiceImpl;
			import net.xdclass.demoproject.utils.JsonData;
			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;
			import java.io.PrintWriter;
			
			//@WebFilter(urlPatterns = "/api/v1/pri/*", filterName = "loginFilter")
			public class LoginFilter implements Filter {
			
			
			    private static final ObjectMapper objectMapper = new ObjectMapper();
			
			    /**
			     * 容器加载的时候
			     * @param filterConfig
			     * @throws ServletException
			     */
			    @Override
			    public void init(FilterConfig filterConfig) throws ServletException {
			
			        System.out.println("init LoginFilter======");
			    }
			
			
			
			    @Override
			    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
			
			        System.out.println("doFilter LoginFilter======");
			
			        HttpServletRequest req = (HttpServletRequest) servletRequest;
			
			        HttpServletResponse resp = (HttpServletResponse) servletResponse;
			
			        //先从请求头里拿token,没有的话就从请求参数里面拿
			        String token = req.getHeader("token");
			        if(StringUtils.isEmpty(token)){
			            token = req.getParameter("token");
			        }
			
			
			        if(!StringUtils.isEmpty(token)){
			
			            //判断token是否合法
			            User user = UserServiceImpl.sessionMap.get(token);
			            if(user!=null){
			                filterChain.doFilter(servletRequest,servletResponse);
			            }else {
			
			               JsonData jsonData =  JsonData.buildError("登录失败,token无效",-2);
			                String jsonStr = objectMapper.writeValueAsString(jsonData);
			                renderJson(resp,jsonStr);
			
			            }
			
			        }else {
			
			            JsonData jsonData =  JsonData.buildError("未登录",-3);
			            String jsonStr = objectMapper.writeValueAsString(jsonData);
			            renderJson(resp,jsonStr);
			        }
			
			    }
			
			    private void renderJson(HttpServletResponse response,String json){
			
			        response.setCharacterEncoding("UTF-8");
			        response.setContentType("application/json");
			
			        try(PrintWriter writer = response.getWriter()){
			            writer.print(json);
			        }catch (Exception e){
			            e.printStackTrace();
			        }
			
			    }
			
			
			
			
			
			
			    /**
			     * 容器销毁的时候
			     */
			    @Override
			    public void destroy() {
			
			        System.out.println("destroy LoginFilter======");
			
			    }
			
			
			}

  

这篇关于过滤器提示未登录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!