Struts2自定义拦截器,实现登录检查。

[1].[代码] JavaWeb/struts2框架 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
一、新建struts-base.xml
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
     <include file= "struts-default.xml" ></include>
     < package name= "checkLogin" extends = "struts-default" >
     <!--定义返回结果类型  -->
     <result-types>
         <result-type name= "json" class = "com.util.JSONResult" ></result-type>
     </result-types>
 
     <!-- 定义一个拦截器 -->
     <interceptors>
         <interceptor name= "authority" class = "com.util.CheckLoginInterceptor" ></interceptor>
         <!--定义一个拦截栈  -->
         <interceptor-stack name= "myDefaultStack" >
             <interceptor-ref name= "defaultStack" ></interceptor-ref><!--默认拦截栈  -->
             <interceptor-ref name= "authority" ></interceptor-ref><!--自定义拦截栈  -->
         </interceptor-stack>
     </interceptors>
     <!--设定默认栈  -->
     < default -interceptor-ref name= "myDefaultStack" ></ default -interceptor-ref>
     
     <!--定义全局结果集  -->
     <global-results>
         <result name= "login" >/index.jsp</result>
     </global-results>
     
     </ package >
</struts>
 
 
2 .struts.xml 配置
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
     <constant name= "struts.devModel" value= "true" ></constant>
     <include file= "struts-default.xml" ></include>
     <include file= "struts-base.xml" ></include>
     
     < package name= "default" extends = "struts-default" namespace= "/" >
     
     <result-types>
         <result-type name= "json" class = "com.util.JSONResult" ></result-type>
     </result-types>
     
         <action name= "index" class = "com.action.LoginAction" >
             <result name= "json" type= "json" />
         </action>
         
         <action name= "code" class = "com.action.CodeAction" ></action>
         
     </ package >
     
     < package name= "show" extends = "checkLogin" namespace= "/" >
     
         <action name= "show" class = "com.action.ShowAction" >
             <result name= "success" >/main.jsp</result>
         </action>
         
     </ package >
</struts>
 
3 .action
 
public class CheckLoginInterceptor extends AbstractInterceptor {
 
     @Override
     public String intercept(ActionInvocation invocation) throws Exception {
         //获取ActionContext
         ActionContext ac = invocation.getInvocationContext();
         //获取session
         Map<String, Object> session = ac.getSession();
         //获取session中的用户信息
         String userName = (String)session.get( "userName" );
         //判断用户信息是否正确
         if (userName != null ) {
             return invocation.invoke();
         } else {
             return "login" ;
         }
     }
 
}

[2].[文件] struts-base.xml ~ 1KB    下载(0) 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
< struts >
     < include file = "struts-default.xml" ></ include >
     < package name = "checkLogin" extends = "struts-default" >
     <!--定义返回结果类型  -->
     < result-types >
         < result-type name = "json" class = "com.util.JSONResult" ></ result-type >
     </ result-types >
 
     <!-- 定义一个拦截器 -->
     < interceptors >
         < interceptor name = "authority" class = "com.util.CheckLoginInterceptor" ></ interceptor >
         <!--定义一个拦截栈  -->
         < interceptor-stack name = "myDefaultStack" >
             < interceptor-ref name = "defaultStack" ></ interceptor-ref > <!--默认拦截栈  -->
             < interceptor-ref name = "authority" ></ interceptor-ref > <!--自定义拦截栈  -->
         </ interceptor-stack >
     </ interceptors >
     <!--设定默认栈  -->
     < default-interceptor-ref name = "myDefaultStack" ></ default-interceptor-ref >
     
     <!--定义全局结果集  -->
     < global-results >
         < result name = "login" >/index.jsp</ result >
     </ global-results >
     
     </ package >
</ struts >

[3].[文件] struts.xml ~ 927B    下载(0) 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
< struts >
     < constant name = "struts.devModel" value = "true" ></ constant >
     < include file = "struts-default.xml" ></ include >
     < include file = "struts-base.xml" ></ include >
     
     < package name = "default" extends = "struts-default" namespace = "/" >
     
     < result-types >
         < result-type name = "json" class = "com.util.JSONResult" ></ result-type >
     </ result-types >
     
         < action name = "index" class = "com.action.LoginAction" >
             < result name = "json" type = "json" />
         </ action >
         
         < action name = "code" class = "com.action.CodeAction" ></ action >
         
     </ package >
     
     < package name = "show" extends = "checkLogin" namespace = "/" >
     
         < action name = "show" class = "com.action.ShowAction" >
             < result name = "success" >/main.jsp</ result >
         </ action >
         
     </ package >
</ struts >

[4].[文件] CheckLoginInterceptor.java ~ 786B    下载(0) 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.util;
 
import java.util.Map;
 
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 
public class CheckLoginInterceptor extends AbstractInterceptor {
 
     @Override
     public String intercept(ActionInvocation invocation) throws Exception {
         //获取ActionContext
         ActionContext ac = invocation.getInvocationContext();
         //获取session
         Map<String, Object> session = ac.getSession();
         //获取session中的用户信息
         String userName = (String)session.get( "userName" );
         //判断用户信息是否正确
         if (userName != null ) {
             return invocation.invoke();
         } else {
             return "login" ;
         }
     }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值