Browse Source

集群外部服务请求认证注解和拦截器雏形

dev_shibei_match
wxz 5 years ago
parent
commit
032463eabe
  1. 32
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/annotation/ExternalRequestAuth.java
  2. 35
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/ExternalRequestAuthAspect.java
  3. 10
      epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java
  4. 7
      epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/TestService.java
  5. 16
      epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/service/impl/TestServiceImpl.java

32
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
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 {
}

35
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);
}
}

10
epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/controller/TestController.java

@ -1,5 +1,8 @@
package com.epmet.controller; 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -9,9 +12,14 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("test") @RequestMapping("test")
public class TestController { public class TestController {
@Autowired
private TestService testService;
@ExternalRequestAuth
@GetMapping("test") @GetMapping("test")
public void test() { public void test() {
System.out.println(666); System.out.println("TestController -> test()");
testService.test();
} }
} }

7
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();
}

16
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()");
}
}
Loading…
Cancel
Save