yj10864

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  8 Posts :: 1 Stories :: 9 Comments :: 0 Trackbacks
基本思想:先将的IP地址转换成long型的数,然后比较IP的大小来判断是否处在符合条件的IP地址段。
IP地址转化成long型数的算法
 1
 2  // 一个IP,是一个32位无符号的二进制数。故用long的低32表示无符号32位二进制数。  
 3    public long getIP(InetAddress ip) {  
 4           byte[] b = ip.getAddress();  
 5          long l = b[0<< 24L & 0xff000000L | b[1<< 16L & 0xff0000L  
 6                 | b[2<< 8L & 0xff00 | b[3<< 0L & 0xff;  
 7           return l;  
 8       }
  
 9
10// 一个IP,是一个32位无符号的二进制数。故用long的低32表示无符号32位二进制数。
11public long getIP(InetAddress ip) {
12        byte[] b = ip.getAddress();
13        long l = b[0<< 24L & 0xff000000L | b[1<< 16L & 0xff0000L
14                | b[2<< 8L & 0xff00 | b[3<< 0L & 0xff;
15        return l;
16    }
在struts2相应的action中编写如下判断是否用户是校内用户的方法(方法参数中ip1的IP大小应该大于ip2的IP大小):
 1   public void isSchoolUser(String ip1, String ip2) throws Exception {  
 2             // 得到用户的IP地址  
 3            String s = ServletActionContext.getRequest().getRemoteAddr();  
 4           long l = getIP(InetAddress.getByName(s));  
 5            // 设置IP地址段  
 6           long l1 = getIP(InetAddress.getByName(ip1));  
 7           long l2 = getIP(InetAddress.getByName(ip2));  
 8             // 判断用户IP是否处在IP段中  
 9            if (l >= l1 && l <= l2) {  
10           ActionContext.getContext().getSession().put("isSchoolUser","yes");  
11           }
  
12       }
  
13
14public void isSchoolUser(String ip1, String ip2) throws Exception {
15         // 得到用户的IP地址
16        String s = ServletActionContext.getRequest().getRemoteAddr();
17        long l = getIP(InetAddress.getByName(s));
18         // 设置IP地址段
19        long l1 = getIP(InetAddress.getByName(ip1));
20        long l2 = getIP(InetAddress.getByName(ip2));
21         // 判断用户IP是否处在IP段中
22        if (l >= l1 && l <= l2) {
23        ActionContext.getContext().getSession().put("isSchoolUser","yes");
24        }

25    }
上面的方法中用到了InetAddress,所以需要引入java.net.InetAddress包;
接着再编写IP拦截器如下:
 1    public class IpAuthorityInterceptor extends AbstractInterceptor {  
 2        public String intercept(ActionInvocation invocation) throws Exception {  
 3            ActionContext context = invocation.getInvocationContext();  
 4            Map<String, String> session = context.getSession();  
 5            if ("yes".equals(session.get("isSchoolUser"))){  
 6                return invocation.invoke();  
 7            }
 else {  
 8                context.put("AuthorityError""你是外网用户无法访问此资源");  
 9                return "error";  
10           }
  
11       }
  
12   }
  
13
14public class IpAuthorityInterceptor extends AbstractInterceptor {
15    public String intercept(ActionInvocation invocation) throws Exception {
16        ActionContext context = invocation.getInvocationContext();
17        Map<String, String> session = context.getSession();
18        if ("yes".equals(session.get("isSchoolUser"))){
19            return invocation.invoke();
20        }
 else {
21            context.put("AuthorityError""你是外网用户无法访问此资源");
22            return "error";
23        }

24    }

25}
 1<interceptors>  
 2             <interceptor name="IpAuthorityInterceptor"  
 3                  class="web.IpAuthorityInterceptor">  
 4                 <!--此class对应你项目中的IpAuthorityInterceptor的编写位置-->  
 5               </interceptor>  
 6               <interceptor-stack name="IpAuthority">  
 7                   <interceptor-ref name="defaultStack"></interceptor-ref>  
 8                  <interceptor-ref name="IpAuthorityInterceptor"></interceptor-ref>  
 9               </interceptor-stack>  
10           </interceptors>  
11
12<interceptors>
13            <interceptor name="IpAuthorityInterceptor"
14                class="web.IpAuthorityInterceptor">
15              <!--此class对应你项目中的IpAuthorityInterceptor的编写位置-->
16            </interceptor>
17            <interceptor-stack name="IpAuthority">
18                <interceptor-ref name="defaultStack"></interceptor-ref>
19                <interceptor-ref name="IpAuthorityInterceptor"></interceptor-ref>
20            </interceptor-stack>
21        </interceptors>
posted on 2009-10-26 10:43 jerry yang 阅读(1574) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: