基于springmvc开发注解式ip
⼀、注解类
1 @Documented
2 @Target({ElementType.TYPE,ElementType.METHOD})
3 @Retention(RetentionPolicy.RUNTIME)
4public @interface IPFilter {
5
6/**
7 * 访问ip⽩名单
8 * @return
9*/
10 String[] allow() default {};
11
12/**
13 * 访问ip⿊名单
14 * @return
15*/
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。
⼆、注解实现
1public class AuthorityIPInterceptor extends HandlerInterceptorAdapter {
2private final static Logger logger = Logger(AuthorityIPInterceptor.class);
3
4 @Override
5public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
6throws Exception {
7if (handler instanceof HandlerMethod) {
8 IPFilter ipFilter = ((HandlerMethod) handler).getMethodAnnotation(IPFilter.class);
9if (ipFilter == null) {
10return true;
11 }
12 String ipAddress = getIpAddress(request);
13 ArrayList<String> deny = wArrayList(ipFilter.deny());
14 ArrayList<String> allow = wArrayList(ipFilter.allow());
15
16//设置了⿊名单, ⿊名单内ip不给通过
17if (CollectionUtils.isNotEmpty(deny)) {
18if (ains(ipAddress)) {
19 logger.info("IP: "+ipAddress+" 被拦截");
20 response.setStatus(500);
21return false;
22 }
23 }
24//设置了⽩名单, 只有⽩名单内的ip给通过
25if (CollectionUtils.isNotEmpty(allow)) {
26if (ains(ipAddress)) {
27 logger.info("IP: "+ipAddress+" 被放⾏");
28return true;
29 }else {
30 logger.info("IP: "+ipAddress+" 没有放⾏权利");
31 response.setStatus(500);
32return false;
33 }
34 }
35 }
36return true;
37 }
38
39/**
40 * 获取请求主机IP地址,如果通过代理进来,则透过防⽕墙获取真实IP地址;
41 *
42 * @param request
43 * @return
44 * @throws IOException
45*/
46public final static String getIpAddress(HttpServletRequest request) throws IOException {
47// 获取请求主机IP地址,如果通过代理进来,则透过防⽕墙获取真实IP地址
48
49 String ip = Header("X-Forwarded-For");
50if (logger.isInfoEnabled()) {
51 logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
52 }
53
54if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
55if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
56 ip = Header("Proxy-Client-IP");
57if (logger.isInfoEnabled()) {
58 logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
59 }
60 }
61if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
62 ip = Header("WL-Proxy-Client-IP");
63if (logger.isInfoEnabled()) {
64 logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
65 }
66 }
67if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
68 ip = Header("HTTP_CLIENT_IP");
69if (logger.isInfoEnabled()) {
70 logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
71 }
72 }
73if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
74 ip = Header("HTTP_X_FORWARDED_FOR");
75if (logger.isInfoEnabled()) {
76 logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
77 }
78 }
79if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
80 ip = RemoteAddr();
81if (logger.isInfoEnabled()) {
82 logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
83 }
84 }springmvc面试题常用注解
85 } else if (ip.length() > 15) {
86 String[] ips = ip.split(",");
87for (int index = 0; index < ips.length; index++) {
88 String strIp = (String) ips[index];
89if (!("unknown".equalsIgnoreCase(strIp))) {
90 ip = strIp;
91break;
92 }
93 }
94 }
95return ip;
96 }
97 }
View Code
通过继承springmvc的,对所有访问当前加了注解的controller接⼝和⽅法进⾏拦截
三、路径拦截
1 <mvc:interceptor>
2 <mvc:mapping path="/**/xxx/**"/>
3 <bean class="AuthorityIPInterceptor"/>
4 </mvc:interceptor> 通过在mvc的配置⽂件配置请求的拦截路径,防⽌所有的请求都被拦截,做到精准控制
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论