zoukankan      html  css  js  c++  java
  • yb课堂之用户登陆校验拦截器开发 《十一》

    开发对应的登陆拦截器

    开发loginInterceptor

    • 登陆校验成功放行
    • 登陆不成功返回json数据

    LoginInterceptor.java

    package net.ybclass.online_ybclass.interceptor;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import io.jsonwebtoken.Claims;
    import net.ybclass.online_ybclass.utils.JWTUtils;
    import net.ybclass.online_ybclass.utils.JsonData;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.PrintWriter;
    
    public class LoginInterceptor implements HandlerInterceptor {
        /**
         * 进入到Controller之前的方法
         *
         * @param request
         * @param response
         * @param handler
         * @return
         * @throws Exception
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            try {
                String accesToken = request.getHeader("token");
                if (accesToken == null) {
                    accesToken = request.getParameter("token");
                }
                if (StringUtils.isNoneBlank(accesToken)) {
                    Claims claims = JWTUtils.checkJWT(accesToken);
                    if (claims == null) {
                        sendJsonMessage(response, JsonData.buildError("登陆过期,请重新登陆"));
                        //告诉登陆过期,重新登陆
                        return false;
                    }
                    Integer id = (Integer) claims.get("id");
                    String name = (String) claims.get("name");
                    request.setAttribute("user_id", id);
                    request.setAttribute("name", name);
                    return true;
                }
            } catch (Exception e) {
            }
            //登陆失败
            sendJsonMessage(response, JsonData.buildError("登陆过期,请重新登陆"));
            return false;
        }
    
        /**
         * 响应json数据给前端
         * @param response
         * @param obj
         */
        public static void sendJsonMessage(HttpServletResponse response, Object obj) {
            try {
                ObjectMapper objectMapper=new ObjectMapper();
                response.setContentType("application/json;charset=utf-8");
                PrintWriter writer=response.getWriter();
                writer.print(objectMapper.writeValueAsString(obj));
                writer.close();
                writer.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @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 {
    
        }
    }
  • 相关阅读:
    [Angualr 2] Watch for changes
    [Angular 2] Custom Validtors
    [Angular 2] The form export from NgFormControl
    [Angular 2] Validation
    [Angualr 2] Using FormBuilder
    [Angular 2] ng-control & ng-control-group
    [Angular 2] NgNonBindable
    [Ruby] LEVEL 2 Methods and Classes
    [Rails Level 2] Ground up
    [AngularJS] 5 simple ways to speed up your AngularJS application
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13328532.html
Copyright © 2011-2022 走看看