Skip to content

Commit

Permalink
perf($Starter): reduce unnecessary access log
Browse files Browse the repository at this point in the history
reduce unnecessary access log

BREAKING CHANGE: reduce unnecessary access log
  • Loading branch information
Johnny Miller (锺俊) committed Dec 22, 2020
1 parent bd1e5b3 commit 7e8a816
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 5 deletions.
13 changes: 13 additions & 0 deletions auth-center/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ jwt:
# an hour
ttl: 3600000
ttl-for-remember-me: 604800000

maf:
configuration:
ignored-url:
pattern:
- "/static/**"
- "/actuator/**"
- "/druid/**"
- "/swagger-resources/**"
- "/v2/api-docs/**"
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
13 changes: 13 additions & 0 deletions exercise-mis/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,16 @@ project:
developer-name: @developerName@
developer-email: @developerEmail@
developer-url: @developerUrl@

maf:
configuration:
ignored-url:
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
@@ -0,0 +1,69 @@
package com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.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 = "^(/[a-z0-9]+-?[a-z0-9]*|/\\*{1,2})*(/[a-z0-9]+\\.[a-z0-9]+)?$";
}

/**
* 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
@@ -0,0 +1,75 @@
package com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.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;

/**
* <h1>CustomConfiguration</h1>
* <p>Custom configurations which are written in .yml files, containing a variety of fragmentary configs. Such as,
* Druid login info, web security switch, web log and so on.</p>
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com
* @date 2019-03-23 14:24
**/
@Data
@Validated
@Component
@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 = "admin";
/**
* Ignore URLs
*/
@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;

/**
* Flatten ignored urls string [ ].
*
* @return the string [ ]
*/
public String[] flattenIgnoredUrls() {
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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.aspect.ExceptionControllerAdvice;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.aspect.WebRequestLogAspect;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.controller.RedirectController;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.filter.RequestFilter;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.filter.AccessLogFilter;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.helper.IpHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -30,6 +30,13 @@ public void afterInitialization() {
log.debug("{} initialization is done. About to inject beans.", getClass().getSimpleName());
}

@Bean
@ConditionalOnMissingBean
public MafConfiguration mafConfiguration() {
log.debug("Initial bean: {}", MafConfiguration.class.getName());
return new MafConfiguration();
}

@Bean
@ConditionalOnMissingBean
public ExceptionControllerAdvice exceptionControllerAdvice() {
Expand All @@ -53,9 +60,9 @@ public RedirectController redirectController() {

@Bean
@ConditionalOnMissingBean
public RequestFilter requestFilter() {
log.debug("Initial bean: {}", RequestFilter.class.getName());
return new RequestFilter();
public AccessLogFilter requestFilter(MafConfiguration mafConfiguration) {
log.debug("Initial bean: {}", AccessLogFilter.class.getName());
return new AccessLogFilter(mafConfiguration);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.filter;

import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.muscleandfitnessserverspringbootstarter.util.RequestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
Expand All @@ -20,11 +23,22 @@
**/
@Slf4j
@Component
public class RequestFilter extends OncePerRequestFilter {
@RequiredArgsConstructor
public class AccessLogFilter extends OncePerRequestFilter {
private final MafConfiguration mafConfiguration;
private final AntPathMatcher antPathMatcher = new AntPathMatcher();

@Override
@SuppressWarnings("NullableProblems")
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// Ignore URL
for (String ignoredUrl : mafConfiguration.flattenIgnoredUrls()) {
if (antPathMatcher.match(ignoredUrl, request.getRequestURI())) {
filterChain.doFilter(request, response);
return;
}
}
log.info("The requester({}) requested resource. Request URL: [{}] {}", RequestUtil.getRequestIpAndPort(request),
request.getMethod(), request.getRequestURL());
filterChain.doFilter(request, response);
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions muscle-mis/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ project:
developer-name: @developerName@
developer-email: @developerEmail@
developer-url: @developerUrl@

maf:
configuration:
ignored-url:
pattern:
- "/static/**"
- "/actuator/**"
- "/druid/**"
- "/swagger-resources/**"
- "/v2/api-docs/**"
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
7 changes: 7 additions & 0 deletions service-registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ project:
developer-name: @developerName@
developer-email: @developerEmail@
developer-url: @developerUrl@

maf:
configuration:
ignored-url:
pattern:
- "/actuator/**"
super-user: "admin"
13 changes: 13 additions & 0 deletions spring-boot-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ project:
developer-name: @developerName@
developer-email: @developerEmail@
developer-url: @developerUrl@

maf:
configuration:
ignored-url:
pattern:
- "/static/**"
- "/actuator/**"
- "/druid/**"
- "/swagger-resources/**"
- "/v2/api-docs/**"
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"

0 comments on commit 7e8a816

Please sign in to comment.