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 {
}
}