今天在项目中实现了访问控制,较之其他功能简单很多,总结一下:
在SSH框架之下,最基本的是要实现访问控制。
主要思想就是使用拦截器控制action的访问,使用jsp代码控制jsp页面的访问。
对Action的访问控制:
拦截器需要制定拦截除了LoginAction和LogoutAction之外的所有Action
一般是声明一些拦截器拦截器,然后将某些拦截器组合成拦截器栈:
<!-- 定义拦截器,在用户访问受保护资源之前判断其是否登录 -->
<!-- 拦截器 -->
<!-- 定义一个拦截器栈 -->
然后在Action配置中制定需要使用的拦截器:
yndw_1.jsp
error.jsp
但是不可能在所有的Action配置中都加上
可以这样做:
新建1+1+n个包
第一个包声明登入登出等不需要拦截的Action:
<!-- class属性对应IoC容器中的BeanId,因为struts将Action托管给Spring了 -->
yndwAction
/
返回input结果,若找不到input类型,就会尝试用action的class去创建一个
action,导致action未知类型的错误 -->
/login.jsp
login.jsp
第二个包声明拦截器,然后定义为默认拦截器:
<!-- 定义拦截器,在用户访问受保护资源之前判断其是否登录 -->
<!-- 拦截器 -->
class="com.jsjl.interceptor.LoginAuthenInterceptor" />
<!-- 定义一个拦截器栈 -->
<!-- 在这个包中声明一个默认的拦截器对于action资源直接访问,使用拦截器,对于页面直接访问,页面中判断-->
<!-- 如果一个结果在多个Action或者拦截器中使用,就应该将其配置为全局结果,注意类型为redirect -->
/login.jsp
然后其他N和包,都继承自第二个包,这样就默认继承了第二个包的默认的拦截器:
就这样,访问除了第一个包中的所有Action都会通过拦截器判断用户是否登入,否则转到登陆页面。
然后对于jsp页面的访问控制,只需要在页面开始加上以下几句即可:
<%
Object isLogin = session.getAttribute("isLogin");
if(isLogin == null){
response.sendRedirect("login.jsp");
}
%>
拦截器是这样的:
public class LoginAuthenInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context = ActionContext.getContext();
Map session = context.getSession();
Object user = session.get("isLogin");
if(user == null){
return "login";
}
return invocation.invoke();
}
}
对JSP页面的访问控制:
在页面开头加入如下jsp代码即可:
<%!
String greeting = "";
%>
<%
UserBean user = (UserBean)session.getAttribute("user");
if(user == null){
response.sendRedirect("login.jsp");
}
else greeting = user.getNiname();
%>
---------------------->>转载于:http://blog.sina.com.cn/s/blog_6a1928130100ughg.html
配置文件部分
(struts.xml)
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
"struts-default.xml"/>
<!--不受权限控制的Action请求配置-->
<package name="non-authority" extends="struts-default" >
"login" class=
"com.aumy.struts.example.LoginAction">
"input">/login.jsp
"error">/error.jsp
"success">/welcome.jsp
"qurey" class="com.aumy.struts.example.LoginAction" method="qurey">
"success">/qurey.jsp
package>
<!--受权限控制的Action请求配置-->
<
package name=
"authority" extends=
"struts-default">
<!--定义一个名为authority的拦截器-->
class="com.aumy.struts.example.intercepter.AuthorityInterceptor"
name="authority"/>
<!--定义一个包含权限检查的拦截器栈-->
"mydefault">
<!--配置内建默认拦截器-->
"defaultStack"/>
<!--配置自定义的拦截器-->
"authority"/>
<
default-interceptor-ref name=
"mydefault" />
<!--定义全局Result-->
"login">/login.jsp
"show" class="com.aumy.struts.example.LoginAction"
method="show">
"success">/show.jsp
"add" class="com.aumy.struts.example.LoginAction"
method="add">
"success">/add.jsp
package>
(struts.properties)
struts.custom.i18n.resources=message.messageResouce
(web.xml)
"1.0" encoding="UTF-8"?>
"2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
Struts test
struts2
class>org.apache.struts2.dispatcher.FilterDispatcherclass>
struts2
/*
login.jsp
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29119536/viewspace-1181564/
,如需转载,请注明出处,否则将追究法律责任。