The source code has been uploaded to GitHub
Back-end part: https://github.com/Wisdom-Bao/shiro-ssm.git
Front-end part: https://github.com/Wisdom-Bao/shiro-vue.git
1. Log in and register
Key code:
@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" });
}
});
}
Key code:
@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: "验证码不正确"
});
}
});
},
2. Display personal information
Key code:
@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;
}
The following parts are mainly crud, just connect to the database operation
Three, role management
1. Show all roles
2. Show detailed information about roles
3. Modify role information
3. Delete the role
4. Add roles
Three, authority management
Functions are similar to role management, so I won’t demonstrate them one by one
Four, user management
1. Show users
2. Modify user roles
Five, insufficient user authority
If the user does not have the corresponding permission, he cannot access the corresponding content