成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術文章
文章詳情頁

SpringBoot配置shiro安全框架的實現

瀏覽:111日期:2023-03-14 18:03:14

首先引入pom

<!--SpringBoot 2.1.0--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!-- shiro-redis --> <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.1.0</version> </dependency> <!-- shiro-freemarker-tag --> <dependency> <groupId>net.mingsoft</groupId> <artifactId>shiro-freemarker-tags</artifactId> <version>1.0.0</version> </dependency> <!-- freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>

ShiroConfig.java

package com.jx.cert.web.framework.config.shiro;import java.util.LinkedHashMap;import java.util.Map;import javax.servlet.Filter;import org.apache.shiro.authc.credential.HashedCredentialsMatcher;import org.apache.shiro.cache.MemoryConstrainedCacheManager;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.session.mgt.SessionManager;import org.apache.shiro.spring.LifecycleBeanPostProcessor;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.crazycake.shiro.RedisCacheManager;import org.crazycake.shiro.RedisManager;import org.crazycake.shiro.RedisSessionDAO;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.DependsOn;import com.jx.cert.web.framework.config.shiro.filter.KickoutSessionControlFilter;import com.jx.cert.web.framework.config.shiro.filter.ShiroPermissionsFilter;import com.jx.cert.web.framework.config.shiro.filter.SystemLogoutFilter;import com.jx.common.utils.CacheConstants;/*** @ClassName: gyu * @Description: TODO(shrio配置) * @author gangyu* @date 2018年12月4日 下午2:28:07 */@Configurationpublic class ShiroConfig { Logger log=LoggerFactory.getLogger(ShiroConfig.class); @Value('${spring.redis.host}') private String host; @Value('${spring.redis.prot}') private int port; @Value('${spring.redis.timeout}') private int timeout; @Value('${spring.redis.password}') private String password; @Value('${spring.redis.database}') private int database; //注意這里需要設置成 static 否則 @Value 注入不了數據 @Bean(name = 'lifecycleBeanPostProcessor') public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {return new LifecycleBeanPostProcessor(); } @Bean(name = 'shiroFilter') public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); log.debug('-----------------Shiro攔截器工廠類注入開始'); Map<String,Filter> filterMap=shiroFilterFactoryBean.getFilters(); //權限過濾器 filterMap.put('perms', new ShiroPermissionsFilter()); shiroFilterFactoryBean.setFilters(filterMap); // 配置shiro安全管理器 SecurityManager shiroFilterFactoryBean.setSecurityManager(securityManager); // 指定要求登錄時的鏈接 shiroFilterFactoryBean.setLoginUrl('/login'); // 登錄成功后要跳轉的鏈接 shiroFilterFactoryBean.setSuccessUrl('/index'); // filterChainDefinitions攔截器=map必須用:LinkedHashMap,因為它必須保證有序 Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(); //對外應用開發接口不驗證 filterChainDefinitionMap.put('/app/**', 'anon');filterChainDefinitionMap.put('/ajaxLogin', 'anon');// 放開驗證碼 filterChainDefinitionMap.put('/public/getGifCode', 'anon'); // 退出 filterChainDefinitionMap.put('/logout', 'logout'); //TODO 全部放行// filterChainDefinitionMap.put('/manage/**', 'anon[*]'); //公共資源filterChainDefinitionMap.put('/static/**', 'anon');filterChainDefinitionMap.put('/css/**', 'anon');filterChainDefinitionMap.put('/img/**', 'anon');filterChainDefinitionMap.put('/js/**', 'anon'); // authc:所有url都必須認證通過才可以訪問; filterChainDefinitionMap.put('/**', 'authc'); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); log.debug('-----------------Shiro攔截器工廠類注入成功'); return shiroFilterFactoryBean; }@Bean public HashedCredentialsMatcher hashedCredentialsMatcher() {HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName('MD5');//散列算法:這里使用MD5算法;hashedCredentialsMatcher.setHashIterations(1);//散列的次數,比如散列兩次,相當于 md5(md5(''));return hashedCredentialsMatcher; } //權限緩存到內存 @Bean(name = 'shiroRealm') @DependsOn('lifecycleBeanPostProcessor') public MyShiroRealm myShiroRealm() {MyShiroRealm myShiroRealm = new MyShiroRealm();myShiroRealm.setCacheManager(new MemoryConstrainedCacheManager());myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());return myShiroRealm; }@Bean(name = 'securityManager') public SecurityManager securityManager() {DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(myShiroRealm());// 自定義session管理 使用redissecurityManager.setSessionManager(sessionManager());// 自定義緩存實現 使用redissecurityManager.setCacheManager(cacheManager());return securityManager; }/** * 配置shiro redisManager * @return */ public RedisManager redisManager() {RedisManager redisManager = new RedisManager();redisManager.setHost(host);redisManager.setPort(port);redisManager.setTimeout(timeout);//redisManager.setPassword(password);redisManager.setDatabase(database);return redisManager; }//自定義sessionManager @Bean public SessionManager sessionManager() {MySessionManager mySessionManager = new MySessionManager();mySessionManager.setSessionDAO(redisSessionDAO());//默認1個小時session過期mySessionManager.setGlobalSessionTimeout(CacheConstants.SHIRO_SESSION_MS);return mySessionManager; } /** * RedisSessionDAO shiro sessionDao層的實現 通過redis */ @Bean public RedisSessionDAO redisSessionDAO() {RedisSessionDAO redisSessionDAO = new RedisSessionDAO();redisSessionDAO.setRedisManager(redisManager());return redisSessionDAO; } /** * cacheManager 緩存 redis實現 * @return */ @Bean public RedisCacheManager cacheManager() {RedisCacheManager redisCacheManager = new RedisCacheManager();redisCacheManager.setRedisManager(redisManager());redisCacheManager.setExpire(CacheConstants.USER_DATA_TTL);return redisCacheManager; } }

MyShiroRealm.java

package com.jx.cert.web.framework.config.shiro;import java.util.ArrayList;import java.util.List;import org.apache.commons.codec.digest.DigestUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.LockedAccountException;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authc.UnknownAccountException;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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import com.jx.cert.web.framework.config.shiro.exception.SysUsreNotLoginAPPException;import com.jx.cert.web.framework.config.shiro.exception.SystemNotExistException;import com.jx.common.utils.SysCode;import com.jx.core.api.model.vo.cert.SysPermission;import com.jx.core.api.model.vo.cert.SysRole;import com.jx.core.api.model.vo.cert.SysSystem;import com.jx.core.api.model.vo.cert.SysUser;import com.jx.core.api.service.business.cert.SysPermissionService;import com.jx.core.api.service.business.cert.SysRoleService;import com.jx.core.api.service.business.cert.SysSystemService;import com.jx.core.api.service.business.cert.SysUserService;public class MyShiroRealm extends AuthorizingRealm { private Logger logger = LoggerFactory.getLogger(MyShiroRealm.class); @Autowired private SysUserService sysUserService; @Autowired private SysRoleService sysRoleService; @Autowired private SysPermissionService sysPermissionService; @Autowired private SysSystemService systemService; @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {logger.info('####################開始配置權限####################');SysUser user = (SysUser) principals.getPrimaryPrincipal();if (user != null) { //權限信息對象info,用來存放查出的用戶的所有的角色(role)及權限(permission) SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); List<String> roleStrlist = new ArrayList<String>();//用戶的角色集合 List<String> perminsStrlist = new ArrayList<String>();//用戶的菜單權限集合 for (SysRole role : user.getRoleList()) {roleStrlist.add(role.getRoleName()); } for (SysPermission permission : user.getPermissList()) {perminsStrlist.add(permission.getUrl()); } //用戶的角色集合 authorizationInfo.addRoles(roleStrlist); //用戶的菜單按鈕權限集合 authorizationInfo.addStringPermissions(perminsStrlist); return authorizationInfo;}return null; } /*主要是用來進行身份認證的,也就是說驗證用戶輸入的賬號和密碼是否正確。*/ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {logger.info('####################身份認證####################');String userStr = (String) token.getPrincipal(); SysUser user = sysUserService.getUserByUserName(userName); //認證系統用戶List<SysRole> roleList = sysRoleService.findRoleByUserId(user.getUserId(),systemId);user.setRoleList(roleList);//獲取用戶角色List<SysPermission> list=sysPermissionService.getUserPermission(user.getUserId(),systemId);SysPermission permis=new SysPermission();list.add(permis);user.setPermissList(list);//獲取用戶權限 return new SimpleAuthenticationInfo(user, DigestUtils.md5Hex(user.getPassword()),getName()); }}

ShiroTagsFreeMarkerCfg.java

package com.jx.cert.web.framework.config.shiro;import javax.annotation.PostConstruct;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;import com.jagregory.shiro.freemarker.ShiroTags;import freemarker.template.TemplateModelException;/** * @ClassName: ShiroTagsFreeMarkerCfg * @Description: TODO(ftl shiro 標簽) * @author gangyu* @date 2018年12月5日 下午5:16:27 * */@Componentpublic class ShiroTagsFreeMarkerCfg { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @PostConstruct public void setSharedVariable() throws TemplateModelException {freeMarkerConfigurer.getConfiguration().setSharedVariable('shiro', new ShiroTags()); }}

ShiroPermissionsFilter.java

/** * @Title: ShiroPermissionsFilter.java * @Package com.jx.cert.web.config.shiro * @Description: TODO(用一句話描述該文件做什么) * @author gangyu* @date 2018年12月5日 上午11:47:00 * @version V1.0 */package com.jx.cert.web.framework.config.shiro.filter;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.gson.Gson;import com.jx.cert.web.framework.config.shiro.ShiroUtil;import com.jx.common.utils.Result;import com.jx.common.utils.enums.CodeEnum;import com.jx.core.api.model.vo.cert.SysPermission;import com.jx.core.api.model.vo.cert.SysUser;/** * @ClassName: ShiroPermissionsFilter * @Description: TODO(權限驗證) * @author gangyu* @date 2018年12月5日 上午11:47:00 */public class ShiroPermissionsFilter extends PermissionsAuthorizationFilter { private static final Logger logger = LoggerFactory.getLogger(ShiroPermissionsFilter.class);/** * 權限檢測驗證 */ @Override protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException {logger.info('----------權限校驗-------------');HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;String reqUrl=request.getRequestURI();List<SysPermission> permsList=ShiroUtil.getUser().getPermissList();String contextPath=request.getContextPath();reqUrl=reqUrl.substring(contextPath.length()+1, reqUrl.length());String header = request.getHeader('X-Requested-With');boolean isAjax = 'XMLHttpRequest'.equals(header);SysUser user=ShiroUtil.getUser(); if(!new Gson().toJson(permsList).contains(reqUrl)){ if (isAjax) {logger.info('----------AJAX請求拒絕-------------');response.setCharacterEncoding('UTF-8');response.setContentType('application/json');response.getWriter().write(new Gson().toJson(new Result(CodeEnum.NOT_PERMISSION))); } else {logger.info('----------普通請求拒絕-------------');response.sendRedirect(request.getContextPath()+'/403'); } return false;}else { return true;} }}

ShiroUtil.java

package com.jx.cert.web.framework.config.shiro;import org.apache.shiro.SecurityUtils;import org.apache.shiro.session.Session;import org.apache.shiro.subject.Subject;import com.jx.core.api.model.vo.cert.SysUser;public class ShiroUtil { /** * 獲取當前 Subject * * @return */ public static Subject getSubject() {return SecurityUtils.getSubject(); } /** * 獲取shiro指定的sessionKey * * @param key * @param <T> * @return */ public static <T> T getSessionAttr(String key) {Session session = getSession();return session != null ? (T) session.getAttribute(key) : null; } /** * 設置shiro指定的sessionKey * * @param key * @param value */ public static void setSessionAttr(String key, Object value) {Session session = getSession();session.setAttribute(key, value); } /** * 獲取當前用戶對象 * * @return */ public static SysUser getUser() { if(getSubject().isAuthenticated()){ return (SysUser) getSubject().getPrincipals().getPrimaryPrincipal(); } return null; } /** * 獲取當前用戶對象UserID * * @return */ public static String getUserId() {return getUser().getUserId(); } /** * 移除shiro指定的sessionKey * * @param key */ public static void removeSessionAttr(String key) {Session session = getSession();if (session != null) session.removeAttribute(key); } /** * 驗證當前用戶是否屬于該角色 * * @param roleName 角色名稱 * @return 屬于該角色:true,否則false */ public static boolean hasRole(String roleName) {return getSubject() != null && roleName != null&& roleName.length() > 0 && getSubject().hasRole(roleName); } /** * shiro 中獲取session * * @return session */ public static Session getSession() {return getSubject().getSession(); } /** * 驗證當前用戶是否屬于以下所有角色 * 多權限“,”分割 * * @param roleNames 權限名稱 * @return 屬于:true,否則false */ public static boolean hasAllRoles(String roleNames) {boolean hasAllRole = true;Subject subject = getSubject();if (subject != null && roleNames != null && roleNames.length() > 0) { for (String role : roleNames.split(',')) {if (!subject.hasRole(role.trim())) { hasAllRole = false; break;} }}return hasAllRole; } /** * 驗證當前用戶是否屬于以下任意一個角色 * 多權限“,”分割 * @param roleNames * @return */ public static boolean hasAnyRoles(String roleNames) {boolean hasAnyRole = false;Subject subject = getSubject();if (subject != null && roleNames != null && roleNames.length() > 0) { for (String role : roleNames.split(',')) {if (subject.hasRole(role.trim())) { hasAnyRole = true; break;} }}return hasAnyRole; }}

到此這篇關于SpringBoot配置shiro安全框架的實現的文章就介紹到這了,更多相關SpringBoot配置shiro內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
亚洲激情在线激情| 欧美日韩一区 二区 三区 久久精品| 91成人免费网站| 亚洲成人免费视| 日韩午夜在线电影| 中文字幕在线不卡一区二区三区| 99久久er热在这里只有精品15| 91精品国产一区二区| 久久草av在线| 日本久久电影网| 日韩av一区二区三区| 蘑菇福利视频一区播放| 亚洲一区在线播放| 国产精品久久久久久模特| 国产精品你懂的| 99久久99久久精品免费观看| 欧美刺激午夜性久久久久久久| 激情欧美日韩一区二区| 欧美蜜桃一区二区三区| 国产剧情一区二区| 日韩一区二区视频| 国产91精品一区二区| 欧美精品一区二区三区高清aⅴ| caoporm超碰国产精品| 久久精品一区八戒影视| 91丨porny丨国产入口| 国产精品视频观看| aa成人免费视频| 亚洲va在线va天堂| 在线亚洲高清视频| 久久成人久久爱| 91精品国产91久久久久久最新毛片 | 欧美日韩中文字幕一区二区| 久国产精品韩国三级视频| 51精品秘密在线观看| 成人av免费在线| 欧美极品美女视频| 亚洲大片av| 亚洲激情午夜| 一本不卡影院| 国产精品高潮久久久久无| 你懂的国产精品| 成a人片国产精品| 日本一区二区三区国色天香| 亚洲午夜精品一区二区| 亚洲一区在线视频观看| 欧美午夜精品理论片a级按摩| 国产成人亚洲综合a∨婷婷| 日韩一区国产二区欧美三区| 国产乱对白刺激视频不卡| 欧美成人精品二区三区99精品| 色综合天天狠狠| 欧美日韩蜜桃| 亚洲天堂福利av| 久久婷婷麻豆| 久久国产精品99久久人人澡| 日韩一区二区三区免费看| 日韩一区欧美小说| 99re这里只有精品首页| 18欧美乱大交hd1984| 免费亚洲婷婷| 国产又粗又猛又爽又黄91精品| 日韩精品一区二区在线| 国产一区日韩欧美| 亚洲 欧美综合在线网络| 欧美视频一区在线| 大胆亚洲人体视频| 国产精品久久久久久一区二区三区 | 亚洲综合激情网| 色素色在线综合| 成年人午夜久久久| 一区二区激情视频| 欧美日韩视频在线第一区 | 天天综合色天天综合| 欧美在线免费播放| 91小视频免费观看| 亚洲va天堂va国产va久| 日韩精品一区二| 91视频xxxx| 欧美精品一区二区精品网| 国产精品二区二区三区| 亚洲午夜av在线| 欧美一区二区在线不卡| 91一区在线观看| 午夜a成v人精品| 精品av久久707| 男人的天堂亚洲| fc2成人免费人成在线观看播放| 亚洲激情五月婷婷| 91精品国产综合久久久久久漫画| 亚洲国产1区| 国产精品1区2区3区在线观看| 中文字幕一区日韩精品欧美| 欧美丝袜丝nylons| 亚洲第一区色| 国产风韵犹存在线视精品| 一区二区三区自拍| 日韩小视频在线观看专区| 一区二区三区精品视频在线观看 | 伊人久久婷婷| 韩国女主播一区二区三区| 国产精品污污网站在线观看| 欧美亚洲国产一区在线观看网站| 91社区在线播放| 日韩成人伦理电影在线观看| 国产偷国产偷精品高清尤物| 在线视频国内一区二区| 亚洲天堂激情| 国产成人精品免费网站| 亚洲18色成人| 久久婷婷成人综合色| 一本大道综合伊人精品热热| 午夜精品久久| 狠狠网亚洲精品| 有码一区二区三区| 精品国产亚洲在线| 91国偷自产一区二区使用方法| 欧美涩涩网站| 国产v综合v亚洲欧| 亚洲成人在线免费| 国产欧美中文在线| 91麻豆精品国产91久久久使用方法 | 性做久久久久久免费观看欧美| 欧美一区二区三区性视频| 国产精品久久久久久久久久妞妞| eeuss鲁片一区二区三区在线看| 日本不卡的三区四区五区| 日韩美女视频一区二区| 日韩精品一区二| 欧洲精品中文字幕| 国产亚洲一级| 国产在线欧美日韩| a级精品国产片在线观看| 久久99精品国产.久久久久| 一区二区三区在线视频免费| 欧美极品美女视频| 精品99一区二区| 7777精品伊人久久久大香线蕉| 亚洲女优在线| 在线播放亚洲| 欧美喷水视频| 91亚洲精品久久久蜜桃网站| 国产麻豆视频精品| 麻豆久久久久久| 肉色丝袜一区二区| 亚洲综合男人的天堂| 亚洲欧洲日韩综合一区二区| 久久尤物电影视频在线观看| 在线电影欧美成精品| 91黄视频在线| 一本大道久久a久久精二百| 国产日韩欧美一区在线| 国产精品vip| 9l国产精品久久久久麻豆| 国产精品一区二区黑丝| 久久精品国产免费| 日精品一区二区三区| 亚洲一区二区美女| 一区二区三区四区在线| 日韩理论电影院| 国产精品国产三级国产a | 亚洲日本在线a| 欧美极品少妇xxxxⅹ高跟鞋| 亚洲精品一线二线三线| 日韩亚洲电影在线| 欧美一区二区三区人| 欧美日本不卡视频| 欧美日韩中文字幕一区| 欧美日韩中文精品| 欧美日韩激情一区| 欧美疯狂性受xxxxx喷水图片| 欧美日韩在线播| 欧美视频三区在线播放| 在线视频一区二区三区| 91福利视频网站| 久久综合一区| 日本高清无吗v一区| 欧美亚洲在线| 可以看av的网站久久看| 久久一区二区精品| 在线亚洲欧美专区二区| 在线观看欧美黄色| 欧美日韩在线播放一区| 欧美日韩不卡一区二区| 日韩亚洲欧美中文三级| 久久综合色一综合色88| 久久久欧美精品sm网站| 国产无人区一区二区三区| 中文字幕欧美激情一区| 亚洲人成在线播放网站岛国 | 99久久精品一区二区| 91麻豆自制传媒国产之光| 欧美日韩亚洲免费| 伊人激情综合| 国产一级精品aaaaa看| 国产精品久久亚洲7777| 久热re这里精品视频在线6| 欧美日韩一区二区三区在线看| 91在线精品一区二区三区| 精品一区在线看|