http://www.cnblogs.com/mailingfeng/archive/2011/12/21/2296105.html.
http://www.boyunjian.com/ 拨云剑
http://www.th7.cn/Program/java/201411/313875.shtml
/**
* ----xiaowu
*
* 随机数的生成
* @param nRandomCount 随机数的位数
* @param isNumber ---true:随机数全为数字;false:随机数为数字字母混编;
* @return
*/
public static String getRandomString(int nRandomCount, boolean isNumber) {
char[] a = null;
if (isNumber) {
a = new char[10];
for (int i = 48, j = 0; i <= 57; i++, j++) {
a[j] = (char) i;
}
} else {
// ASCII 48~57,String 0~9;
// ASCII 65~90,String A-Z;
// ASCII 97~122,String a-z;
a = new char[62];
for (int i = 48, j = 0; i <= 122; i++) {
if ((i > 57 && i < 65) || (i > 90 && i < 97)) {
continue;
} else {
a[j] = (char) i;
j++;
}
}
}
String strRand = "";
int LengthOfRandom = a.length;
Random random = new Random();
for (int i = 0; i < nRandomCount; i++) {
int nRand = random.nextInt(LengthOfRandom);
strRand += a[nRand];
}
return strRand;
}
private static String getMD5(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
System.exit(-1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
/**
*
*/
package com.houtai.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.houtai.po.User;
/**
* 自定义拦截器
* @author wuzhq
* @date Nov 5, 2011
*/
public class MyInterceptor extends HandlerInterceptorAdapter {
/**
* 最后执行,可用于释放资源
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
}
/**
* 显示视图前执行
*/
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//System.out.println(request.getContentType()+"-----"+request.getCharacterEncoding()+"------"+request.getContextPath());
//System.out.println("MyInterceptor.postHandle()---viewName:"+modelAndView.getViewName());
super.postHandle(request, response, handler, modelAndView);
}
/**
* Controller之前执行
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURI();
System.out.println("MyInterceptor.preHandle()"+url);
User htUser = (User) request.getSession().getAttribute("htUser");
if(htUser == null || htUser.getId()== null)
{
request.getRequestDispatcher("/ht.jsp").forward(request, response);
return false;
}
/**
String param = request.getParameter("type");
if(param!=null)
{
url= url+"?type="+param;
}
List <Function> list = user.getResourceList();
boolean flag = false;
for(Function func:list)
{
String userUrl = func.getUrl();
if(url.indexOf(userUrl)!=-1||(param!=null&&userUrl.indexOf("type="+param)!=-1))
{
flag = true;
break;
}
}
if(!flag && param!=null){
//request.setAttribute("msg", "您没有此权限!请重新登录");
request.getRequestDispatcher("./index.jsp").forward(request, response);
return false;
}
**/
return super.preHandle(request, response, handler);
}
}
<!-- 这里拦截器还有一种配置方法【针对路径进行配置】 推荐使用这个,方便直观-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/qt/meeting*"/>
<bean class="com.houtai.web.interceptor.MyInterceptorForQt"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/ht/*"/>
<bean class="com.houtai.web.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>