yj10864
BlogJava
::
首頁(yè)
::
聯(lián)系
::
聚合
::
管理
8 Posts :: 1 Stories :: 9 Comments :: 0 Trackbacks
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
給我留言
查看公開(kāi)留言
查看私人留言
我參與的團(tuán)隊(duì)
隨筆檔案
2010年1月 (1)
2009年12月 (2)
2009年11月 (1)
2009年10月 (2)
2009年9月 (2)
文章檔案
2009年10月 (1)
搜索
最新評(píng)論
1.?re: 23中設(shè)計(jì)模式-------輕松的比喻語(yǔ)言
不錯(cuò),讓我有了一定的理解。
--lexy
2.?re: 23中設(shè)計(jì)模式-------輕松的比喻語(yǔ)言
頂一下,牛!
--WIN
3.?re: 日期類(lèi)的加減及java中所以日期類(lèi)的操作算法大全
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--找個(gè)美女做老婆
4.?re: 日期類(lèi)的加減及java中所以日期類(lèi)的操作算法大全
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--找個(gè)美女做老婆
5.?re: 23中設(shè)計(jì)模式-------輕松的比喻語(yǔ)言
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--找個(gè)美女做老婆
閱讀排行榜
1.?日期類(lèi)的加減及java中所以日期類(lèi)的操作算法大全(7623)
2.?23中設(shè)計(jì)模式-------輕松的比喻語(yǔ)言(3224)
3.?解析properties資源文件(645)
4.?Spring的字符過(guò)濾器的配置(580)
5.?tomcat關(guān)閉后重啟,SESSION 仍然有效的處理方法(352)
評(píng)論排行榜
1.?23中設(shè)計(jì)模式-------輕松的比喻語(yǔ)言(5)
2.?日期類(lèi)的加減及java中所以日期類(lèi)的操作算法大全(4)
3.?解析properties資源文件(0)
4.?使用 Java 生成 MD5 編碼 (0)
5.?tomcat關(guān)閉后重啟,SESSION 仍然有效的處理方法(0)
struts2實(shí)現(xiàn)IP攔截
基本思想:先將的IP地址轉(zhuǎn)換成long型的數(shù),然后比較IP的大小來(lái)判斷是否處在符合條件的IP地址段。
IP地址轉(zhuǎn)化成long型數(shù)的算法
1
2
//
一個(gè)IP,是一個(gè)32位無(wú)符號(hào)的二進(jìn)制數(shù)。故用long的低32表示無(wú)符號(hào)32位二進(jìn)制數(shù)。
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
//
一個(gè)IP,是一個(gè)32位無(wú)符號(hào)的二進(jìn)制數(shù)。故用long的低32表示無(wú)符號(hào)32位二進(jìn)制數(shù)。
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相應(yīng)的action中編寫(xiě)如下判斷是否用戶(hù)是校內(nèi)用戶(hù)的方法(方法參數(shù)中ip1的IP大小應(yīng)該大于ip2的IP大小):
1
public
void
isSchoolUser(String ip1, String ip2)
throws
Exception
{
2
//
得到用戶(hù)的IP地址
3
String s
=
ServletActionContext.getRequest().getRemoteAddr();
4
long
l
=
getIP(InetAddress.getByName(s));
5
//
設(shè)置IP地址段
6
long
l1
=
getIP(InetAddress.getByName(ip1));
7
long
l2
=
getIP(InetAddress.getByName(ip2));
8
//
判斷用戶(hù)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
//
得到用戶(hù)的IP地址
16
String s
=
ServletActionContext.getRequest().getRemoteAddr();
17
long
l
=
getIP(InetAddress.getByName(s));
18
//
設(shè)置IP地址段
19
long
l1
=
getIP(InetAddress.getByName(ip1));
20
long
l2
=
getIP(InetAddress.getByName(ip2));
21
//
判斷用戶(hù)IP是否處在IP段中
22
if
(l
>=
l1
&&
l
<=
l2)
{
23
ActionContext.getContext().getSession().put(
"
isSchoolUser
"
,
"
yes
"
);
24
}
25
}
上面的方法中用到了InetAddress,所以需要引入java.net.InetAddress包;
接著再編寫(xiě)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
"
,
"
你是外網(wǎng)用戶(hù)無(wú)法訪(fǎng)問(wèn)此資源
"
);
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
"
,
"
你是外網(wǎng)用戶(hù)無(wú)法訪(fǎng)問(wèn)此資源
"
);
22
return
"
error
"
;
23
}
24
}
25
}
1
<
interceptors
>
2
<
interceptor
name
="IpAuthorityInterceptor"
3
class
="web.IpAuthorityInterceptor"
>
4
<!--
此class對(duì)應(yīng)你項(xiàng)目中的IpAuthorityInterceptor的編寫(xiě)位置
-->
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對(duì)應(yīng)你項(xiàng)目中的IpAuthorityInterceptor的編寫(xiě)位置
-->
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)
評(píng)論(0)
編輯
收藏
新用戶(hù)注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶(hù)
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
Copyright @ jerry yang
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
主站蜘蛛池模板:
亚洲人成777在线播放
|
亚洲AV中文无码乱人伦
|
亚洲精品午夜国产VA久久成人
|
亚洲av无码一区二区三区人妖
|
中国在线观看免费国语版
|
日本亚洲成高清一区二区三区
|
自拍偷自拍亚洲精品偷一
|
日韩a级毛片免费视频
|
国产精品亚洲专一区二区三区
|
日本免费一本天堂在线
|
www亚洲精品久久久乳
|
深夜国产福利99亚洲视频
|
日韩成人毛片高清视频免费看
|
亚洲精品久久久www
|
a级毛片无码免费真人久久
|
久久久久久a亚洲欧洲AV
|
222www免费视频
|
亚洲乱码一二三四区乱码
|
无人在线观看免费高清视频
|
亚洲精品亚洲人成在线
|
亚洲AV之男人的天堂
|
精品亚洲永久免费精品
|
亚洲综合视频在线
|
欧美在线看片A免费观看
|
亚洲乱亚洲乱妇24p
|
久久久久亚洲精品天堂久久久久久
|
成人片黄网站色大片免费观看APP
|
亚洲成a人片77777kkkk
|
18禁免费无码无遮挡不卡网站
|
亚洲欧洲日韩极速播放
|
亚洲中文字幕第一页在线
|
久久99精品免费视频
|
亚洲第一区二区快射影院
|
青青青国产色视频在线观看国产亚洲欧洲国产综合
|
亚洲成a人片在线观看无码专区
|
无码精品A∨在线观看免费
|
日日摸日日碰夜夜爽亚洲
|
久久久久亚洲av无码尤物
|
日韩免费电影在线观看
|
baoyu122.永久免费视频
|
ass亚洲**毛茸茸pics
|