一般网站为了防止有人恶意登录(未登录就直接访问后台页面),都会用拦截器Interceptor来限制登录。
下边是一个拦截器登录限制的一个小例子,帮助理解和应用拦截器。
在拦截器中拦截的依据就是查看session作用域中有没有用户的信息。有的话,通过;没有则根据需要进行处理。
首先要理解拦截器在登录限制里所起到的作用。拦截器拦截的是action请求。所以应有这样一个流程。
登录页面-->LoginAction-->result-->Interceptor-->ShowAction-->后台。
如果将Interceptor 放在LoginAction之前,就会导致一直登录不成功(此时程序还没有运行过LoginAction,session中肯定是没有用户信息的)。
看代码。
配置struts.xml(拦截器要配置在action上边,需要使用拦截器的action需要配置<interceptor-ref/>)
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- 指定Struts 2配置文件的根元素 -->
<struts>
<package name="wyg" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="authority"
class="com.wyg.inter.LoginInterceptor">
</interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<action name="login" class="com.wyg.action.LoginAction" >
<result name="success" type="chain">show</result>
<result name="error">/login.jsp</result>
</action>
<action name="show" class="com.wyg.action.ShowAction">
<result name="success">/show.jsp</result>
<result name="error">/login.jsp</result>
<!-- 使用此拦截器 -->
<interceptor-ref name="mydefault" />
</action>
</package>
</struts>
拦截器类
public class LoginInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
String user = (String) session.getAttribute("User");
if (user != null && user.equals("admin")) {
System.out.println("test");
return invocation.invoke();
}
return "error";
}
}
LoginAction(拦截器并不对经过这个action的请求进行拦截)
public class LoginAction extends ActionSupport {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String execute() throws Exception {
if(getName()!=null&&getPwd()!=null){
if(this.getName().equals("admin")&& this.getPwd().equals("123")){
ServletActionContext.getRequest().getSession().setAttribute("User", getName());
ServletActionContext.getRequest().getSession().setAttribute("Pwd", getPwd());
return "success";
}
return "error";
}
return "error";
}
}
ShowAction(拦截器对经过此action的请求进行拦截。若session中有需要的数据,则可通过拦截器完成后续操作。)
public class ShowAction extends ActionSupport {
public String execute() {
return "success";
}
}
login.jsp(登录页面)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
User:<input type="text" name="name"><br>
Passoword:<input type="password" name="pwd"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
show.jsp(登录成功页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Show This Page
登陆成功<br>
</body>
</html>
测试
在浏览器地址栏输入地址
”http://localhost:8090/StrutsDemo_Interceptor/login.jsp“
登录成功后。在新开的标签页中直接输入后台地址(show为action地址)
“http://localhost:8090/StrutsDemo_Interceptor/show”
仍可进入登录成功页面。
若在另一浏览器中(session中没有存’user’)输入上边的后台地址
“http://localhost:8090/StrutsDemo_Interceptor/show”
则会跳到登录界面。
注意:拦截器拦截的是经过action的请求。若你直接在浏览器地址栏中输入后台页面的地址,是可以看到页面的。但是动态页面是不会拿到数据的。
另外,在struts中,我们进行操作都是通过action提交请求的,所以普通用户是看不到我们的后台页面地址的,在地址栏显示的都是我们的action地址,所以不用纠结于用户会直接敲后台页面地址去访问。