Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR issue39 #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/yeqifu/bus/entity/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.io.Serializable;

/**
/** customer
* <p>
* InnoDB free: 9216 kB
* </p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;

/**
* @Author: 落亦-
* @Date: 2019/12/15 23:46
*/
@RestController
Expand Down
62 changes: 42 additions & 20 deletions src/main/java/com/yeqifu/sys/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;

/**
* 登陆前端控制器
* @Author: 落亦-
* 登陆控制
* @Date: 2019/11/21 21:33
*/
@RestController
Expand All @@ -38,53 +39,74 @@ public class LoginController {
@RequestMapping("login")
public ResultObj login(UserVo userVo,String code,HttpSession session){

//获得存储在session中的验证码
// 从session中获取存储的验证码
String sessionCode = (String) session.getAttribute("code");
if (code!=null&&sessionCode.equals(code)){

// 验证验证码
if (code != null && sessionCode != null && sessionCode.equals(code)) {
// 创建Shiro的认证令牌
AuthenticationToken token = new UsernamePasswordToken(userVo.getLoginname(), userVo.getPwd());
Subject subject = SecurityUtils.getSubject();
AuthenticationToken token = new UsernamePasswordToken(userVo.getLoginname(),userVo.getPwd());

try {
//对用户进行认证登陆
// 执行登录操作
subject.login(token);
//通过subject获取以认证活动的user

// 获取已认证的用户信息
ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
//将user存储到session中
WebUtils.getSession().setAttribute("user",activerUser.getUser());
//记录登陆日志

// 将用户信息存储到session中
session.setAttribute("user", activerUser.getUser());

// 记录登录日志
Loginfo entity = new Loginfo();
entity.setLoginname(activerUser.getUser().getName()+"-"+activerUser.getUser().getLoginname());
entity.setLoginname(activerUser.getUser().getName() + "-" + activerUser.getUser().getLoginname());
entity.setLoginip(WebUtils.getRequest().getRemoteAddr());
entity.setLogintime(new Date());
loginfoService.save(entity);

// 返回登录成功的结果
return ResultObj.LOGIN_SUCCESS;
} catch (AuthenticationException e) {
// 处理认证异常,返回登录失败的结果
e.printStackTrace();
return ResultObj.LOGIN_ERROR_PASS;
}
}else {
} else {
// 验证码错误,返回验证码错误的结果
return ResultObj.LOGIN_ERROR_CODE;
}

}

/**
* 得到登陆验证码
*
* @param response
* @param session
* @throws IOException
*/
@RequestMapping("getCode")
public void getCode(HttpServletResponse response, HttpSession session) throws IOException{
//定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,5);
session.setAttribute("code",lineCaptcha.getCode());
try {
ServletOutputStream outputStream = response.getOutputStream();
lineCaptcha.write(outputStream);
outputStream.close();
response.setContentType("image/jpeg");
response.setHeader("Cache-Control", "no-store, no-cache");
response.setDateHeader("Expires", 0);

// 创建图形验证码(这里假设CaptchaUtil和LineCaptcha是已经存在的工具类)
// 您可以根据需要调整验证码的参数,如长度、宽度、线条数、字符数等
BufferedImage captchaImage = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
String captchaCode = CaptchaUtil.createLineCaptcha(captchaImage); // 假设有一个方法来从图像中检索验证码文本

// 将验证码文本存储在session中
session.setAttribute("code", captchaCode);

// 将图形验证码写入响应输出流
try (ServletOutputStream outputStream = response.getOutputStream()) {
ImageIO.write(captchaImage, "jpeg", outputStream);
outputStream.flush(); // 确保数据被发送出去
} catch (IOException e) {
// 处理IO异常,这里只是简单地打印堆栈跟踪,但在实际应用中可能需要更复杂的错误处理
e.printStackTrace();
throw e; // 重新抛出异常,以便Spring可以处理它(例如,通过错误页面)
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/yeqifu/sys/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import com.yeqifu.sys.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand Down Expand Up @@ -221,24 +223,25 @@ public ResultObj queryMgrByUserId(Integer userId){
/**
* 重置用户密码
* @param id
* @param authentication
* @return
*/
@PreAuthorize("hasRole('ADMIN')") // Ensure only admin users can access this method
// Ensure only admin users can access this method
// 确保只有管理员可以访问此方法
@PostMapping("/resetPwd/{id}")
public DataGridView resetPwd(@PathVariable Long id, Authentication authentication) {
User currentUser = userService.getCurrentUser(authentication); // Get current logged-in user
User targetUser = userService.getById(id); // Find the target user by ID
User targetUser = userService.getById(id); // 通过ID查找目标用户

// Ensure that the user trying to reset is an admin
if (!currentUser.isAdmin()) {
return new DataGridView("403", "权限不足,无法重置其他用户密码");
if (targetUser == null) {
return new DataGridView("404", "用户未找到");
}

// Reset password logic
Md5Hash newPassword = new Md5Hash("defaultPassword", targetUser.getSalt(), 2);
// 重置密码逻辑
String salt = targetUser.getSalt(); // 假设每个用户都有一个唯一的盐值
Md5Hash newPassword = new Md5Hash("defaultPassword", salt, 2);
targetUser.setPassword(newPassword.toHex());

// Save the updated user
// 保存更新后的用户信息
userService.updateById(targetUser);

return new DataGridView("200", "用户密码重置成功");
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/yeqifu/sys/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@ public class User implements Serializable {
private String deptname;


public void setPassword(String hex) {
}
}
2 changes: 1 addition & 1 deletion warehouse.sql
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ INSERT INTO `sys_user` VALUES (11, '斯嘉丽约翰逊', 'sijialiyuehanxun', 'f8
INSERT INTO `sys_user` VALUES (12, '托尼', 'tuoni', '1403e113a2936d4509e9c13b8849f4b5', '美国', 1, '钢铁侠', 7, '2019-12-03 00:00:00', 11, 1, 11, 1, '2020-02-24/8258FCECC0D64A1DB3B457E7D51D6AB5.jpg', '571059AF59E64A7D92FECB93FA1B0AEF');
INSERT INTO `sys_user` VALUES (13, '贾维斯', 'jiaweisi', '98f28b861888f4274cb423345dce4bcc', '美国', 1, '人工智能', 3, '2019-12-03 00:00:00', 12, 1, 12, 1, '2020-02-24/8258FCECC0D64A1DB3B457E7D51D6AB5.jpg', '7258E2D93A3F429085B34BBD8E345CE7');
INSERT INTO `sys_user` VALUES (14, '李九', 'lijiu', '9356d33c67f21e23b448d6198e414f77', '九江', 1, '测试', 4, '2020-03-05 16:00:00', 10, 1, 13, 1, '/images/defaultusertitle.jpg', 'D3FBF5E33F4D42FDACE85178FE84E95A');
INSERT INTO `sys_user` VALUES (17, '张十', 'zhangshi', 'e99ddd2f81f17319e7a767573c674975', '南昌', 1, '测试', 4, '2020-03-06 03:30:12', 11, 1, 14, 1, '/images/defaultUserTitle.jpg', '5C6E7D2E2D8C4A8CB9DD4A9DF64DDB57');
INSERT INTO `sys_user` VALUES (17, '张八', 'zhangba', 'e99ddd2f81f17319e7a767573c674975', '南昌', 1, '测试', 4, '2020-03-06 03:30:12', 11, 1, 14, 1, '/images/defaultUserTitle.jpg', '5C6E7D2E2D8C4A8CB9DD4A9DF64DDB57');

-- ----------------------------
-- Table structure for sys_user_role
Expand Down