♻️ 重构代码
This commit is contained in:
commit
4521d58db2
28 files changed
+465
-143
No files matched your search
+2
-26
@@ -2,15 +2,13 @@ package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.*;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
|
||||
@@ -38,28 +36,6 @@ public class PigxResourceServerTokenRelayAutoConfiguration {
|
||||
return new AccessTokenContextRelay(context);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ResourceServerTokenRelayRegistrationAutoConfiguration implements WebMvcConfigurer {
|
||||
@Autowired
|
||||
AccessTokenContextRelay accessTokenContextRelay;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(
|
||||
new HandlerInterceptorAdapter() {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request,
|
||||
HttpServletResponse response, Object handler) throws Exception {
|
||||
accessTokenContextRelay.copyToken();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
|
||||
+5
-3
@@ -21,7 +21,9 @@ package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
@@ -37,10 +39,10 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
|
||||
@AllArgsConstructor
|
||||
@ConditionalOnProperty("security.oauth2.client.client-id")
|
||||
public class PigxFeignClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext,
|
||||
OAuth2ProtectedResourceDetails resource) {
|
||||
return new PigxFeignClientInterceptor(oAuth2ClientContext, resource);
|
||||
OAuth2ProtectedResourceDetails resource,
|
||||
AccessTokenContextRelay accessTokenContextRelay) {
|
||||
return new PigxFeignClientInterceptor(oAuth2ClientContext, resource,accessTokenContextRelay);
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -17,12 +17,17 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import feign.RequestTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.security.oauth2.client.AccessTokenContextRelay;
|
||||
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/8/13
|
||||
@@ -31,32 +36,39 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
|
||||
@Slf4j
|
||||
public class PigxFeignClientInterceptor extends OAuth2FeignRequestInterceptor {
|
||||
private final OAuth2ClientContext oAuth2ClientContext;
|
||||
private final AccessTokenContextRelay accessTokenContextRelay;
|
||||
|
||||
/**
|
||||
* Default constructor which uses the provided OAuth2ClientContext and Bearer tokens
|
||||
* within Authorization header
|
||||
*
|
||||
* @param oAuth2ClientContext provided context
|
||||
* @param resource type of resource to be accessed
|
||||
* @param oAuth2ClientContext provided context
|
||||
* @param resource type of resource to be accessed
|
||||
* @param accessTokenContextRelay
|
||||
*/
|
||||
public PigxFeignClientInterceptor(OAuth2ClientContext oAuth2ClientContext
|
||||
, OAuth2ProtectedResourceDetails resource) {
|
||||
, OAuth2ProtectedResourceDetails resource, AccessTokenContextRelay accessTokenContextRelay) {
|
||||
super(oAuth2ClientContext, resource);
|
||||
this.oAuth2ClientContext = oAuth2ClientContext;
|
||||
this.accessTokenContextRelay = accessTokenContextRelay;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a template with the header of provided name and extracted extract
|
||||
* 1. 如果使用 非web 请求,header 区别
|
||||
* 2. 根据authentication 还原请求token
|
||||
*
|
||||
* @param template
|
||||
*/
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
if (template.headers().isEmpty()) {
|
||||
Collection<String> fromHeader = template.headers().get(SecurityConstants.FROM);
|
||||
if (CollUtil.isNotEmpty(fromHeader) && fromHeader.contains(SecurityConstants.FROM_IN)) {
|
||||
return;
|
||||
}
|
||||
|
||||
accessTokenContextRelay.copyToken();
|
||||
if (oAuth2ClientContext != null
|
||||
&& oAuth2ClientContext.getAccessToken() != null) {
|
||||
super.apply(template);
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.handler;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/10/8
|
||||
* 认证失败事件处理器
|
||||
*/
|
||||
public abstract class AuthenticationFailureEvenHandler implements ApplicationListener<AbstractAuthenticationFailureEvent> {
|
||||
|
||||
/**
|
||||
* Handle an application event.
|
||||
*
|
||||
* @param event the event to respond to
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
|
||||
AuthenticationException authenticationException = event.getException();
|
||||
Authentication authentication = (Authentication) event.getSource();
|
||||
|
||||
handle(authenticationException, authentication);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录成功方法
|
||||
* <p>
|
||||
*
|
||||
* @param authenticationException 登录的authentication 对象
|
||||
* @param authentication 登录的authenticationException 对象
|
||||
*/
|
||||
public abstract void handle(AuthenticationException authenticationException, Authentication authentication);
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.handler;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/10/8
|
||||
* 认证成功事件处理器
|
||||
*/
|
||||
public abstract class AuthenticationSuccessEventHandler implements ApplicationListener<AuthenticationSuccessEvent> {
|
||||
/**
|
||||
* Handle an application event.
|
||||
*
|
||||
* @param event the event to respond to
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(AuthenticationSuccessEvent event) {
|
||||
Authentication authentication = (Authentication) event.getSource();
|
||||
handle(authentication);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录成功方法
|
||||
* <p>
|
||||
* 获取到登录的authentication 对象
|
||||
*
|
||||
* @param authentication 登录对象
|
||||
*/
|
||||
public abstract void handle(Authentication authentication);
|
||||
}
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
package com.pig4cloud.pigx.common.security.mobile;
|
||||
package com.pig4cloud.pigx.common.security.handler;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
+1
@@ -72,6 +72,7 @@ public class PigxUser extends User {
|
||||
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
|
||||
this.id = id;
|
||||
this.deptId = deptId;
|
||||
this.deptId = deptId;
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user