✨ Introducing new features. #3860 资源服务器校验令牌支持用户状态后即时生效

This commit is contained in:
lbw committed 2022-04-28 13:43:04 +08:00
1 parent 5abb4c6019
commit c3d55c3fe2
3 files changed
+27 -4

No files matched your search

@@ -4,14 +4,17 @@ import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.security.exception.UnauthorizedException;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import com.pig4cloud.pigx.common.security.util.PigxSecurityMessageSourceUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AccountStatusException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsChecker;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
@@ -36,6 +39,8 @@ public class PigxLocalResourceServerTokenServices implements ResourceServerToken
private final TokenStore tokenStore;
private final UserDetailsChecker preAuthenticationChecks;
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
throws AuthenticationException, InvalidTokenException {
@@ -68,11 +73,19 @@ public class PigxLocalResourceServerTokenServices implements ResourceServerToken
UserDetails userDetails;
try {
userDetails = optional.get().loadUserByUser(pigUser);
preAuthenticationChecks.check(userDetails);
}
catch (UsernameNotFoundException notFoundException) {
throw new UnauthorizedException(String.format("%s username not found", pigUser.getUsername()),
notFoundException);
}
catch (AccountStatusException accountStatusException) {
throw new UnauthorizedException(
PigxSecurityMessageSourceUtil.getAccessor().getMessage(
"AbstractUserDetailsAuthenticationProvider.locked", accountStatusException.getMessage()),
accountStatusException);
}
Authentication userAuthentication = new UsernamePasswordAuthenticationToken(userDetails, "N/A",
userDetails.getAuthorities());
OAuth2Authentication authentication = new OAuth2Authentication(oAuth2Request, userAuthentication);
@@ -7,19 +7,20 @@ import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.core.userdetails.UserDetailsChecker;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.TokenStore;
/**
* @author lengleng
* @date 2020/9/29
* @date 2022-04-28
* <p>
* redis token store 自动配置
* 资源服务器 认证服务器 公用配置
*/
@RequiredArgsConstructor
@Configuration(proxyBeanMethods = false)
public class PigxTokenStoreAutoConfiguration {
public class PigxSecurityCommonAutoConfiguration {
private final KeyStrResolver resolver;
@@ -39,4 +40,13 @@ public class PigxTokenStoreAutoConfiguration {
return tokenStore;
}
/**
* 认证状态检查
* @return UserDetailsChecker
*/
@Bean
public UserDetailsChecker preAuthenticationChecks() {
return new PigxPreAuthenticationChecks();
}
}
@@ -4,7 +4,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.security.service.PigxDefaultUserDetailsServiceImpl,\
com.pig4cloud.pigx.common.security.service.PigxAppUserDetailsServiceImpl,\
com.pig4cloud.pigx.common.security.service.PigxClientDetailsServiceImpl,\
com.pig4cloud.pigx.common.security.component.PigxTokenStoreAutoConfiguration,\
com.pig4cloud.pigx.common.security.component.PigxSecurityCommonAutoConfiguration,\
com.pig4cloud.pigx.common.security.component.PigxSecurityMessageSourceConfiguration,\
com.pig4cloud.pigx.common.security.component.PigxTokenStoreCleanSchedule,\
com.pig4cloud.pigx.common.security.listener.AuthenticationFailureEventListener,\