spring-security-oauth2自定义token返回格式

该博客主要展示了如何在Spring Security OAuth2环境中自定义`AuthController`,重写`/oauth/token`和`/oauth/check_token`接口,以统一返回数据格式。通过`@Autowired`注入`TokenEndpoint`和`CheckTokenEndpoint`,实现了获取和校验OAuth2访问令牌的功能。
摘要由CSDN通过智能技术生成

AuthController:

package com.sdkj.security.oauth.controller;

import com.sdkj.security.oauth.entity.AjaxResult;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.Map;

@RestController
@RequestMapping("/oauth")
public class AuthController implements InitializingBean {

    //令牌请求的端点
    @Autowired
    private TokenEndpoint tokenEndpoint;

    @Autowired
    private CheckTokenEndpoint checkTokenEndpoint;

    /**
     * 重写/oauth/token这个默认接口,返回的数据格式统一
     */
    @PostMapping(value = "/token")
    public AjaxResult postAccessToken(Principal principal, @RequestParam
            Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken accessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        return AjaxResult.success(accessToken);
    }

    /**
     * 重写/oauth/check_token这个默认接口,用于校验令牌,返回的数据格式统一
     */
    @PostMapping(value = "/check_token")
    public AjaxResult checkToken(@RequestParam("token") String value)  {
        Map<String, ?> map = checkTokenEndpoint.checkToken(value);
        return AjaxResult.success(map);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javachen__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值