Spring Oauth2 重寫(xiě) 登錄返回結(jié)果

項(xiàng)目中安全框架用的是 spring security + oauth2
返回結(jié)果大致是這樣,不太符合我們的標(biāo)準(zhǔn)返回所以重新修改一下,加上其他邏輯

{
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"token_type": "bearer",
"refresh_token": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"expires_in": 258611,
"scope": "read"
}

討一個(gè)取巧的方式,使用restTemplate請(qǐng)求/oauth/token接口,獲取的結(jié)果可以自由包裝

private OAuth2AccessToken httpLogin(LoginREQ login) {

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.set("username", login.getUsername());
        paramsMap.set("password", login.getPassword());
        paramsMap.set("client_id", !StringUtils.isNullOrEmpty(login.getClient_id()) ? login.getClient_id() : "browser");
        paramsMap.set("grant_type", !StringUtils.isNullOrEmpty(login.getGrant_type()) ? login.getGrant_type() : "password");
        // 構(gòu)造頭部信息
        HttpHeaders headers = new HttpHeaders();
        // 設(shè)置類型 "application/json;charset=UTF-8"
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> req = new HttpEntity(paramsMap, headers);
        //HTTP 請(qǐng)求登錄接口
        String requestUrl = "http://" + applicationName + "/oauth/token";
        try {
            OAuth2AccessToken result = restTemplate.postForObject(requestUrl, req, OAuth2AccessToken.class, paramsMap);
            if (result != null) {
                return result;
            }
        } catch (Exception e) {
            log.error("登錄失敗");
            e.printStackTrace();
            throw ExceptionUtils.commonException("登錄失敗");
        }
        return null;
    }

===========================================結(jié)束,下面是廢棄的,沒(méi)走通==============================

Controller方法

    @Autowired
    private TokenEndpoint tokenEndPoint;
/**
     * 功能等同于 /oauth/token 獲取的token
     * @return
     * @throws HttpRequestMethodNotSupportedException
     */
    @ApiOperation("登錄 功能等同于 /oauth/token 獲取的token,參數(shù)一致")
    @PostMapping("/login")
    public Result login(@Validated LoginREQ login) throws HttpRequestMethodNotSupportedException {
        Map<String, String> parameters = new HashMap<String, String>(8);
        parameters.put("client_id", !StringUtils.isNullOrEmpty(login.getClient_id())?login.getClient_id():"browser");
        parameters.put("grant_type", !StringUtils.isNullOrEmpty(login.getGrant_type())?login.getGrant_type():"password");
        parameters.put("username", login.getUsername());
        parameters.put("password", login.getPassword());

        ResponseEntity<OAuth2AccessToken> result = tokenEndPoint.postAccessToken(UserUtils.getAuthentication(), parameters);
        //其他邏輯
        //其他邏輯
        //其他邏輯

return 自定義返回 Result ;

請(qǐng)求參數(shù)類

@Api("登錄參數(shù)")
@Accessors(chain = true)
@Data
public class LoginREQ {
    @NotBlank(message = "用戶名不能為空")
    @ApiModelProperty("用戶名")
    private String username;
    @NotBlank(message = "密碼不能為空")
    @ApiModelProperty("密碼")
    private String password;
    @ApiModelProperty("登錄模式,默認(rèn) password")
    private String grant_type="password";
    @ApiModelProperty("客戶端id,默認(rèn) browser")
    private String client_id="browser";
}

最關(guān)鍵在

tokenEndPoint. postAccessToken (UserUtils.getAuthentication(), parameters);
這個(gè)方法,這個(gè)將相當(dāng)于內(nèi)部RPC調(diào)用了一次登錄接口,然后就可以根據(jù)返回?cái)?shù)據(jù)就可以自由修改了

下面是org.springframework.security.oauth2.provider.endpoint.TokenEndpoint中部分代碼;


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容