yj10864
BlogJava
::
首頁
::
聯系
::
聚合
::
管理
8 Posts :: 1 Stories :: 9 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
給我留言
查看公開留言
查看私人留言
我參與的團隊
隨筆檔案
2010年1月 (1)
2009年12月 (2)
2009年11月 (1)
2009年10月 (2)
2009年9月 (2)
文章檔案
2009年10月 (1)
搜索
最新評論
1.?re: 23中設計模式-------輕松的比喻語言
不錯,讓我有了一定的理解。
--lexy
2.?re: 23中設計模式-------輕松的比喻語言
頂一下,牛!
--WIN
3.?re: 日期類的加減及java中所以日期類的操作算法大全
評論內容較長,點擊標題查看
--找個美女做老婆
4.?re: 日期類的加減及java中所以日期類的操作算法大全
評論內容較長,點擊標題查看
--找個美女做老婆
5.?re: 23中設計模式-------輕松的比喻語言
評論內容較長,點擊標題查看
--找個美女做老婆
閱讀排行榜
1.?日期類的加減及java中所以日期類的操作算法大全(7622)
2.?23中設計模式-------輕松的比喻語言(3224)
3.?解析properties資源文件(645)
4.?Spring的字符過濾器的配置(580)
5.?tomcat關閉后重啟,SESSION 仍然有效的處理方法(352)
評論排行榜
1.?23中設計模式-------輕松的比喻語言(5)
2.?日期類的加減及java中所以日期類的操作算法大全(4)
3.?解析properties資源文件(0)
4.?使用 Java 生成 MD5 編碼 (0)
5.?tomcat關閉后重啟,SESSION 仍然有效的處理方法(0)
struts2實現IP攔截
基本思想:先將的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位二進制數。
11
public
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
14
public
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
14
public
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
閱讀(1584)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Copyright @ jerry yang
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
主站蜘蛛池模板:
免费精品国自产拍在线播放
|
久9这里精品免费视频
|
91频在线观看免费大全
|
精品亚洲麻豆1区2区3区
|
99热这里只有精品6免费
|
亚洲综合免费视频
|
美女视频黄频a免费观看
|
国产成人免费一区二区三区
|
亚洲av成人一区二区三区观看在线
|
亚洲精品天堂成人片AV在线播放
|
操美女视频免费网站
|
亚洲精品国产精品
|
亚洲av成人一区二区三区在线观看
|
国产成人无码综合亚洲日韩
|
a级毛片免费完整视频
|
亚洲国产成人久久综合碰碰动漫3d
|
亚洲粉嫩美白在线
|
成人毛片18女人毛片免费视频未
|
亚洲精品无码永久在线观看男男
|
国产yw855.c免费视频
|
亚洲成年人免费网站
|
猫咪社区免费资源在线观看
|
日韩欧美亚洲中文乱码
|
亚洲午夜无码片在线观看影院猛
|
韩日电影在线播放免费版
|
911精品国产亚洲日本美国韩国
|
成人AV免费网址在线观看
|
亚洲成av人片在www鸭子
|
久久久久亚洲AV综合波多野结衣
|
久爱免费观看在线网站
|
最新亚洲精品国偷自产在线
|
亚洲高清免费视频
|
8x8x华人永久免费视频
|
亚洲国产无线乱码在线观看
|
国产精品亚洲视频
|
无码日韩人妻av一区免费
|
黄色a三级免费看
|
亚洲高清无在码在线无弹窗
|
手机看片久久国产免费
|
亚洲国产精品成人AV在线
|
不卡一卡二卡三亚洲
|