SpringBoot后台管理系统框架
生活随笔
收集整理的這篇文章主要介紹了
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 org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.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;/*** 自定義權限匹配和賬號密碼匹配*/ @Service public 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
總結
以上是生活随笔為你收集整理的SpringBoot后台管理系统框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python if continue的用
- 下一篇: 自定义分页信息java_java-dis