1. 首先说登陆:
个人觉得登陆配置一个过滤器(LoginCheckFilter)类继承javax.servlet.Filter,实现doFilter方法,再者就是在项目的web.xml文件中配置好过滤器类,下面贴出个人的实际可行代码
1.1 LoginCheckFilter类:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.ipanel.tv.bank.bean.LoginBean;
public class LoginCheckFilter implements Filter
{
@Override
public void destroy()
{
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
LoginBean loginBean = (LoginBean) request.getSession().getAttribute(
"loginBean");
if (loginBean != null)
{
arg2.doFilter(arg0, arg1);
}
else
{
response.sendRedirect("../publicPage/login.jsp");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException
{
}
}
(附录补冲说明:LoginBean是自己写的一个接口,对于要登录的各种实体Bean类都会去实现这个接口的getLoginName(),getLoginPassword,getLoginType(),setLoginType()方法)
1.2 过滤器类LoginCheckFilter的配置:
<filter>
<filter-name>loginCheck</filter-name>
<filter-class>cn.ipanel.tv.bank.filter.LoginCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginCheck</filter-name>
<url-pattern>/adminManagePage/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>loginCheck</filter-name>
<url-pattern>/merchantManagePage/*</url-pattern>
</filter-mapping>
2. 权限验证
个人J2EE项目是基于SSH2开发,所以在所有的操作上,每一个操作都对应一个ActionSupport类的子类(基于struts项目开发的action继承于ActionSupport类),采用struts2大量使用的拦截器机制,编写自己的拦截器类AuthorityCheckInterceptor继承com.opensymphony.xwork2.interceptor.AbstractInterceptor,并同struts2提供的拦截器组成自己的拦截器栈,过滤用户的action操作,即过滤用户的操作权限。
2.1 AuthorityCheckInterceptor类:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import cn.ipanel.tv.bank.bean.AdminBean;
import cn.ipanel.tv.bank.bean.AuthorityBean;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityCheckInterceptor extends AbstractInterceptor
{
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
String result = null;
AdminBean adminBean = (AdminBean) invocation.getInvocationContext()
.getSession().get("loginBean");
Iterator<AuthorityBean> authorityIterator = adminBean
.getAuthorityBeans().iterator();
List<String> authorityNameList = new LinkedList<String>();
while (authorityIterator.hasNext())
{
authorityNameList.add(authorityIterator.next().getAuthorityName());
}
if (authorityNameList.contains(invocation.getAction().getClass()
.getSimpleName()))
{
result = invocation.invoke();
}
else
{
result = Action.ERROR;
}
return result;
}
}
(附录补充说明,写自己的拦截器类就是实现拦截器的intercept方法,而这个方法中的参数ActionInvocation类型的invocation,既能通过invocation的getInvocationContext方法获取Jsp的默认aplication、session属性,还能通过invocation的getAction方法获取此次拦截的Action的实际类名)
2.2 拦截器在J2EE项目中的配置放在自己编写的struts.xml文件中:
<package name="adminManagePage" extends="struts-default" namespace="/adminManagePage">
<interceptors>
<interceptor name="AuthorityCheckInterceptor" class="authorityCheck">
<param name=""></param>
</interceptor>
<interceptor-stack name="my-defaultStack">
<interceptor-ref name="AuthorityCheckInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="my-defaultStack"/>
<global-results>
<result name="error">adminNoAuthority.jsp</result>
</global-results>
<action name="adminAllList" class="adminList" method="listAllAdmin">
<result name="success">adminAllList.jsp</result>
</action>
<action name="adminAuthorityList" class="adminList" method="listAdminAuthority">
<result name="success">adminAllList.jsp</result>
</action>
...(后续还有类似的大量<action ...><result...></result></action>)...