一、注解类 1 @Documented 2 @Target({ElementType.TYPE,ElementType.METHOD}) 3 @Retention(RetentionPolicy.RUNTIME) 4 public @interface IPFilter { 5 ...
一、注解类
1 @Documented 2 @Target({ElementType.TYPE,ElementType.METHOD}) 3 @Retention(RetentionPolicy.RUNTIME) 4 public @interface IPFilter { 5 6 /** 7 * 访问ip白名单 8 * @return 9 */10 String[] allow() default {};11 12 /**13 * 访问ip黑名单14 * @return15 */16 String[] deny() default {};17 }
View Code
1. @Documented —— 指明拥有这个注解的元素可以被javadoc此类的工具文档化。这种类型应该用于注解那些影响客户使用带注释的元素声明的类型。如果一种声明使用Documented进行注解,这种类型的注解被作为被标注的程序成员的公共API。
2. @Target——指明该类型的注解可以注解的程序元素的范围。该元注解的取值可以为TYPE,METHOD,CONSTRUCTOR,FIELD等。如果Target元注解没有出现,那么定义的注解可以应用于程序的任何元素。
3. @Inherited——指明该注解类型被自动继承。如果用户在当前类中查询这个元注解类型并且当前类的声明中不包含这个元注解类型,那么也将自动查询当前类的父类是否存在Inherited元注解,这个动作将被重复执行知道这个标注类型被找到,或者是查询到顶层的父类。
4.@Retention——指明了该Annotation被保留的时间长短。RetentionPolicy取值为SOURCE,CLASS,RUNTIME。
二、注解实现
1 public class AuthorityIPInterceptor extends HandlerInterceptorAdapter { 2 private final static Logger logger = LoggerFactory.getLogger(AuthorityIPInterceptor.class); 3 4 @Override 5 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 6 throws Exception { 7 if (handler instanceof HandlerMethod) { 8 IPFilter ipFilter = ((HandlerMethod) handler).getMethodAnnotation(IPFilter.class); 9 if (ipFilter == null) {10 return true;11 }12 String ipAddress = getIpAddress(request);13 ArrayList<String> deny = Lists.newArrayList(ipFilter.deny());14 ArrayList<String> allow = Lists.newArrayList(ipFilter.allow());15 16 //设置了黑名单, 黑名单内ip不给通过17 if (CollectionUtils.isNotEmpty(deny)) {18 if (deny.contains(ipAddress)) {19 logger.info("IP: "+ipAddress+" 被拦截");20 response.setStatus(500);21 return false;22 }23 }24 //设置了白名单, 只有白名单内的ip给通过25 if (CollectionUtils.isNotEmpty(allow)) {26 if (allow.contains(ipAddress)) {27 logger.info("IP: "+ipAddress+" 被放行");28 return true;29 }30 }31 }32 return true;33 }34 }
View Code
通过继承springmvc的**,对所有访问当前加了注解的controller接口和方法进行拦截
三、路径拦截
1 <mvc:interceptor> 2 <mvc:mapping path="/**/xxx/**"/> 3 <bean class="com.xxx..AuthorityIPInterceptor"/> 4 </mvc:interceptor>
通过在mvc的配置文件配置请求的拦截路径,防止所有的请求都被拦截,做到精准控制
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:基于springmvc开发注解式ip拦截器
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。