源码已上传至GitHub
后端部分:https://github.com/Wisdom-Bao/shiro-ssm.git
前端部分:https://github.com/Wisdom-Bao/shiro-vue.git
一、登录注册
关键代码:
@RequestMapping("login")
@ResponseBody
public String login(String username,String password){
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
return "success";
} catch (UnknownAccountException e) { //用户名不存在
return "no user";
} catch (IncorrectCredentialsException e) { //密码错误
return "wrong password";
}
}
submitForm(formName) {
this.$http
.get(`/login?username=${formName.username}&password=${formName.pass}`)
.then(resp => {
if (formName.code != this.identifyCode) {
this.$message({
type: "info",
message: "验证码错误"
});
} else if (resp.data == "success") {
sessionStorage.setItem("token", "true");
sessionStorage.setItem("username", formName.username);
sessionStorage.setItem("password", formName.pass);
_this.$router.push({ path: "/" });
this.$message({
type: "success",
message: "登录成功"
});
} else if (resp.data == "wrong password") {
this.$message({
type: "info",
message: "密码错误"
});
_this.$router.push({ path: "/login" });
} else if (resp.data == "no user") {
this.$message({
type: "info",
message: "用户名不存在"
});
_this.$router.push({ path: "/login" });
}
});
}
关键代码:
@RequestMapping("register")
@ResponseBody
public String register(String username, String password, String code){
if(!code.equals(this.messageCode)){
return "wrong code";
}
Object salt = ByteSource.Util.bytes(username);
SimpleHash simpleHash = new SimpleHash("MD5", password, salt, 1);
User user = new User();
user.setName(username);
user.setPassword(simpleHash.toString());
userService.addUser(user);
return "register success";
}
submitForm(formName) {
this.$http
.get(
`/register?username=${formName.username}&password=${formName.pass}&code=${formName.code}`
)
.then(resp => {
console.log(resp);
if (resp.data == "register success") {
this.$message({
type: "success",
message: "注册成功"
});
_this.$router.push({ path: "/" });
}else if(resp.data == "wrong code"){
this.$message({
type: "info",
message: "验证码不正确"
});
}
});
},
二、展示个人信息
关键代码:
@ResponseBody
@RequestMapping("findAllUserInfo")
public List<UserVo> findAllUserInfo(){
List<User> userList = userService.findAllUsers();
List<UserVo> userVoList = new ArrayList<>();
for(User user : userList) {
Set roleSet = new HashSet();
List<Role> roleList = roleService.findRoleByUserId(user.getId());
for (Role role : roleList) {
roleSet.add(role.getName());
}
UserVo userVo = new UserVo();
userVo.setUserId(user.getId());
userVo.setUserName(user.getName());
userVo.setUserRoles(roleSet);
userVoList.add(userVo);
}
return userVoList;
}
以下几个部分主要就是一些crud,连接数据库操作就行了
三、角色管理
1、显示所有角色
2、显示角色详细信息
3、修改角色信息
3、删除角色
4、添加角色
三、权限管理
功能与角色管理类似,就不一一演示了
四、用户管理
1、展示用户
2、修改用户角色
五、用户权限不足情况
如果用户没有相应的权限则无法访问对应的内容
转载:https://blog.csdn.net/qq_44042316/article/details/106528251
查看评论