public static String md5(String source) {
//判断source是否生效
if (source == null || source.length() == 0) {
//不是有效的数据
throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE);
}
String algorithm = "md5";
//获取MessageDigest对象
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
// 获取明文字符串对应的字节数组
byte[] input = source.getBytes();
// 执行加密
byte[] output = messageDigest.digest(input);
// 创建BigInterger对象
int signum = 1;
BigInteger bigInteger = new BigInteger(signum, output);
// 按照十六进制将bigInteger的值转换成字符串
int base = 16;
String encoded = bigInteger.toString(base).toUpperCase();
return encoded;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
package com.songzhishu.crowd.exception;
/**
* @BelongsProject: CrowdFunding-parent
* @BelongsPackage: com.songzhishu.crowd.exception
* @Author: 斗痘侠
* @CreateTime: 2023-10-29 15:32
* @Description: 登录失败异常
* @Version: 1.0
*/
public class LoginFailedException extends RuntimeException {
private static final long serialVersionUID = 1577454949343343608L;
public LoginFailedException() {
}
public LoginFailedException(String message) {
super(message);
}
public LoginFailedException(String message, Throwable cause) {
super(message, cause);
}
public LoginFailedException(Throwable cause) {
super(cause);
}
public LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
//登录异常
@ExceptionHandler(value = LoginFailedException.class)
public ModelAndView resolveNullPointerException(LoginFailedException exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
String viewName = "admin-longin";
return commonResolve(viewName, exception, request, response);
}
${requestScope.exception.message}
@Controller
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping(value = "/admin/do/login.html")
public String doLogin(@RequestParam("loginAcct") String loginAcct,
@RequestParam("userPswd") String userPswd,
HttpSession session
) {
// 调用登录检查的方法 返回admin
Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
// 将登录成功的数据存入session域
session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
//跳转后台主页面
return "admin-main";
}
}
@Override
public Admin getAdminByLoginAcct(String loginAcct, String userPswd) {
// 1查询用户
// 1.1创建adminExample对象
AdminExample adminExample = new AdminExample();
// 1.2创建criteria对象
AdminExample.Criteria criteria = adminExample.createCriteria();
// 1.3 在criteria中添加条件
criteria.andLoginAcctEqualTo(loginAcct);
List adminList = adminMapper.selectByExample(adminExample);
// 判断用户
if (adminList == null||adminList.size()==0) {
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
if (adminList.size()>1) {
//数据错误
throw new LoginFailedException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
}
Admin admin = adminList.get(0);
if (admin == null) {
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
// 获取密码
String userPswdDBMD5 = admin.getUserPswd();
// 加密
String userPswdFormMD5 = CrowdUtil.md5(userPswd);
// 比较
if (!(Objects.equals(userPswdDBMD5,userPswdFormMD5))){
throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
}
//返回数据
return admin;
}
修改控制层代码:为了避免跳转到后台主页面再刷新浏览器导致重复提交登录表单,重定向到目标页面。
//跳转后台主页面
return "redirect:/admin/to/main/page.html";
使用视图控制器是因为,这个页面的访问不需要数据的,直接进行跳转就可以!
这里遇见一个小问题就是,跳转后的页面的样式没有生效,然后我以为是可能和浏览器的缓存什么的也有关系,所以就清除数据,然后发现没有效果,就是不理解问什么找不到资源,然后网上查资料说是在配置SpringMVC中的前端控制器将所有的静态资源都给屏蔽啦,然后就导致数据不能正常的访问,然后我记得我也设置啦注解驱动,教程讲要加上一个
但是加上后不起作用,然后我脑袋突然开窍,我定义啦一个base标签,我访问css资源的标签写在这个base标签上,然后导致找不到数据,哈哈哈哈,以后找不到数据的话可以试试绝对路径!
将部分资源保护起来,让没有登录的请求不能访问。
/**
* @BelongsProject: CrowdFunding-parent
* @BelongsPackage: com.songzhishu.crowd.mvc.interceptor
* @Author: 斗痘侠
* @CreateTime: 2023-10-30 11:32
* @Description: 登录拦截器
* @Version: 1.0
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
/**
* @description: 控制器之前执行
* @author: 斗痘侠
* @date: 2023/10/30 11:35
* @param: null
* @return: null
**/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 检测是否登录 获取session中的数据
HttpSession session = request.getSession();
Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
// 判断
if (admin == null) {
throw new AccessForbiddenException(CrowdConstant.MESSAGE_LOGIN_FORBIDEN);
}
// 不为空 放行
return true;
}
}
package com.songzhishu.crowd.exception;
/**
* @BelongsProject: CrowdFunding-parent
* @BelongsPackage: com.songzhishu.crowd.exception
* @Author: 斗痘侠
* @CreateTime: 2023-10-30 11:42
* @Description: 表示用户没有登录就访问受保护的资源时的异常
* @Version: 1.0
*/
public class AccessForbiddenException extends RuntimeException{
private static final long serialVersionUID = -1279033257779871422L;
public AccessForbiddenException() {
super();
}
public AccessForbiddenException(String message) {
super(message);
}
public AccessForbiddenException(String message, Throwable cause) {
super(message, cause);
}
public AccessForbiddenException(Throwable cause) {
super(cause);
}
protected AccessForbiddenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
导入依赖
com.github.pagehelper
pagehelper
5.3.3
配置
mysql
true
气死啦这个配置,
mapper:
service
@Override
public PageInfo getPageInfo(String keyword, Integer pageNum, Integer pageSize) {
// 开启分页插件
PageHelper.startPage(pageNum,pageSize);
// 调用mapper
List adminList= adminMapper.selectAdminByKeyword(keyword);
// 将数据封装到PageInfo
return new PageInfo<>(adminList);
}
controller
@RequestMapping(value = "/admin/get/page.html")
public String getPageInfo(@RequestParam(value = "keyword", defaultValue = "") String keyword,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
ModelMap modelMap
) {
// 获取pageInfo
PageInfo pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
// 存入模型
modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
return "admin-page";
}
jsp
<%--没有数据--%>
没有数据
<%--有数据--%>
${myStatus.count}
${admin.loginAcct}
${admin.userName}
${admin.email}
写这个的时候要使用jstl标签,所以使用之前要先导入jstl的jar包,问题来了他有三个,导入哪一个呐,啧啧啧多试试就知道啦!
导入js、css后处理前端页面;
显示
jsp
<%--条件查询--%>
这样写只能查询一次,也就是说这在点击分页导航条的时候就不携带查询的关键字啦!
window.location.href = "admin/get/page.html?pageNum=" + pageNum+ "&keyword=${param.keyword}";
jsp