diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/annotation/ExternalRequestAuth.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/annotation/ExternalRequestAuth.java new file mode 100644 index 0000000000..0cf03c25b7 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/annotation/ExternalRequestAuth.java @@ -0,0 +1,32 @@ +/** + * Copyright 2018 人人开源 http://www.renren.io + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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. + */ + +package com.epmet.commons.tools.annotation; + +import java.lang.annotation.*; + +/** + * 需要认证的外部请求 + * @Author wxz + * @Description + * @Date 2020/4/23 16:17 + **/ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ExternalRequestAuth { + +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/ExternalRequestAuthAspect.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/ExternalRequestAuthAspect.java new file mode 100644 index 0000000000..fa9156f8f3 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/ExternalRequestAuthAspect.java @@ -0,0 +1,35 @@ +package com.epmet.commons.tools.aspect; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; + +/** + * 外部请求认证切面 + */ +@Aspect +@Component +public class ExternalRequestAuthAspect { + + /** + * 拦截加了ExternalRequestAuth注解的方法 + * @param point + * @throws Throwable + */ + @Before("@annotation(com.epmet.commons.tools.annotation.ExternalRequestAuth)") + public void before(JoinPoint point) throws Throwable { + System.out.println("切面执行了"); + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes; + HttpServletRequest request = sra.getRequest(); + String token = request.getHeader("token"); + System.out.println("token:" + token); + } + +} diff --git a/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java index 438f105eab..b8ab2dbda6 100644 --- a/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java +++ b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java @@ -1,5 +1,8 @@ package com.epmet.controller; +import com.epmet.commons.tools.annotation.ExternalRequestAuth; +import com.epmet.service.TestService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -9,9 +12,14 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("test") public class TestController { + @Autowired + private TestService testService; + + @ExternalRequestAuth @GetMapping("test") public void test() { - System.out.println(666); + System.out.println("TestController -> test()"); + testService.test(); } } diff --git a/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/TestService.java b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/TestService.java new file mode 100644 index 0000000000..587690221c --- /dev/null +++ b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/TestService.java @@ -0,0 +1,7 @@ +package com.epmet.service; + +public interface TestService { + + void test(); + +} diff --git a/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/impl/TestServiceImpl.java b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/impl/TestServiceImpl.java new file mode 100644 index 0000000000..99ba370525 --- /dev/null +++ b/epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/impl/TestServiceImpl.java @@ -0,0 +1,16 @@ +package com.epmet.service.impl; + +import com.epmet.commons.tools.annotation.ExternalRequestAuth; +import com.epmet.service.TestService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class TestServiceImpl implements TestService { + + @ExternalRequestAuth + @Override + public void test() { + System.out.println("TestService -> test()"); + } +}