根据访问令牌获取用户登录信息
This commit is contained in:
1 parent
65f21be0db
commit
baac7a6cc6
2 files changed
+31
No files matched your search
@@ -27,10 +27,12 @@ import com.pig4cloud.pigx.auth.service.PigxTokenDealServiceImpl;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.AuthorizationRequest;
|
||||
import org.springframework.security.oauth2.provider.ClientDetails;
|
||||
@@ -140,4 +142,14 @@ public class PigxTokenEndpoint {
|
||||
return dealService.queryToken(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据访问令牌获取用户信息
|
||||
* @param accessToken 访问令牌
|
||||
* @return 登录用户信息
|
||||
*/
|
||||
@GetMapping("/user")
|
||||
public ResponseEntity<PigxUser> userInfo(@RequestParam("access_token") String accessToken){
|
||||
return dealService.getUserInfo(accessToken);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -18,9 +19,12 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.event.LogoutSuccessEvent;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -150,4 +154,19 @@ public class PigxTokenDealServiceImpl {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据访问令牌获取用户信息
|
||||
* @param accessToken 访问令牌
|
||||
* @return 登录用户信息
|
||||
*/
|
||||
public ResponseEntity<PigxUser> getUserInfo(String accessToken) {
|
||||
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(accessToken);
|
||||
if (oAuth2AccessToken == null || StrUtil.isBlank(oAuth2AccessToken.getValue()) || oAuth2AccessToken.isExpired()) {
|
||||
throw new InvalidRequestException("The access token is invalid or expired");
|
||||
}
|
||||
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(oAuth2AccessToken);
|
||||
PigxUser userInfo = (PigxUser) auth2Authentication.getUserAuthentication().getPrincipal();
|
||||
return new ResponseEntity<PigxUser>(userInfo, HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user