前端部分代码省略(自个复习用),学个项目,日常记录一下
- 怕的就是后台程序员也知道用户的密码,MD5是一种安全性比较好的加密方式
- 用户点击登陆的时候,向后台在MVC控制层提交账号和密码请求
- 控制层代码
@RequestMapping("/admin/do/login.html")
public String doLogin(@RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session) {
// 调用Service方法执行登陆检查
// 这个方法如果能够返回admin对象说明登陆成功,如果账号或者密码不正确则会抛出异常(找自定义的处理机制)
Admin admin = adminService.getAdminByLoginAct(loginAcct, userPswd);
// 将登陆成功的admin对象存入Session域
session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
// 转发抽象刷新一下就会抽象提交表单,查询数据库。所以使用重定向
// 重定向是浏览器给服务器发一个请求,服务器响应告诉浏览器这事儿没完,
// 你还需要访问另一个地址,浏览器访问的地址访问不到web-info,所以得经过mvc转发一下
// 没有携带数据,直接view-controller
return "redirect:/admin/to/main/page.html";
}
- 在service层中进行处理账号查询
@Override
public Admin getAdminByLoginAct(String loginAcct, String userPswd) {
// 1.根据登陆账号查询Admin对象
// a.创建AdminExample对象
AdminExample adminExample = new AdminExample();
// b.创建Criteria对象
AdminExample.Criteria criteria = adminExample.createCriteria();
// c.在Criteria对象中封装查询条件
criteria.andLoginAcctEqualTo(loginAcct);
// d.调用AdminMapper的方法执行查询
List<Admin> list = adminMapper.selectByExample(adminExample);
// 2.判断Admin对象是否为null
if (list == null || list.size() ==0){
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
if (list.size() > 1){
throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
}
Admin admin = list.get(0);
// 3.如果为null则抛出异常
if (admin == null){
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
// 4.如果对象不为null将数据库密码从admin对象取出
String userPswdDB = admin.getUserPswd();
// 5.将表单提交的密码进行加密
String userPswdForm = CrowdUtil.md5(userPswd);
// 6.使数据密码和表单密码比较
// Objects这个比较方法的好处是可以避免空指针异常
if (!Objects.equals(userPswdDB,userPswdForm)) {
// 7.如果密码不一致则抛出异常
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
// 8.一致则返回Admin对象
return admin;
}
- 自定义异常类
/**
* 登陆失败后抛出的异常
*
* @author 梁承缘
*/
public class LoginFailedException extends RuntimeException {
// 序列化版本号
private static final long serialVersionUID = 1L;
public LoginFailedException() {
super();
}
public LoginFailedException(String message) {
super(message);
}
public LoginFailedException(String message, Throwable cause) {
super(message, cause);
}
public LoginFailedException(Throwable cause) {
super(cause);
}
protected LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
- MD5工具类
/**
* 对明文字符串进行MD5加密
*
* @param source 传入的明文字符串
* @return 返回的加密结果
*/
public static String md5(String source) {
// 1.判断source是否有效 平时多注意,就业少挨骂
if (source == null || source.length() == 0) {
// 2.如果不是有效的字符串则抛出异常
throw new RuntimeException(CrowdConstant.MESSAGE_SIRING_INVALIDATE);
}
// 异常如果是抛给虚拟机或者框架的,就可以不用try
try {
// 3.获取MessageDigest对象 jdk提供的一个加密的工具
String algorithm = "md5";
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
// 4.获取明文字符串对应的字节数组
byte[] input = source.getBytes();
// 5.执行加密
byte[] output = messageDigest.digest(input);
// 6.创建BigInteger对象
int signum = 1; // 1正数 0为0 -1负数
BigInteger bigInteger = new BigInteger(signum, output);
// 7.按照16进制将bigInteger的值转换为字符串
int radix = 16; //指定是多少进制
String encoded = bigInteger.toString(radix).toUpperCase();
return encoded;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
- SpringMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置自动扫描的包,扫描handler控制层-->
<context:component-scan base-package="com.lcy.crowd.mvc"/>
<!-- 配置SpringMVC的注解驱动-->
<mvc:annotation-driven/>
<!-- 配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 基于XML的异常映射 -->
<bean id="simpleMappingExceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 配置异常类型和具体视图页面的对应关系 -->
<property name="exceptionMappings">
<props>
<!--key指异常类型 中间是视图名称 -->
<prop key="java.lang.Exception">system.error</prop>
</props>
</property>
</bean>
<!-- 配置view-controller,直接将请求地址和视图名称关联起来,不必写控制层方法了 -->
<mvc:view-controller path="admin/to/login/page.html" view-name="admin-login"/>
<mvc:view-controller path="admin/to/main/page.html" view-name="admin-main"/>
</beans>