1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SpringBoot后台管理系统框架

SpringBoot后台管理系统框架

时间:2021-10-16 08:47:42

相关推荐

SpringBoot后台管理系统框架

SpringBoot后台管理系统框架

SpringBoot后台管理系统功能介绍

登录 注册 用户列表和添加功能

只是个框架 实现了shiro权限控制, 详细的shiro使用

一个模板项目系统 只有少量功能

使用技术

SpringBoot框架

Mysql数据库

redis

shiro权限

thymeleaf(前端)

功能展示

shiro权限

package com.game.app.shiro;import com.game.app.model.system.Permission;import com.game.app.model.system.Role;import com.game.app.model.system.User;import com.game.app.service.system.AuthService;import com.game.app.service.system.UserService;import mons.codec.digest.DigestUtils;import mons.lang3.builder.ReflectionToStringBuilder;import mons.lang3.builder.ToStringStyle;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.*;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.apache.shiro.subject.Subject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/*** 自定义权限匹配和账号密码匹配*/@Servicepublic class MyShiroRealm extends AuthorizingRealm {private static final Logger logger = LoggerFactory.getLogger(MyShiroRealm.class);@Autowiredprivate AuthService authService;@Autowiredprivate UserService userService;/*** 此方法调用 hasRole,hasPermission的时候才会进行回调.* 权限信息.(授权):* 1、如果用户正常退出,缓存自动清空;* 2、如果用户非正常退出,缓存自动清空;* 3、如果我们修改了用户的权限,而用户不退出系统,修改的权限无法立即生效。* (需要手动编程进行实现;放在service进行调用)* 在权限修改后调用realm中的方法,realm已经由spring管理,所以从spring中获取realm实例,* 调用clearCached方法;* :Authorization 是授权访问控制,用于对用户进行的操作授权,证明该用户是否允许进行当前操作,如访问某个链接,某个资源文件等。*/@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {/** 当没有使用缓存的时候,不断刷新页面的话,这个代码会不断执行,* 当其实没有必要每次都重新设置权限信息,所以我们需要放到缓存中进行管理;* 当放到缓存中时,这样的话,doGetAuthorizationInfo就只会执行一次了,* 缓存过期之后会再次执行。*/logger.debug("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");// 获取当前登陆用户Subject subject = SecurityUtils.getSubject();User user = (User) subject.getPrincipal();// 添加权限 和 角色信息SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();// 根据用户id查询用户的角色List<Role> roles = authService.selectRoleByUserId(user.getId());if (null != roles && roles.size() > 0) {for (Role role : roles) {//添加角色authorizationInfo.addRole(role.getRoleName());//根据角色查询对应权限数据List<Permission> permissions = authService.selectPermissionByRoleId(role.getId());if (null != permissions && permissions.size() > 0) {// 授权角色下所有权限for (Permission permission : permissions) {authorizationInfo.addStringPermission(permission.getName());}}}}return authorizationInfo;}/*** 认证信息.(身份验证)* Authentication 是用来验证用户身份* 主要是用来进行身份认证的,也就是说验证用户输入的账号和密码是否正确*/@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)throws AuthenticationException {logger.debug("用户登录身份认证-->MyShiroRealm.doGetAuthenticationInfo()");//UsernamePasswordToken用于存放提交的登录信息UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;logger.info("用户登录认证:验证当前Subject时获取到token为:" +ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE));String username = token.getUsername();// 调用数据层 查询用户User user = userService.selectByUsername(username);logger.debug("用户登录认证!用户信息user:" + user);if (user == null) {// 用户不存在return null;} else {// 密码存在//盐值加密: 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以自定义实现//SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(//user, //用户名//user.getPassword(), //密码//ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt//getName() //realm name//);//明文: 若存在,将此用户存放到登录认证info中,无需自己做密码对比,Shiro会为我们进行密码对比校验//SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(//user, //用户名//user.getPassword(), //密码//getName() //realm name//);//普通md5: Shiro会为我们进行密码对比校验// 第一个参数 ,登陆后,需要在session保存数据// 第二个参数,查询到密码(加密规则要和自定义的HashedCredentialsMatcher中的HashAlgorithmName散列算法一致)// 第三个参数 ,realm名字SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, //用户DigestUtils.md5Hex(user.getPassword()),getName() //realm name);return authenticationInfo;}}}

运行

创建数据库, 然后修改数据库连接相关信息。

启动 SpringBoot 类的main方法

访问: http://localhost:8080/manage-demo

账号: admin 密码: 654321

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。