feat: 抽取通用的资源服务器配置
This commit is contained in:
+2
@@ -9,6 +9,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal;
|
||||
import org.springframework.security.web.server.WebFilterExchange;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -26,6 +27,7 @@ import java.util.Map;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Component
|
||||
public class Oauth2ServerAuthSuccessHandler implements ServerAuthenticationSuccessHandler {
|
||||
@Override
|
||||
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
||||
|
||||
+3
-73
@@ -1,24 +1,8 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import com.central.gateway.auth.*;
|
||||
import com.central.oauth2.common.component.CustomReactiveAuthorizationServiceIntrospector;
|
||||
import com.central.oauth2.common.component.CustomServerBearerTokenAuthConverter;
|
||||
import com.central.oauth2.common.config.BaseSecurityConfig;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import com.central.oauth2.common.config.DefaultWebFluxResourceServerConf;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenReactiveAuthenticationManager;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
|
||||
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationEntryPointFailureHandler;
|
||||
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
|
||||
|
||||
/**
|
||||
* 资源服务器
|
||||
@@ -30,60 +14,6 @@ import org.springframework.security.web.server.authorization.ServerAccessDeniedH
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebFluxSecurity
|
||||
@Slf4j
|
||||
@Import(BaseSecurityConfig.class)
|
||||
public class ResourceServerConfiguration {
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http
|
||||
, SecurityProperties securityProperties, PermissionAuthManager permissionAuthManager
|
||||
, ServerAuthenticationEntryPoint authenticationEntryPoint, ServerAccessDeniedHandler accessDeniedHandler) {
|
||||
http.csrf(ServerHttpSecurity.CsrfSpec::disable)
|
||||
.cors(ServerHttpSecurity.CorsSpec::disable)
|
||||
.headers(hSpec -> hSpec.frameOptions(ServerHttpSecurity.HeaderSpec.FrameOptionsSpec::disable))
|
||||
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
|
||||
.exceptionHandling(e -> {
|
||||
e.authenticationEntryPoint(authenticationEntryPoint)
|
||||
.accessDeniedHandler(accessDeniedHandler);
|
||||
});
|
||||
public class ResourceServerConfiguration extends DefaultWebFluxResourceServerConf {
|
||||
|
||||
http.authorizeExchange(exchange -> {
|
||||
if (securityProperties.getAuth().getHttpUrls().length > 0) {
|
||||
exchange.pathMatchers(securityProperties.getAuth().getHttpUrls()).authenticated();
|
||||
}
|
||||
if (securityProperties.getIgnore().getUrls().length > 0) {
|
||||
exchange.pathMatchers(securityProperties.getIgnore().getUrls()).permitAll();
|
||||
}
|
||||
exchange.pathMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyExchange().access(permissionAuthManager);
|
||||
});
|
||||
|
||||
http.addFilterAt(this.getAuthWebFilter(authenticationEntryPoint, securityProperties)
|
||||
, SecurityWebFiltersOrder.AUTHENTICATION);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
private AuthenticationWebFilter getAuthWebFilter(ServerAuthenticationEntryPoint authenticationEntryPoint
|
||||
, SecurityProperties securityProperties) {
|
||||
OpaqueTokenReactiveAuthenticationManager authenticationManager =
|
||||
new OpaqueTokenReactiveAuthenticationManager(
|
||||
new CustomReactiveAuthorizationServiceIntrospector());
|
||||
CustomServerBearerTokenAuthConverter authenticationConverter =
|
||||
new CustomServerBearerTokenAuthConverter(securityProperties);
|
||||
authenticationConverter.setAllowUriQueryParameter(true);
|
||||
|
||||
AuthenticationWebFilter oauth2 = new AuthenticationWebFilter(authenticationManager);
|
||||
oauth2.setServerAuthenticationConverter(authenticationConverter);
|
||||
oauth2.setAuthenticationFailureHandler(getFailureHandler(authenticationEntryPoint));
|
||||
oauth2.setAuthenticationSuccessHandler(new Oauth2ServerAuthSuccessHandler());
|
||||
return oauth2;
|
||||
}
|
||||
|
||||
private ServerAuthenticationEntryPointFailureHandler getFailureHandler(ServerAuthenticationEntryPoint authenticationEntryPoint) {
|
||||
ServerAuthenticationEntryPointFailureHandler failureHandler =
|
||||
new ServerAuthenticationEntryPointFailureHandler(authenticationEntryPoint);
|
||||
failureHandler.setRethrowAuthenticationServiceException(false);
|
||||
return failureHandler;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user