package com.pacmp.config.intercepters; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.pacmp.dto.User; import com.pacmp.vo.ResponseData; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import java.io.IOException; import java.io.PrintWriter; /** * @Author xxxx * @Date 2020/6/10 19:17 * @Version 1.0 * @Description 登录拦截器 */ @Slf4j @Component public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session=request.getSession(); User user = (User)session.getAttribute("User"); if(user!=null){ return true; } returnJson(response); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } private void returnJson(HttpServletResponse response){ PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); try { writer = response.getWriter(); ResponseData responseData = new ResponseData(); responseData.setCode(500); responseData.setMsg("您已登出,请重新登录!"); writer.print(responseData); } catch (IOException e){ log.error("拦截器输出流异常",e); } finally { if(writer != null){ writer.close(); } } } }
package com.pacmp.config; import com.pacmp.config.intercepters.LoginInterceptor; import lombok.extern.slf4j.Slf4j; 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 xxxxx * @Date 2020/6/10 19:30 * @Version 1.0 * @Description 注册登录拦截器 */ @Slf4j @Configuration public class InterceptorConfig implements WebMvcConfigurer{ @Autowired private LoginInterceptor interceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(interceptor) .addPathPatterns("/**") .excludePathPatterns("/login/*"); log.info("登录拦截器注册成功!"); } }
package com.pacmp.vo; import lombok.Data; import java.io.Serializable; /** * @Author xxxx * @Date 2020/6/5 10:41 * @Version 1.0 * @Description 返回前端数据 */ @Data public class ResponseData implements Serializable { private int code; private String msg; private Object results; }
package com.pacmp.dto; import lombok.Data; import java.io.Serializable; /** * @Author xxxx * @Date 2020/6/11 11:55 * @Version 1.0 * @Description 用户实体类 */ @Data public class User implements Serializable { /** * 用户id */ private Integer userId; /** * 用户账号 */ private String userName; private String sessionId; }