♻️ 重构代码。 重构token TokenEnhancer
This commit is contained in:
1 parent
b7b414b3f2
commit
75a9e98c4b
9 files changed
+77
-39
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-09-17
|
||||
* <p>
|
||||
* token增强,客户端模式不增强。
|
||||
*/
|
||||
public class PigxTokenEnhancer implements TokenEnhancer {
|
||||
/**
|
||||
* Provides an opportunity for customization of an access token (e.g. through its additional information map) during
|
||||
* the process of creating a new token for use by a client.
|
||||
*
|
||||
* @param accessToken the current access token with its expiration and refresh token
|
||||
* @param authentication the current authentication including client and user details
|
||||
* @return a new token enhanced with additional information
|
||||
*/
|
||||
@Override
|
||||
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
|
||||
if (SecurityConstants.CLIENT_CREDENTIALS
|
||||
.equals(authentication.getOAuth2Request().getGrantType())) {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
final Map<String, Object> additionalInfo = new HashMap<>(8);
|
||||
PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
|
||||
additionalInfo.put(SecurityConstants.DETAILS_USER_ID, pigxUser.getId());
|
||||
additionalInfo.put(SecurityConstants.DETAILS_USERNAME, pigxUser.getUsername());
|
||||
additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, pigxUser.getDeptId());
|
||||
additionalInfo.put(SecurityConstants.DETAILS_TENANT_ID, pigxUser.getTenantId());
|
||||
additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PIGX_LICENSE);
|
||||
additionalInfo.put(SecurityConstants.ACTIVE, Boolean.TRUE);
|
||||
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
|
||||
return accessToken;
|
||||
}
|
||||
}
|
||||
-2
@@ -35,7 +35,6 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -48,7 +47,6 @@ import java.util.Set;
|
||||
* @author lengleng
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
private final RemoteUserService remoteUserService;
|
||||
|
||||
-2
@@ -50,8 +50,6 @@ public class SecurityUtils {
|
||||
* @param authentication
|
||||
* @return PigxUser
|
||||
* <p>
|
||||
* 获取当前用户的全部信息 EnablePigxResourceServer true
|
||||
* 获取当前用户的用户名 EnablePigxResourceServer false
|
||||
*/
|
||||
public PigxUser getUser(Authentication authentication) {
|
||||
Object principal = authentication.getPrincipal();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.openfeign.PigxHystrixFeignTargeterConfiguration,\
|
||||
com.pig4cloud.pigx.common.security.component.GlobalExceptionHandlerResolver,\
|
||||
com.pig4cloud.pigx.common.security.component.PigxTokenEnhancer,\
|
||||
com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl
|
||||
Reference in new issue
Block a user