From 24300f36626c87c64f834b002bf58d8478909397 Mon Sep 17 00:00:00 2001 From: jiangjiazhi Date: Fri, 22 Apr 2016 14:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-report-api/pom.xml | 105 +++++++++++------- .../report/web/controller/TestController.java | 13 +++ .../web/inteceptor/TokenValidateInteceptor.java | 94 +++++++++++++++++ .../resources/spring/applicationContext-mvc.xml | 42 ++++++++ .../main/resources/spring/applicationContext.xml | 2 +- .../src/main/resources/spring/spring-mongodb.xml | 54 ++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 23 ---- .../src/main/webapp/WEB-INF/web.xml | 117 ++++++++++++--------- 8 files changed, 341 insertions(+), 109 deletions(-) create mode 100644 platform-report-api/src/main/java/com/lyms/platform/report/web/controller/TestController.java create mode 100644 platform-report-api/src/main/java/com/lyms/platform/report/web/inteceptor/TokenValidateInteceptor.java create mode 100644 platform-report-api/src/main/resources/spring/applicationContext-mvc.xml create mode 100644 platform-report-api/src/main/resources/spring/spring-mongodb.xml delete mode 100644 platform-report-api/src/main/webapp/WEB-INF/cxf-servlet.xml diff --git a/platform-report-api/pom.xml b/platform-report-api/pom.xml index 9f5e98b..31cd7b2 100644 --- a/platform-report-api/pom.xml +++ b/platform-report-api/pom.xml @@ -11,24 +11,87 @@ platform-report-api - + com.lyms.core platform-common ${project.version} - + + + com.lyms.core + platform-biz-patient-service + ${project.version} + + + + dev + + + + true + + + + ../platform-resource/resources/config-dev.properties + + + + + test + + + ../platform-resource/resources/config-test.properties + + + + + prod + + + ../platform-resource/resources/config-product.properties + + + + + + + ../platform-resource/resources + + + **/* + + + true + + + src/main/resources + + **/* + + + + + + + src/main/resources + + database.properties + + true + + org.apache.maven.plugins @@ -41,36 +104,4 @@ platform-report-api - \ No newline at end of file diff --git a/platform-report-api/src/main/java/com/lyms/platform/report/web/controller/TestController.java b/platform-report-api/src/main/java/com/lyms/platform/report/web/controller/TestController.java new file mode 100644 index 0000000..9cf62b3 --- /dev/null +++ b/platform-report-api/src/main/java/com/lyms/platform/report/web/controller/TestController.java @@ -0,0 +1,13 @@ +package com.lyms.platform.report.web.controller; + +import org.springframework.stereotype.Controller; + +/** + * + * + * + * Created by Administrator on 2016/4/20 0020. + */ +@Controller +public class TestController { +} diff --git a/platform-report-api/src/main/java/com/lyms/platform/report/web/inteceptor/TokenValidateInteceptor.java b/platform-report-api/src/main/java/com/lyms/platform/report/web/inteceptor/TokenValidateInteceptor.java new file mode 100644 index 0000000..548210b --- /dev/null +++ b/platform-report-api/src/main/java/com/lyms/platform/report/web/inteceptor/TokenValidateInteceptor.java @@ -0,0 +1,94 @@ + +package com.lyms.platform.report.web.inteceptor; + + +import com.lyms.platform.common.annotation.TokenRequired; +import com.lyms.platform.common.base.BaseController; +import com.lyms.platform.common.base.ContextHolder; +import com.lyms.platform.common.base.LoginContext; +import com.lyms.platform.common.exception.ParameterException; +import com.lyms.platform.common.exception.TokenException; +import com.lyms.platform.common.utils.LogUtil; +import com.lyms.platform.common.utils.LoginUtil; +import com.lyms.platform.permission.model.Users; +import com.lyms.platform.permission.service.UsersService; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.lang.annotation.Annotation; + +/** + * 验证token拦截器 + * + * + * + */ + +public class TokenValidateInteceptor extends HandlerInterceptorAdapter { + + @Autowired + private UsersService usersService; + + public static boolean isSiteController(Object handler) { + return handler instanceof HandlerMethod && (((HandlerMethod) handler).getBean() instanceof BaseController); + } + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + if (!isSiteController(handler)) + return true; + TokenRequired clientRequired = findAnnotation((HandlerMethod) handler, TokenRequired.class); + if (null == clientRequired) + return true; + + validateToken(request, response); + + return true; + } + + private T findAnnotation(HandlerMethod handler, Class annotationType) { + T annotation = handler.getBeanType().getAnnotation(annotationType); + if (annotation != null) + return annotation; + return handler.getMethodAnnotation(annotationType); + } + + public boolean validateToken(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { + String token = httpServletRequest.getHeader("Authorization"); + if (StringUtils.isEmpty(token)) { + throw new ParameterException(); + } + LoginContext loginContext = LoginUtil.checkLoginState(token); + if(!loginContext.isLogin()) { + throw new TokenException(); + } + Users users = usersService.getUsersByLoginCenterId(loginContext.getId()); + if(null == users) { + throw new TokenException(); + } + loginContext.setId(users.getId()); + loginContext.setToken(token); + httpServletRequest.setAttribute("loginContext", loginContext); + + LogUtil.tokenInfo(" userId:" + users.getId() + ", ,url:" + httpServletRequest.getRequestURI() + ",method:" + httpServletRequest.getMethod()); + + return loginContext.isLogin(); + } + + @Override + public void afterCompletion( + HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) + throws Exception { + ContextHolder.clean(); + } +} diff --git a/platform-report-api/src/main/resources/spring/applicationContext-mvc.xml b/platform-report-api/src/main/resources/spring/applicationContext-mvc.xml new file mode 100644 index 0000000..ffb94ff --- /dev/null +++ b/platform-report-api/src/main/resources/spring/applicationContext-mvc.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/platform-report-api/src/main/resources/spring/applicationContext.xml b/platform-report-api/src/main/resources/spring/applicationContext.xml index 1dc27d6..c997463 100644 --- a/platform-report-api/src/main/resources/spring/applicationContext.xml +++ b/platform-report-api/src/main/resources/spring/applicationContext.xml @@ -13,5 +13,5 @@ - + \ No newline at end of file diff --git a/platform-report-api/src/main/resources/spring/spring-mongodb.xml b/platform-report-api/src/main/resources/spring/spring-mongodb.xml new file mode 100644 index 0000000..32cd518 --- /dev/null +++ b/platform-report-api/src/main/resources/spring/spring-mongodb.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/platform-report-api/src/main/webapp/WEB-INF/cxf-servlet.xml b/platform-report-api/src/main/webapp/WEB-INF/cxf-servlet.xml deleted file mode 100644 index cc10158..0000000 --- a/platform-report-api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - diff --git a/platform-report-api/src/main/webapp/WEB-INF/web.xml b/platform-report-api/src/main/webapp/WEB-INF/web.xml index 2c6f850..8ebaefa 100644 --- a/platform-report-api/src/main/webapp/WEB-INF/web.xml +++ b/platform-report-api/src/main/webapp/WEB-INF/web.xml @@ -1,56 +1,77 @@ - + + + contextConfigLocation + + classpath*:/spring/applicationContext.xml + + - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. ---> - - cxf + + 60 + + + log4jConfigLocation + classpath:log4j_config.xml + - contextConfigLocation - - classpath*:/applicationContext.xml - - + webAppRootKey + webapp.report-api + + + log4jRefreshInterval + 60000 + - Apache CXF Endpoint - cxf - cxf - org.apache.cxf.transport.servlet.CXFServlet + dispatcher + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath*:/spring/applicationContext-mvc.xml + 1 - cxf - /services/* + dispatcher + / - - 60 - - - log4jConfigLocation - classpath:log4j_config.xml - - - log4jRefreshInterval - 60000 - - - org.springframework.web.context.ContextLoaderListener - - - org.springframework.web.util.Log4jConfigListener - - + + HttpMethodFilter + org.springframework.web.filter.HttpPutFormContentFilter + + + HttpMethodFilter + /* + + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + + + encodingFilter + /* + + + org.springframework.web.context.ContextLoaderListener + + + org.springframework.web.util.Log4jConfigListener + + \ No newline at end of file -- 1.8.3.1