From e7e5c37ea3ac7c9d7d573297ec0818dd2f77977a Mon Sep 17 00:00:00 2001 From: tamguo Date: Thu, 15 Nov 2018 16:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E5=91=98=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tamguo/config/web/WebConfig.java | 11 ++++ .../tamguo/interceptor/MemberInterceptor.java | 60 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/application.properties | 8 +-- 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tamguo-bms/src/main/java/com/tamguo/interceptor/MemberInterceptor.java diff --git a/tamguo-bms/src/main/java/com/tamguo/config/web/WebConfig.java b/tamguo-bms/src/main/java/com/tamguo/config/web/WebConfig.java index 9c46b03..27b0699 100644 --- a/tamguo-bms/src/main/java/com/tamguo/config/web/WebConfig.java +++ b/tamguo-bms/src/main/java/com/tamguo/config/web/WebConfig.java @@ -1,18 +1,29 @@ package com.tamguo.config.web; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import com.tamguo.interceptor.MemberInterceptor; + @Configuration public class WebConfig implements WebMvcConfigurer { @Value("${file.storage.path}") private String fileStoragePath; + @Autowired + private MemberInterceptor memberInterceptor; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/files/**").addResourceLocations("file:"+fileStoragePath); } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(memberInterceptor).addPathPatterns("/member/**"); + } } diff --git a/tamguo-bms/src/main/java/com/tamguo/interceptor/MemberInterceptor.java b/tamguo-bms/src/main/java/com/tamguo/interceptor/MemberInterceptor.java new file mode 100644 index 0000000..411e506 --- /dev/null +++ b/tamguo-bms/src/main/java/com/tamguo/interceptor/MemberInterceptor.java @@ -0,0 +1,60 @@ +package com.tamguo.interceptor; + +import java.net.URLEncoder; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +@Component +public class MemberInterceptor extends HandlerInterceptorAdapter{ + + /** "重定向URL"参数名称 */ + private static final String REDIRECT_URL_PARAMETER_NAME = "redirectUrl"; + + /** 默认登录URL */ + private static final String DEFAULT_LOGIN_URL = "/login.html"; + + /** 登录URL */ + private String loginUrl = DEFAULT_LOGIN_URL; + + @Value("${tiku.admin.name}") + private String tDomainName; + + /** + * 请求前处理 + * + * @param request + * HttpServletRequest + * @param response + * HttpServletResponse + * @param handler + * 处理器 + * @return 是否继续执行 + */ + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + Object currMember = request.getSession().getAttribute("currMember"); + if (currMember != null) { + return true; + } else { + String requestType = request.getHeader("X-Requested-With"); + if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) { + response.addHeader("loginStatus", "accessDenied"); + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return false; + } else { + if (request.getMethod().equalsIgnoreCase("GET")) { + String redirectUrl = request.getQueryString() != null ? request.getRequestURI() + "?" + request.getQueryString() : request.getRequestURI(); + response.sendRedirect(tDomainName + request.getContextPath() + loginUrl + "?" + REDIRECT_URL_PARAMETER_NAME + "=" + URLEncoder.encode(redirectUrl, "UTF-8")); + } else { + response.sendRedirect(tDomainName + request.getContextPath() + loginUrl); + } + return false; + } + } + } + +} diff --git a/tamguo-bms/src/main/resources/application.properties b/tamguo-bms/src/main/resources/application.properties index 804550b..8508fca 100644 --- a/tamguo-bms/src/main/resources/application.properties +++ b/tamguo-bms/src/main/resources/application.properties @@ -1,5 +1,6 @@ domain.name=http://localhost:8083/ admin.domain.name=http://localhost:8083/ +tiku.admin.name=https://www.tamguo.com/ server.port=8083 jasypt.encryptor.password=tamguo diff --git a/tamguo-tms/src/main/resources/application.properties b/tamguo-tms/src/main/resources/application.properties index 1c869ee..6f48bab 100644 --- a/tamguo-tms/src/main/resources/application.properties +++ b/tamguo-tms/src/main/resources/application.properties @@ -1,5 +1,5 @@ -domain.name=https://www.tamguo.com/ -admin.domain.name=https://www.tamguo.com/ +domain.name=http://localhost:8081/ +admin.domain.name=http://localhost:8081/ server.port=8081 jasypt.encryptor.password=tamguo @@ -12,14 +12,14 @@ spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.maxWait=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.minIdle=5 -spring.datasource.password=tanguo520pig +spring.datasource.password=123456 spring.datasource.poolPreparedStatements=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.testWhileIdle=true spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource -spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tamguo?useUnicode=true&characterEncoding=UTF-8&useSSL=false +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tamguo_20181110?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.validationQuery=SELECT 1 FROM DUAL