发表于: 2020-05-14 23:58:10
1 1401
今天完成的事情
登录改成post了
又看了下form表单post get区别
get url是这样的 保密性不好 直接就看到了 不能用
http://www.jnshu.com/daily?username=xx&password=xx
post时直接就提交一个对象
保密性更好一些
配置了拦截器
springmvc.xml
<!-- 拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器,顺序执行 -->
<!-- 登录认证拦截器 -->
<mvc:interceptor>
<!--/** 表示所有url包括子url路径 -->
<mvc:mapping path="/u/**"/>
<bean class="com.mb.Interceptor.LoginInterceptor"/>
<!--设置拦截器路径-->
</mvc:interceptor>
</mvc:interceptors>
public class LoginInterceptor implements HandlerInterceptor {
Logger logger = Logger.getLogger(LoginInterceptor.class);
//在请求之前被调用
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
// 请求cookie里的信息,放入cookies
Cookie[] cookies = httpServletRequest.getCookies();
//如果cookies的长度等于0,表示没有cookies,返回登录页面,让用户登录一下下,发给用户一个cookie,下次光临的时候用户就有cookie了
if (cookies.length == 0) {
logger.info("没有cookie");
} else {
// 用户有cookies
logger.info("有cookie");
//遍历一下用户的cookies,放入cookie,看看他的cookie是什么呀
for (Cookie cookie : cookies) {
//如果他的cookie与我发给他的cookie相等,就可以直接返到页面里
if (cookie.getName().equals("name")) {
logger.info("cookie内容(name) is:" + cookie.getValue());
return true;
}
}
}
// 返回登录页面
httpServletResponse.sendRedirect("/beforeLogin");
return false;
}
//在业务处理器处理请求完成之后,生成视图之前执行
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception{
}
//在DispatcherServlet完全处理完请求之后被调用,可用于清理资源
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception{
}
拦截文件
先登录
进去了
再进就不用登陆了 就是css js每加载出来...??
登录默认显示的cookie内容
从网上复制了
md5 des的加密代码
@Component
public class DesUtil {
private static final String ENCRYPT_TYPE = "DES";
private static String defaultKey = "";// 字符串默认键值
private Cipher encryptCipher = null;// 加密工具
private Cipher decryptCipher = null;// 解密工具
public DesUtil() throws Exception {
this(defaultKey);
}
/**
* 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public DesUtil(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance(ENCRYPT_TYPE);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance(ENCRYPT_TYPE);
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* 加密字节数组
*
* @param arr
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
private byte[] encryptStr(byte[] arr) throws Exception {
return encryptCipher.doFinal(arr);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return StrConvertUtil.byteArrToHexStr(encryptStr(strIn.getBytes()));
}
/**
* 解密字节数组
*
* @param arr
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
private byte[] decryptStr(byte[] arr) throws Exception {
return decryptCipher.doFinal(arr);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decryptStr(StrConvertUtil.hexStrToByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位。不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
*/
private Key getKey(byte[] arrBTmp) {
byte[] arrB = new byte[8];// 创建一个空的8位字节数组(默认值为0)
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, ENCRYPT_TYPE);// 生成密钥
return key;
}
@Component
public class Md5Util {
public final static String getMd5(String str) {
MessageDigest mdInst = null;
try {
mdInst = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
mdInst.update(str.getBytes());// 使用指定的字节更新摘要
byte[] md = mdInst.digest();// 获得密文
return StrConvertUtil.byteArrToHexStr(md);
}
}
不过还没用 因为任务
所需要加密解密还没写
明天计划的事情:
继续任务
评论