源码获取:俺的博客首页 "资源" 里下载!
项目介绍
实现了用户登录与注册,查看首页,查看我的相册,添加相册,提交评论,修改密码等功能
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;
6.是否Maven项目:是;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:html+CSS+JavaScript+bootstrap+layui
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入 http://localhost:8080/ssm_xcselect
系统用户管理控制层:
/**
* 系统用户
*
*/
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
@Autowired
private SysUserService sysUserService;
@Autowired
private SysUserRoleService sysUserRoleService;
/**
* 所有用户列表
*/
@RequestMapping("/list")
@RequiresPermissions("sys:user:list")
public R list(@RequestParam Map<String, Object> params) {
//查询列表数据
Query query = new Query(params);
List<SysUserEntity> userList = sysUserService.queryList(query);
int total = sysUserService.queryTotal(query);
PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
return R.ok().put("page", pageUtil);
}
@RequestMapping("/list2")
public R list2(@RequestParam Map<String, Object> params) {
//查询列表数据
Query query = new Query(params);
List<SysUserEntity> userList = sysUserService.queryList(query);
return R.ok().put("list", userList);
}
/**
* 获取登录的用户信息
*/
@RequestMapping("/info")
public R info() {
return R.ok().put("user", getUser());
}
/**
* 修改登录用户密码
*/
@SysLog("修改密码")
@RequestMapping("/password")
public R password(String password, String newPassword) {
Assert.isBlank(newPassword, "新密码不为能空");
//sha256加密
password = new Sha256Hash(password).toHex();
//sha256加密
newPassword = new Sha256Hash(newPassword).toHex();
//更新密码
int count = sysUserService.updatePassword(getUserId(), password, newPassword);
if (count == 0) {
return R.error("原密码不正确");
}
//退出
ShiroUtils.logout();
return R.ok();
}
@SysLog("修改个人信息")
@RequestMapping("/updateInfo")
public R updateInfo(String mobile, String email) {
getUser().setMobile(mobile);
getUser().setEmail(email);
getUser().setPassword(null);
this.sysUserService.update(getUser());
return R.ok();
}
/**
* 用户信息
*/
@RequestMapping("/info/{userId}")
@RequiresPermissions("sys:user:info")
public R info(@PathVariable("userId") Long userId) {
SysUserEntity user = sysUserService.queryObject(userId);
//获取用户所属的角色列表
List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
user.setRoleIdList(roleIdList);
return R.ok().put("user", user);
}
/**
* 保存用户
*/
@SysLog("保存用户")
@RequestMapping("/save")
@RequiresPermissions("sys:user:save")
public R save(@RequestBody SysUserEntity user) {
ValidatorUtils.validateEntity(user, AddGroup.class);
user.setCreateUserId(getUserId());
sysUserService.save(user);
return R.ok();
}
/**
* 修改用户
*/
@SysLog("修改用户")
@RequestMapping("/update")
@RequiresPermissions("sys:user:update")
public R update(@RequestBody SysUserEntity user) {
ValidatorUtils.validateEntity(user, UpdateGroup.class);
user.setCreateUserId(getUserId());
sysUserService.update(user);
return R.ok();
}
/**
* 删除用户
*/
@SysLog("删除用户")
@RequestMapping("/delete")
@RequiresPermissions("sys:user:delete")
public R delete(@RequestBody Long[] userIds) {
if (ArrayUtils.contains(userIds, 1L) || ArrayUtils.contains(userIds, -1L)) {
return R.error("系统管理员不能删除");
}
if (ArrayUtils.contains(userIds, getUserId())) {
return R.error("当前用户不能删除");
}
sysUserService.deleteBatch(userIds);
return R.ok();
}
}
登录管理控制层:
/**
* 登录相关
*
*/
@Controller
public class SysLoginController {
@Autowired
private Producer producer;
@Autowired
private SysUserService sysUserService;
@RequestMapping("captcha.jpg")
public void captcha(HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//生成文字验证码
String text = producer.createText();
//生成图片验证码
BufferedImage image = producer.createImage(text);
//保存到shiro session
ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
}
/**
* 登录
*/
@ResponseBody
@RequestMapping(value = "/sys/login", method = RequestMethod.POST)
public R login(String username, String password, String captcha) throws IOException {
// String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
// if(!captcha.equalsIgnoreCase(kaptcha)){
// return R.error("验证码不正确");
// }
try {
Subject subject = ShiroUtils.getSubject();
//sha256加密
password = new Sha256Hash(password).toHex();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
} catch (UnknownAccountException e) {
return R.error(e.getMessage());
} catch (IncorrectCredentialsException e) {
return R.error(e.getMessage());
} catch (LockedAccountException e) {
return R.error(e.getMessage());
} catch (AuthenticationException e) {
return R.error("账户验证失败");
}
return R.ok();
}
/**
*/
@ResponseBody
@RequestMapping(value = "/sys/reg", method = RequestMethod.POST)
public R reg(String username, String password, String captcha) throws IOException {
// String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
// if(!captcha.equalsIgnoreCase(kaptcha)){
// return R.error("验证码不正确");
// }
//sha256加密
SysUserEntity user = new SysUserEntity();
user.setUsername(username);
user.setPassword(password);
user.setStatus(1);
List<Long> roles = new ArrayList<>();
roles.add(1L);
user.setRoleIdList(roles);
this.sysUserService.save(user);
return R.ok();
}
/**
* 退出
*/
@RequestMapping(value = "logout", method = RequestMethod.GET)
public String logout() {
ShiroUtils.logout();
return "redirect:login.html";
}
}
文件上传管理控制层:
/**
* 文件上传
*
*/
@RestController
@RequestMapping("file")
public class UploadController extends AbstractController {
public static String[] suffixs = {"IMG", "PNG", "JPG", "JPEG", "GIF", "BPM"};
@Autowired
PicService picService;
/**
* 上传文件
*/
@RequestMapping("/upload")
public R upload(@RequestParam("file") MultipartFile aaa, Long id, HttpServletRequest request) throws Exception {
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
for (MultipartFile f : req.getFiles("file")) {
PicEntity p = new PicEntity();
p.setUrl(MultipartFileUtil.uploadFile("", f, request));
p.setUser(super.getUserId());
p.setShare("0");
p.setView(1d);
p.setTitle(f.getOriginalFilename());
p.setAlbum(id);
this.picService.save(p);
}
return R.ok();
}
@RequestMapping("/upload1")
public R upload1(@RequestParam("file") MultipartFile aaa, Long id, HttpServletRequest request) throws Exception {
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
for (MultipartFile f : req.getFiles("file")) {
return R.ok().put("url", MultipartFileUtil.uploadFile("", f, request));
}
return R.ok();
}
/**
* 上传资讯内容的图片
*
* @param upload 图片
* @param response 响应
*/
@ResponseBody
@RequestMapping("ckEditorUpload")
public void uploadFile(MultipartFile upload, String CKEditorFuncNum, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String path = null;
if (upload != null && !upload.isEmpty()) {
String url = MultipartFileUtil.uploadFile("/cdn", upload, request);
path = url;
}
// 返回“图像”选项卡并显示图片
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",'" + path + "','')");
out.println("</script>");
} catch (RuntimeException e) {
out.println("<script type=\"text/javascript\">");
out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",'','" + e.getMessage() + "');");
out.println("</script>");
}
}
}
源码获取:俺的博客首页 "资源" 里下载!