5 changed files with 99 additions and 1 deletions
@ -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 { |
||||
|
|
||||
|
} |
@ -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); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
package com.epmet.service; |
||||
|
|
||||
|
public interface TestService { |
||||
|
|
||||
|
void test(); |
||||
|
|
||||
|
} |
@ -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…
Reference in new issue