// Path: src/main/java/cn/iflyapi/blog/model/JSONResult.java
// @Getter
// @Setter
// public class JSONResult {
// 
//     private int code;
//     private String msg;
//     private boolean success;
//     private Object data;
// 
//     private JSONResult(int code, String msg, boolean success, Object data) {
//         this.code = code;
//         this.msg = msg;
//         this.success = success;
//         this.data = data;
//     }
// 
//     public static JSONResult ok(int code, String msg, Object data) {
//         return new JSONResult(code, msg, true, data);
//     }
// 
//     public static JSONResult ok(CodeMsgEnum codeMsgEnum, Object data) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), data);
//     }
// 
//     public static JSONResult ok(Object data) {
//         return ok(CodeMsgEnum.OK, data);
//     }
// 
//     public static JSONResult ok() {
//         return ok(CodeMsgEnum.OK, null);
//     }
// 
//     public static JSONResult fail(int code, String msg) {
//         return new JSONResult(code, msg, false, null);
//     }
// 
//     public static JSONResult fail(CodeMsgEnum codeMsgEnum, Object data) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), data);
//     }
// 
//     public static JSONResult fail(CodeMsgEnum codeMsgEnum) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), null);
//     }
// 
//     public static JSONResult fail(Object data) {
//         return ok(CodeMsgEnum.FAIL, data);
//     }
// 
//     public static JSONResult fail() {
//         return ok(CodeMsgEnum.FAIL, null);
//     }
// }
// 
// Path: src/main/java/cn/iflyapi/blog/util/JwtUtils.java
// public class JwtUtils {
// 
//     private final static Logger logger = LoggerFactory.getLogger(JwtUtils.class);
// 
//     //一个月
//     private static long EXPIRED_TIME = 60 * 60 * 24 * 30;
// 
//     private static String key = "secret";
// 
//     public static String getToken(Long userId, String nickName) throws Exception {
//         Map<String, Object> mapHeader = new HashMap<>(2);
//         mapHeader.put("alg", "HS256");
//         mapHeader.put("typ", "JWT");
//         long iat = System.currentTimeMillis();
//         long exp = iat + EXPIRED_TIME * 1000L;
//         return JWT.create()
//                 .withHeader(mapHeader)
//                 .withIssuedAt(new Date(iat))
//                 .withExpiresAt(new Date(exp))
//                 .withClaim("userId", String.valueOf(userId))
//                 .withClaim("nickName", nickName)
//                 .sign(Algorithm.HMAC256(key));
//     }
// 
//     public static Map<String, Claim> verify(String token) {
//         DecodedJWT jwt;
//         try {
//             JWTVerifier verifier = JWT.require(Algorithm.HMAC256(key)).build();
//             jwt = verifier.verify(token);
//             Map<String, Claim> claims = jwt.getClaims();
//             return jwt.getClaims();
//         } catch (Exception ex) {
//             logger.error("verify jwt token failed, token={}", token, ex);
//             return null;
//         }
// 
//     }
// 
// 
// /*    public static void main(String[] args) {
//         Map<String, Object> map = new HashMap<>();
//         map.put("userName", "flyhero");
//         try {
//             String t = getToken(map);
//             Thread.sleep(10000);
//             verify(t);
//         } catch (InvalidClaimException e) {
//             System.out.println("InvalidClaimException");
//         } catch (SignatureVerificationException e) {
//             System.out.println("SignatureVerificationException");
//         } catch (Exception e) {
// 
//         }
//     }*/
// }


import cn.iflyapi.blog.model.JSONResult;
import cn.iflyapi.blog.util.JwtUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.auth0.jwt.interfaces.Claim;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.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.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
package cn.iflyapi.blog.config;




public class JwtInterceptor implements HandlerInterceptor {

    private final Logger logger = LoggerFactory.getLogger(JwtInterceptor.class);

    private List<String> excludedUrl = new ArrayList<>();

