首先转帖一片关于WebWork使用拦截器进行用户身份校验的问题! (转自:http://yezi.iteye.com/ )
Webwork最大的特点是拦截器 Interceptor ,下面通过这个来做一下关于用户身份验证的webapp。需求很简单,当收到一些请求的时候,校验用户是否已经登陆,如果没登陆,自动导向登陆界面。
在这个功能中,我通过Cookie来实现用户身份的校验,当然,用Session的方法相同,这里就不写了,首先是配置文件 xml
<interceptors> <interceptor name="authcheck" class="com.xxx.core.interceptor.AuthInterceptor"></interceptor> </interceptors> <action name="index" class="com.xxx.webapp.action.IndexAction"> <result name="success">index.jsp</result> <result name="login">login.jsp</result> <interceptor-ref name="authcheck"></interceptor-ref> </action>
配置文件中,配置了一个authcheck的拦截器,然后配置一个ACTION,action中加入拦截器的引入,同时定义跳转所需的页面
下面看一下拦截器的程序
java 代码
public class AuthInterceptor implements Interceptor{
private CacheService cacheService;
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String userID= CookieUtil.getCookieValue(request, Constants.userValidCookieName);
System.out.println("userID:"+userID);
if (userID==null || userID==""){
return Action.LOGIN;
}else{
// 如果用户有Cookie,检测Memcache中是否有User对象
}
return actionInvocation.invoke();
}
}
该拦截器实现Intercept接口,其中验证了用户的Cookie是否存在,如果不存在跳转到Action.LOGIN。 到此,该拦截器的简单功能就实现了,用户在访问index.action的时候,将作统一的用户身份验证。
================================================================
总结:
1.Webwork最大的特点是拦截器 Interceptor;
2.spring核心技术是注入依赖;
3.java的两大技术特点:(1)interface
(2)public private protected