Struts2使用拦截器完成权限控制示例
[b]示例需求:[/b] 要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源;否则,系统直接转入登陆页面。 一、页面部分 1、登陆页面代码(login.jsp) [code] <%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="loginPage" /> [/code] 2、登陆成功页面(welcome.jsp) [code]<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="succPage" />
show
add
qurey [/code] 3、登陆失败页面(error.jsp) [code]<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="errorPage" />
return [/code] 4、和权限有关的几个显示页面 (add.jsp) [code]<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="addPage"/>
return login [/code] (show.jsp) [code] <%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="showPage"/>
return login [/code] (qurey.jsp) [code] <%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:text name="qureyPage"/>
return login [/code] 二、Action部分(LoginAction.java) [code] public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1030294046920869257L; private String username; private String password; // 处理用户请求的execute方法 public String execute() throws Exception { if (isInvalid(getUsername())) return INPUT; if (isInvalid(getPassword())) return INPUT; if ((getUsername().equals("mm") || getUsername().equals("aumy")) && getPassword().equals("111")) { // 通过ActionContext对象访问Web应用的Session ActionContext.getContext().getSession().put("user", getUsername()); ActionContext.getContext().getSession().put("pass", getPassword()); System.out.println(getUsername() + "----" + getPassword()); return SUCCESS; } else { System.out.println(getUsername() + "----" + getPassword()); return ERROR; } } private boolean isInvalid(String value) { return (value == null || value.length() == 0); } public String add() { return SUCCESS; } public String show() { return SUCCESS; } public String qurey() { return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } [/code] 三、拦截器部分(AuthorityInterceptor.java) [code] public class AuthorityInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 1358600090729208361L; //拦截Action处理的拦截方法 public String intercept(ActionInvocation invocation) throws Exception { // 取得请求相关的ActionContext实例 ActionContext ctx=invocation.getInvocationContext(); Map session=ctx.getSession(); //取出名为user的session属性 String user=(String)session.get("user"); //如果没有登陆,或者登陆所有的用户名不是aumy,都返回重新登陆 if(user!=null && user.equals("aumy")){ return invocation.invoke(); } //没有登陆,将服务器提示设置成一个HttpServletRequest属性 ctx.put("tip","您还没有登录,请登陆系统"); return Action.LOGIN; } } [/code] 四、配置文件部分 (struts.xml) [code] /login.jsp /error.jsp /welcome.jsp /qurey.jsp /login.jsp /show.jsp /add.jsp [/code] (struts.properties) [code] struts.custom.i18n.resources=message.messageResouce [/code] (web.xml) [code] Struts test struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* login.jsp [/code] 五、国际化资源文件(messageResouce.properties) [code] loginPage=Login Page errorPage=Error Page succPage=Welcome Page failTip=Sorry,You can't log in! succTip=welcome,you has logged in! user=User Name pass=User Pass login=Login showPage=Show Page showTip=show a example! addPage=Add Page addTip=add a example! qureyPage=Qurey Page qureyTip=qurey a example! [/code]
才疏学浅,看明白了,运行成功了。感谢楼主分享!
把拦截器注释掉依然能运行,也没用到拦截器呀
可能是你的路径,配的不对。。。所以找不到文件 。。。。
确实是饶不了,可是它也没有拦截,页面直接显示404错误
执行其他action都是正常的
基本看明白了,但是我的执行login.action总是报404 HTTP Status 404 - No result defined for action com.chen.interceptor.action.LoginAction and result success
很好的贴啊。。他这个例子全都是通过Action,知道jsp也拿不到数据。一般人也不会知道jps,url不会显示。
如果把所有网页全放到WEB-INF目录,你就绕不了了……
这个可以用servlet的拦截器做到
不错的文章,这个是struts的拦截器做的,但是没有对jsp页面做拦截,加上去就好了
明显的有漏洞,绕过action的话 不就不行了吗?直接输入网址
很清晰,好文章!
这个比较清楚些 :idea:
把jsp页面都放到WEB-INF下就不可以直接访问了~
你这个只能对action进行权限控制,如果我直接输入 ***.jsp呢,不是照样可以访问到!
看了那么多,这个比较清楚些,顶了
:wink:
拦截器只可以对action拦截,怎么对jsp页面拦截呢?
没有把 权限定位到指定的页面 如果我直接输入管理员的页面,也是可以的
国家化有些不完整吧? ctx.put("tip","您还没有登录,请登陆系统");
这里能实现么?
PS:在下新手..