    public void register(String url) {
        excludedUrl.add(url);
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        try {
            //过滤 OPTIONS 请求
            if (HttpMethod.OPTIONS.name().equals(httpServletRequest.getMethod())) {
                return true;
            }
            System.out.println(excludedUrl.toString());
            String currentPath = httpServletRequest.getMethod() + httpServletRequest.getRequestURI();
            for (String target : excludedUrl) {
                if (pathMatch(currentPath, target)) {
                    return true;
                }
            }

            String token = httpServletRequest.getHeader("Authorization");

            if (StringUtils.isEmpty(token)) {
                logger.error("token validate failed, no auth info in http header");
                return setNoAuth(httpServletResponse);
            }

// Path: src/main/java/cn/iflyapi/blog/model/JSONResult.java
// @Getter
// @Setter
// public class JSONResult {
// 
//     private int code;
//     private String msg;
//     private boolean success;
//     private Object data;
// 
//     private JSONResult(int code, String msg, boolean success, Object data) {
//         this.code = code;
//         this.msg = msg;
//         this.success = success;
//         this.data = data;
//     }
// 
//     public static JSONResult ok(int code, String msg, Object data) {
//         return new JSONResult(code, msg, true, data);
//     }
// 
//     public static JSONResult ok(CodeMsgEnum codeMsgEnum, Object data) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), data);
//     }
// 
//     public static JSONResult ok(Object data) {
//         return ok(CodeMsgEnum.OK, data);
//     }
// 
//     public static JSONResult ok() {
//         return ok(CodeMsgEnum.OK, null);
//     }
// 
//     public static JSONResult fail(int code, String msg) {
//         return new JSONResult(code, msg, false, null);
//     }
// 
//     public static JSONResult fail(CodeMsgEnum codeMsgEnum, Object data) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), data);
//     }
// 
//     public static JSONResult fail(CodeMsgEnum codeMsgEnum) {
//         return ok(codeMsgEnum.getCode(), codeMsgEnum.getMsg(), null);
//     }
// 
//     public static JSONResult fail(Object data) {
//         return ok(CodeMsgEnum.FAIL, data);
//     }
// 
//     public static JSONResult fail() {
//         return ok(CodeMsgEnum.FAIL, null);
//     }
// }
// 
// Path: src/main/java/cn/iflyapi/blog/util/JwtUtils.java
// public class JwtUtils {
// 
//     private final static Logger logger = LoggerFactory.getLogger(JwtUtils.class);
// 
//     //一个月
//     private static long EXPIRED_TIME = 60 * 60 * 24 * 30;
// 
//     private static String key = "secret";
// 
//     public static String getToken(Long userId, String nickName) throws Exception {
//         Map<String, Object> mapHeader = new HashMap<>(2);
//         mapHeader.put("alg", "HS256");
//         mapHeader.put("typ", "JWT");
//         long iat = System.currentTimeMillis();
//         long exp = iat + EXPIRED_TIME * 1000L;
//         return JWT.create()
//                 .withHeader(mapHeader)
//                 .withIssuedAt(new Date(iat))
//                 .withExpiresAt(new Date(exp))
//                 .withClaim("userId", String.valueOf(userId))
//                 .withClaim("nickName", nickName)
//                 .sign(Algorithm.HMAC256(key));
//     }
// 
//     public static Map<String, Claim> verify(String token) {
//         DecodedJWT jwt;
//         try {
//             JWTVerifier verifier = JWT.require(Algorithm.HMAC256(key)).build();
//             jwt = verifier.verify(token);
//             Map<String, Claim> claims = jwt.getClaims();
//             return jwt.getClaims();
//         } catch (Exception ex) {
//             logger.error("verify jwt token failed, token={}", token, ex);
//             return null;
//         }
// 
//     }
// 
// 
// /*    public static void main(String[] args) {
//         Map<String, Object> map = new HashMap<>();
//         map.put("userName", "flyhero");
//         try {
//             String t = getToken(map);
//             Thread.sleep(10000);
//             verify(t);
//         } catch (InvalidClaimException e) {
//             System.out.println("InvalidClaimException");
//         } catch (SignatureVerificationException e) {
//             System.out.println("SignatureVerificationException");
//         } catch (Exception e) {
// 
//         }
//     }*/
// }


// Path: src/main/java/cn/iflyapi/blog/config/JwtInterceptor.java
import cn.iflyapi.blog.model.JSONResult;
import cn.iflyapi.blog.util.JwtUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.auth0.jwt.interfaces.Claim;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.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.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

package cn.iflyapi.blog.config;




public class JwtInterceptor implements HandlerInterceptor {

    private final Logger logger = LoggerFactory.getLogger(JwtInterceptor.class);

    private List<String> excludedUrl = new ArrayList<>();

    public void register(String url) {
        excludedUrl.add(url);
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        try {
            //过滤 OPTIONS 请求
            if (HttpMethod.OPTIONS.name().equals(httpServletRequest.getMethod())) {
                return true;
            }
            System.out.println(excludedUrl.toString());
            String currentPath = httpServletRequest.getMethod() + httpServletRequest.getRequestURI();
            for (String target : excludedUrl) {
                if (pathMatch(currentPath, target)) {
                    return true;
                }
            }

            String token = httpServletRequest.getHeader("Authorization");

            if (StringUtils.isEmpty(token)) {
                logger.error("token validate failed, no auth info in http header");
                return setNoAuth(httpServletResponse);
            }

            Map<String, Claim> map = JwtUtils.verify(token);