加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

springboot+oauth2.0异常重写处理(针对token失效)

发布时间:2020-12-15 07:14:31 所属栏目:Java 来源:网络整理
导读:近来针对微服务框架开发,其中oauth2.0默认返回XML形式的token失效,不符合我们实际的开发需求,于是我参考网上一些博客重写了它,使其符合我们开发的需求。 核心主要涉及两个类: import com.eqics.common.security.utils.ResultJsonUtil;import org.springfr

近来针对微服务框架开发,其中oauth2.0默认返回XML形式的token失效,不符合我们实际的开发需求,于是我参考网上一些博客重写了它,使其符合我们开发的需求。

核心主要涉及两个类:

import com.eqics.common.security.utils.ResultJsonUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {


    @Override
    void commence(HttpServletRequest request,HttpServletResponse response,AuthenticationException authException) throws ServletException {
        Map<String,Object> map = new HashMap<String,Object>();
        Throwable cause = authException.getCause();

        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-Type",application/json;charset=UTF-8");
        try {
            if (cause instanceof InvalidTokenException) {

                response.getWriter().write(ResultJsonUtil.build(
                        222222,token失效
                ));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ResourceServerConfig.java类中补充如下(找到主要方法):

@Override
 configure(ResourceServerSecurityConfigurer resources) {
    resources.tokenServices(tokenServices());
    resources.authenticationEntryPoint(new AuthExceptionEntryPoint());

}

还有一个工具类ResultJsonUtil.java,内容如下:

import java.util.List;
import java.util.Map;
class ResultJsonUtil<T> {

    private int code;
     statusCode;
    private String msg;
     T data;

    static final int DEFAULT_STATUS_CODE = 0;

    /**
     * construction
     *
     * @param code       请求状态码
     * @param statusCode 信息状态码
     * @param msg        信息
     * @param data       数据
     */
    public ResultJsonUtil(int code, statusCode,String msg,T data) {
        this.code = code;
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    static String build(new ResultJsonUtil<>(code,statusCode,msg,1)">""return resultJsonUtil.getResultJson();
    }

     code,String msg) {
         ResultJsonUtil.build(code,ResultJsonUtil.DEFAULT_STATUS_CODE,msg);
    }

    new ResultJsonUtil<>(code,data);
         JSONObject.parSEObject(JSON.toJSONString(data));
        ResultJsonUtil<JSONObject> resultJsonUtil =  JSONArray.parseArray(JSON.toJSONString(data));
         String getResultJson() {
        JSONObject jsonObject =  JSONObject();
        jsonObject.put(codethis.code);
        jsonObject.put(msg.msg);
         JSON.toJSONString(jsonObject,SerializerFeature.DisableCircularReferenceDetect);
    }
}

本文主要参考了这篇文章:
Spring Cloud:Security OAuth2 自定义异常响应

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读