diff --git a/auth-center/src/main/resources/application.yml b/auth-center/src/main/resources/application.yml index edeb523f..bb18c8b0 100644 --- a/auth-center/src/main/resources/application.yml +++ b/auth-center/src/main/resources/application.yml @@ -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" diff --git a/exercise-mis/src/main/resources/application.yml b/exercise-mis/src/main/resources/application.yml index b588276c..5de98c29 100644 --- a/exercise-mis/src/main/resources/application.yml +++ b/exercise-mis/src/main/resources/application.yml @@ -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" diff --git a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/IgnoredUrl.java b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/IgnoredUrl.java new file mode 100644 index 00000000..f2153b81 --- /dev/null +++ b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/IgnoredUrl.java @@ -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; + +/** + *

IgnoredUrl

+ *

+ * 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(); +} diff --git a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MafConfiguration.java b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MafConfiguration.java new file mode 100644 index 00000000..af633d8b --- /dev/null +++ b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MafConfiguration.java @@ -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; + +/** + *

CustomConfiguration

+ *

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.

+ * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com + * @date 2019-03-23 14:24 + **/ +@Data +@Validated +@Component +@ConfigurationProperties(prefix = "maf.configuration") +public class MafConfiguration { + /** + *

The username of super user who has no restriction to access any system's resources.

+ *

ATTENTION: The value of username of super user must be equal to the value that is + * persistent in database.

+ */ + @NotBlank + private String superUser = "admin"; + /** + * Ignore URLs + */ + @Valid + private IgnoredUrl ignoredUrl; + /** + *

Web security feature switch. Default is false.

+ * true - disable web security; false - enable web security. + */ + @NotNull + private Boolean webSecurityDisabled = false; + /** + * Web request log switch. Default is false. + *

+ * 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(); + 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]); + } +} diff --git a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MuscleAndFitnessServerAutoConfiguration.java b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MuscleAndFitnessServerAutoConfiguration.java index 08248a88..a6fb7435 100644 --- a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MuscleAndFitnessServerAutoConfiguration.java +++ b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/configuration/MuscleAndFitnessServerAutoConfiguration.java @@ -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; @@ -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() { @@ -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 diff --git a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/RequestFilter.java b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/AccessLogFilter.java similarity index 63% rename from muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/RequestFilter.java rename to muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/AccessLogFilter.java index 7edac9da..958374ab 100644 --- a/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/RequestFilter.java +++ b/muscle-and-fitness-server-spring-boot-starter/src/main/java/com/jmsoftware/maf/muscleandfitnessserverspringbootstarter/filter/AccessLogFilter.java @@ -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; @@ -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); diff --git a/muscle-and-fitness-server-spring-boot-starter/src/main/resources/application.yml b/muscle-and-fitness-server-spring-boot-starter/src/main/resources/application.yml new file mode 100644 index 00000000..e69de29b diff --git a/muscle-mis/src/main/resources/application.yml b/muscle-mis/src/main/resources/application.yml index db195892..b5d8ab75 100644 --- a/muscle-mis/src/main/resources/application.yml +++ b/muscle-mis/src/main/resources/application.yml @@ -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" diff --git a/service-registry/src/main/resources/application.yml b/service-registry/src/main/resources/application.yml index 2f916af1..f92a0545 100644 --- a/service-registry/src/main/resources/application.yml +++ b/service-registry/src/main/resources/application.yml @@ -47,3 +47,10 @@ project: developer-name: @developerName@ developer-email: @developerEmail@ developer-url: @developerUrl@ + +maf: + configuration: + ignored-url: + pattern: + - "/actuator/**" + super-user: "admin" diff --git a/spring-boot-admin/src/main/resources/application.yml b/spring-boot-admin/src/main/resources/application.yml index 592ae6b0..7a5e7d69 100644 --- a/spring-boot-admin/src/main/resources/application.yml +++ b/spring-boot-admin/src/main/resources/application.yml @@ -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"