Skip to content

Commit

Permalink
perf($ReactiveStarter): abstract access log filter
Browse files Browse the repository at this point in the history
abstract access log filter

BREAKING CHANGE: abstract access log filter
  • Loading branch information
Johnny Miller (锺俊) committed Dec 22, 2020
1 parent 7e8a816 commit d29ba21
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jmsoftware.maf.gateway.security;

import com.jmsoftware.maf.gateway.universal.util.ResponseUtil;
import com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.gateway.security.configuration.JwtConfiguration;
import com.jmsoftware.maf.gateway.universal.configuration.CustomConfiguration;
import com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.configuration.MafConfiguration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
Expand All @@ -27,7 +27,7 @@
@Component
@RequiredArgsConstructor
public class JwtReactiveServerSecurityContextRepository implements ServerSecurityContextRepository {
private final CustomConfiguration customConfiguration;
private final MafConfiguration mafConfiguration;
private final ReactiveAuthenticationManager authenticationManager;
private final AntPathMatcher antPathMatcher = new AntPathMatcher();

Expand All @@ -40,7 +40,7 @@ public Mono<Void> save(ServerWebExchange exchange, SecurityContext context) {
public Mono<SecurityContext> load(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
// Ignore allowed URL
for (String ignoredUrl : customConfiguration.flattenIgnoredUrls()) {
for (String ignoredUrl : mafConfiguration.flattenIgnoredUrls()) {
if (antPathMatcher.match(ignoredUrl, request.getURI().getPath())) {
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jmsoftware.maf.gateway.security;

import com.jmsoftware.maf.gateway.universal.util.ResponseUtil;
import com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmsoftware.maf.gateway.security;

import com.jmsoftware.maf.gateway.security.filter.RequestFilter;
import com.jmsoftware.maf.gateway.universal.configuration.CustomConfiguration;
import com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.filter.AccessLogFilter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -29,13 +29,13 @@
@EnableWebFluxSecurity
@RequiredArgsConstructor
public class WebFluxSecurityConfiguration {
private final CustomConfiguration customConfiguration;
private final MafConfiguration mafConfiguration;
private final JwtReactiveAuthenticationManager reactiveAuthenticationManager;
private final RbacReactiveAuthorizationManager reactiveAuthorizationManager;
private final JwtReactiveServerSecurityContextRepository securityContextRepository;
private final ServerAuthenticationEntryPointImpl serverAuthenticationEntryPoint;
private final GatewayServerAccessDeniedHandler serverAccessDeniedHandler;
private final RequestFilter requestFilter;
private final AccessLogFilter accessLogFilter;

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
Expand All @@ -46,12 +46,12 @@ SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
.authenticationEntryPoint(serverAuthenticationEntryPoint)
.accessDeniedHandler(serverAccessDeniedHandler)
.and()
.addFilterBefore(requestFilter, SecurityWebFiltersOrder.AUTHENTICATION)
.addFilterBefore(accessLogFilter, SecurityWebFiltersOrder.AUTHENTICATION)
// Authentication
.authenticationManager(reactiveAuthenticationManager)
.securityContextRepository(securityContextRepository)
.authorizeExchange()
.pathMatchers(customConfiguration.flattenIgnoredUrls()).permitAll()
.pathMatchers(mafConfiguration.flattenIgnoredUrls()).permitAll()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
// Authorization
.anyExchange().access(reactiveAuthorizationManager)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.gateway.universal;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.gateway.universal.util;
28 changes: 0 additions & 28 deletions gateway/src/main/resources/application-development-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,3 @@ spring:
max-idle: 10
max-wait: -1ms
min-idle: 0

custom:
configuration:
super-user: "admin"
# Make `web-security-disabled` equal to true to disable web security. We suggest you do not turn off web security
# feature unless development environment.
web-security-disabled: false
# Disable web request information log
web-request-log-disabled: false
ignored-request:
post:
- "/authentication/**"
get:
- "/favicon.ico"
- "/auth/check-username-uniqueness"
- "/auth/check-email-uniqueness"
- "/auth/validate-username/**"
- "/user/get-avatar"
- "/common/get-jwt"
pattern:
- "/static/**"
- "/actuator/**"
- "/druid/**"
- "/swagger-resources/**"
- "/v2/api-docs/**"
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
23 changes: 23 additions & 0 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,26 @@ project:
developer-name: @developerName@
developer-email: @developerEmail@
developer-url: @developerUrl@


maf:
configuration:
ignored-url:
post:
- "/authentication/**"
get:
- "/favicon.ico"
- "/auth/check-username-uniqueness"
- "/auth/check-email-uniqueness"
- "/auth/validate-username/**"
- "/user/get-avatar"
- "/common/get-jwt"
pattern:
- "/static/**"
- "/actuator/**"
- "/druid/**"
- "/swagger-resources/**"
- "/v2/api-docs/**"
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.configuration;

import com.google.common.collect.Lists;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
* <h1>IgnoredUrl</h1>
* <p>
* Ignored URL configuration.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com
* @date 5/2/20 11:41 PM
**/
@Data
public class IgnoredUrl {
private interface Constant {
String URL_REGEXP = "^(/.+)+$";
}

/**
* Ignored URL pattern.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> pattern = Lists.newArrayList();
/**
* Ignored GET request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> get = Lists.newArrayList();
/**
* Ignored POST request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> post = Lists.newArrayList();
/**
* Ignored DELETE request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> delete = Lists.newArrayList();
/**
* Ignored PUT request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> put = Lists.newArrayList();
/**
* Ignored HEAD request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> head = Lists.newArrayList();
/**
* Ignored PATCH request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> patch = Lists.newArrayList();
/**
* Ignored OPTIONS request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> options = Lists.newArrayList();
/**
* Ignored TRACE request.
*/
@Valid
private List<@Pattern(regexp = Constant.URL_REGEXP) String> trace = Lists.newArrayList();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.jmsoftware.maf.gateway.universal.configuration;
package com.jmsoftware.maf.muscleandfitnessserverreactivespringbootstarter.configuration;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import lombok.val;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;

/**
Expand All @@ -19,29 +23,32 @@
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "custom.configuration")
public class CustomConfiguration {
@ConfigurationProperties(prefix = "maf.configuration")
public class MafConfiguration {
/**
* <p>The username of super user who has no restriction to access any system&#39;s resources.</p>
* <p><strong>ATTENTION</strong>: The value of username of super user must be equal to the value that is
* persistent in database.</p>
*/
@NotBlank
private String superUser;
private String superUser = "admin";
/**
* Ignore URLs
*/
private IgnoredRequest ignoredRequest;
@Valid
private IgnoredUrl ignoredUrl;
/**
* <p>Web security feature switch. Default is false.</p>
* true - disable web security; false - enable web security.
*/
@NotNull
private Boolean webSecurityDisabled = false;
/**
* Web request log switch. Default is false.
* <p>
* true - disable web request log; false - enable web request log.
*/
@NotNull
private Boolean webRequestLogDisabled = false;

/**
Expand All @@ -50,17 +57,19 @@ public class CustomConfiguration {
* @return the string [ ]
*/
public String[] flattenIgnoredUrls() {
final var ignoredRequests = this.getIgnoredRequest();
final var flattenIgnoredUrls = new ArrayList<String>();
flattenIgnoredUrls.addAll(ignoredRequests.getGet());
flattenIgnoredUrls.addAll(ignoredRequests.getPost());
flattenIgnoredUrls.addAll(ignoredRequests.getDelete());
flattenIgnoredUrls.addAll(ignoredRequests.getPut());
flattenIgnoredUrls.addAll(ignoredRequests.getHead());
flattenIgnoredUrls.addAll(ignoredRequests.getPatch());
flattenIgnoredUrls.addAll(ignoredRequests.getOptions());
flattenIgnoredUrls.addAll(ignoredRequests.getTrace());
flattenIgnoredUrls.addAll(ignoredRequests.getPattern());
if (ObjectUtil.isNull(ignoredUrl)) {
return new String[0];
}
val flattenIgnoredUrls = new ArrayList<String>();
flattenIgnoredUrls.addAll(ignoredUrl.getGet());
flattenIgnoredUrls.addAll(ignoredUrl.getPost());
flattenIgnoredUrls.addAll(ignoredUrl.getDelete());
flattenIgnoredUrls.addAll(ignoredUrl.getPut());
flattenIgnoredUrls.addAll(ignoredUrl.getHead());
flattenIgnoredUrls.addAll(ignoredUrl.getPatch());
flattenIgnoredUrls.addAll(ignoredUrl.getOptions());
flattenIgnoredUrls.addAll(ignoredUrl.getTrace());
flattenIgnoredUrls.addAll(ignoredUrl.getPattern());
return flattenIgnoredUrls.toArray(new String[0]);
}
}
Loading

0 comments on commit d29ba21

Please sign in to comment.