问题

今天开发过程中,写了个拦截器,对于所以请求进行一个token的工作,对于不合标准的token返回错误,在网上找了个拦截器进行二次开发。

package com.maizhiyu.yzt.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
        //Axios 发起跨域请求前,浏览器也会首先发起 OPTIONS 预检请求。检查服务器是否允许跨域访问。
        System.out.println("拦截的请求路径:" + request.getRequestURI());
        System.out.println("拦截的请求方法:" + request.getMethod());
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            System.out.println("允许跨域访问");
            return true;
        }

        response.setCharacterEncoding("utf-8");
        int  index = request.getHeader("cookie").indexOf("token:");
        String token = request.getHeader("cookie").substring(index);
        if(token != null){
            if(true){
                Object o = redisTemplate.opsForValue().get("yztMsCusToken:login:token:" + token);
                if(o==null){
                    System.out.println("token过期或者在用户退出的时候已删除,redis找不到");
                    return   false;
                }
                System.out.println("通过拦截器");
                return true;
            }
        }
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = null;
        response.getWriter().write("认证失败,错误码:50000");
        return false;
    }
}

但是发现这里返回的值是一个String。

但公司的要求是按照固定的Result的格式进行返回的,这就需要转一下了。一开的想法是把里面的直接String转变为Result.toString。

response.getWriter().write(Result.failure(401,"token非法").toString());

结果也是不合标准的。

解决方案

使用JSONObject

JSONObject jsonObject =new JSONObject();
jsonObject.put("code",401);
jsonObject.put("msg","失败");

response.getWriter().write(jsonObject.toString());
response.setContentType("application/json; charset=utf-8");

输出结果

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2134cb94 * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance * fix ci_static_analysis_clang (ci_clang_tidy) * change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance 4 天前
6057b31d * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * Use ubuntu-latest image to run Valgrind (#4575) * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Clang image to run valgrind * :wrench: use Ubuntu image to run valgrind * Use Clang image to run iwyu (#4574) * :wrench: use Clang image to run iwyu * :wrench: use Clang image to run iwyu * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :wrench: overwork astyle call * :art: format code * :hammer: clean up 6 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