From c9334902f2536d9a82e13e53896d5f9a4902b5fb Mon Sep 17 00:00:00 2001 From: wangbing Date: Mon, 6 Jan 2020 13:58:40 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: e1460d423cafe8d1dc363d514938830013853d15 --- .../java/config/FreeMarkerConfig.java | 9 +++++---- .../java/config/SecurityConfig.java | 20 ++++++++++++------- .../SpringBoot/java/config/WebMvcConfig.java | 12 ++++++----- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/resources/modules/SpringBoot/java/config/FreeMarkerConfig.java b/src/main/resources/modules/SpringBoot/java/config/FreeMarkerConfig.java index ae09e4b0..7b380342 100644 --- a/src/main/resources/modules/SpringBoot/java/config/FreeMarkerConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/FreeMarkerConfig.java @@ -5,8 +5,8 @@ import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateModelException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; import org.springframework.validation.support.BindingAwareModelMap; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -30,18 +30,19 @@ import static ${basePackage}.config.ActionConfig.SCREEN_PREFIX; @Configuration public class FreeMarkerConfig { - @Autowired - private Environment environment; @Autowired private FreeMarkerViewResolver viewResolver; @Autowired private freemarker.template.Configuration configuration; + @Value("${r'${server.servlet.context-path}'}") + private String context; + private String suffix = ".ftl"; @PostConstruct public void setSharedVariable() throws TemplateModelException { // 全局共享变量、函数 - configuration.setSharedVariable("context", environment.getProperty("server.servlet.context-path", "/")); + configuration.setSharedVariable("context", context); configuration.setSharedVariable("screenHolder", new ScreenHolder()); configuration.setSharedVariable("controlHolder", new ControlHolder()); configuration.setSharedVariable("UrlUtil", new UrlUtil()); diff --git a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java index 6845caf0..23f0acb5 100644 --- a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java @@ -1,9 +1,8 @@ package ${basePackage}.config; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; @@ -34,12 +33,19 @@ import java.util.regex.Pattern; @Configuration @EnableGlobalMethodSecurity(securedEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { - @Autowired - private Environment environment; + + @Value("${r'${web.url.auth.included}'}") + private String[] included; + @Value("${r'${web.url.auth.excluded}'}") + private String[] excluded; + @Value("${r'${spring.mvc.static-path-pattern}'}") + private String[] staticPath; + @Value("${r'${web.login.page}'}") + private String loginPage; @Override public void configure(WebSecurity web) throws Exception { - web.ignoring().mvcMatchers(environment.getProperty("spring.mvc.static-path-pattern", "")); + web.ignoring().mvcMatchers(staticPath); } @Override @@ -48,8 +54,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .and() .addFilterBefore(new TokenFilter(), FilterSecurityInterceptor.class)// 过滤器用于处理Token .authorizeRequests() - .antMatchers(environment.getProperty("web.url.auth.excluded")).permitAll()// 放行排除的URL - .antMatchers(environment.getProperty("web.url.auth.included")).access("@Authorization.hasPermission(request,authentication)")// 需要权限的URL + .antMatchers(excluded).permitAll()// 放行排除的URL + .antMatchers(included).access("@Authorization.hasPermission(request,authentication)")// 需要权限的URL .and().cors() .and().headers().frameOptions().disable() .and().csrf().disable(); diff --git a/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java b/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java index 007305cc..9f629bce 100644 --- a/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java @@ -3,9 +3,8 @@ package ${basePackage}.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.AntPathMatcher; @@ -21,8 +20,11 @@ import java.util.List; @Configuration public class WebMvcConfig implements WebMvcConfigurer { - @Autowired - private Environment environment; + + @Value("${r'${web.welcome.page}'}") + private String homePage; + @Value("${r'${spring.mvc.static-path-pattern}'}") + private String[] staticPath; /** * 设置忽略路径大小写 @@ -55,7 +57,7 @@ public class WebMvcConfig implements WebMvcConfigurer { } } - }).addPathPatterns("/**").excludePathPatterns(environment.getProperty("spring.mvc.static-path-pattern")); + }).addPathPatterns("/**").excludePathPatterns(staticPath); } /**