SpringBoot集成Shiro管理会话和权限

本文介绍了如何在SpringBoot项目中集成Shiro进行会话管理和权限控制,包括Shiro的依赖配置、SpringBoot的Shiro配置类设定、自定义Realm域、UserToken、密码匹配器,以及利用Ehcache缓存会话,同时讲解了用户登录实现与Thymeleaf模板引擎中访问Shiro权限的示例,还提供了MD5加密工具类的使用。
摘要由CSDN通过智能技术生成

1、Shiro的依赖Jar包

<shiro-all.version>1.3.2</shiro-all.version>
		<shiro-spring.version>1.3.2</shiro-spring.version>
		<shiro-ehcache.version>1.3.2</shiro-ehcache.version>
		<thymeleaf-extras-shiro.version>2.0.0</thymeleaf-extras-shiro.version>

<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-all</artifactId>
			<version>${
   shiro-all.version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.shiro</groupId>
					<artifactId>shiro-quartz</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>${
   shiro-spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-ehcache</artifactId>
			<version>${
   shiro-ehcache.version}</version>
		</dependency>

		<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>${
   thymeleaf-extras-shiro.version}</version>
		</dependency>

2、SpringBoot的Shiro配置类

package com.bsoft.core.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.bsoft.core.security.RetryLimitHashedCredentialsMatcher;
import com.bsoft.core.security.ShiroRealm;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.SessionValidationScheduler;
import org.apache.shiro.session.mgt.ValidatingSessionManager;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author :Liujian
 * @date :2019/11/4 17:36
 * @description:Shiro配置类
 * @version:
 */
@Configuration
public class ShiroConfig {
   

    private static Logger logger = LoggerFactory.getLogger(ShiroConfig.class);

    @Bean(name = "shiroDialect")
    public ShiroDialect shiroDialect(){
   
        return new ShiroDialect();
    }

    @Bean("ehCacheManager")
    public EhCacheManager ehCacheManager(){
   
        CacheManager cacheManager = CacheManager.getCacheManager("es");
        if(cacheManager == null){
   
            try {
   
                cacheManager = CacheManager.create(ResourceUtils.getInputStreamForPath("classpath:ehcache-shiro.xml"));
            } catch (IOException e) {
   
                throw new RuntimeException("initialize cacheManager failed");
            }
        }
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

    /**
     * 凭证密码匹配器
     * @param ehCacheManager
     * @return
     */
    @Bean(name="credentialsMatcher")
    public CredentialsMatcher credentialsMatcher(@Qualifier("ehCacheManager") EhCacheManager ehCacheManager) {
   
        return new RetryLimitHashedCredentialsMatcher(ehCacheManager);
    }

    /**
     * 配置自定义的权限登录器
     * @param credentialsMatcher
     * @return
     */
    @Bean(name="authRealm")
    public ShiroRealm authRealm(@Qualifier("credentialsMatcher") CredentialsMatcher credentialsMatcher) {
   
        ShiroRealm authRealm = new ShiroRealm();
        authRealm.setCredentialsMatcher(credentialsMatcher);
        authRealm.setCachingEnabled(false);
        return authRealm;
    }

    @Bean(name = "sessionIdCookie")
    public SimpleCookie sessionIdCookie() {
   
        SimpleCookie simpleCookie = new SimpleCoo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值