|
|
@ -14,15 +14,26 @@ import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
|
|
|
|
|
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|
|
|
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 这个类使用了Spring的 @Configuration 注解,表明它是一个配置类,用于配置Shiro框架相关的各种组件和功能,
|
|
|
|
|
|
|
|
// Spring在启动时会自动扫描并加载这个类中的配置信息,将其中定义的Bean注册到Spring容器中。
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
public class ShiroConfiguration {
|
|
|
|
public class ShiroConfiguration {
|
|
|
|
|
|
|
|
// 创建一个LinkedHashMap用于定义Shiro的过滤链规则,键是URL路径的匹配模式,值是对应的权限过滤器名称,
|
|
|
|
|
|
|
|
// 后续会将这个映射关系配置到ShiroFilterFactoryBean中,用于控制不同URL的访问权限。
|
|
|
|
private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
|
|
|
|
private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "shiroRealm" 的Bean,这个Bean返回的是自定义的MemberRealm实例,
|
|
|
|
|
|
|
|
// MemberRealm是继承自Shiro的AuthorizingRealm,用于实现具体的认证(登录验证)和授权(权限验证)逻辑,
|
|
|
|
|
|
|
|
// 将其注册为Spring容器中的Bean,方便其他Shiro相关组件引用,比如在SecurityManager中设置使用这个Realm进行验证操作。
|
|
|
|
@Bean(name = "shiroRealm")
|
|
|
|
@Bean(name = "shiroRealm")
|
|
|
|
public MemberRealm getShiroRealm() {
|
|
|
|
public MemberRealm getShiroRealm() {
|
|
|
|
return new MemberRealm();
|
|
|
|
return new MemberRealm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "shiroEhcacheManager" 的Bean,用于创建和配置EhCacheManager实例,
|
|
|
|
|
|
|
|
// EhCacheManager在Shiro框架中用于缓存相关的数据,比如用户的授权信息等,提高系统性能,减少重复查询授权等操作的开销。
|
|
|
|
|
|
|
|
// 通过设置cacheManagerConfigFile属性指定了EhCache的配置文件路径(这里是类路径下的ehcache-shiro.xml文件),
|
|
|
|
|
|
|
|
// 该配置文件会定义缓存的具体策略、缓存区域等相关设置。
|
|
|
|
@Bean(name = "shiroEhcacheManager")
|
|
|
|
@Bean(name = "shiroEhcacheManager")
|
|
|
|
public EhCacheManager getEhCacheManager() {
|
|
|
|
public EhCacheManager getEhCacheManager() {
|
|
|
|
EhCacheManager em = new EhCacheManager();
|
|
|
|
EhCacheManager em = new EhCacheManager();
|
|
|
@ -30,11 +41,17 @@ public class ShiroConfiguration {
|
|
|
|
return em;
|
|
|
|
return em;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "lifecycleBeanPostProcessor" 的Bean,创建LifecycleBeanPostProcessor实例,
|
|
|
|
|
|
|
|
// LifecycleBeanPostProcessor是Shiro提供的一个用于管理Shiro相关Bean生命周期的后置处理器,
|
|
|
|
|
|
|
|
// 它能够确保Shiro的一些组件(如Realm等)在Spring容器中正确地初始化、销毁等,按照其生命周期规范执行相应操作。
|
|
|
|
@Bean(name = "lifecycleBeanPostProcessor")
|
|
|
|
@Bean(name = "lifecycleBeanPostProcessor")
|
|
|
|
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
|
|
|
|
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
|
|
|
|
return new LifecycleBeanPostProcessor();
|
|
|
|
return new LifecycleBeanPostProcessor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "defaultAdvisorAutoProxyCreator" 的Bean,创建DefaultAdvisorAutoProxyCreator实例,
|
|
|
|
|
|
|
|
// 它是Spring AOP中的一个自动代理创建器,用于在Spring容器中自动创建基于Advisor(切面顾问)的代理对象,
|
|
|
|
|
|
|
|
// 在Shiro与Spring集成时,帮助实现基于AOP的权限控制拦截等功能,通过设置proxyTargetClass为true,表示使用基于类的代理方式(而不是基于接口的代理)。
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
|
|
|
|
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
|
|
|
|
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
|
|
|
|
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
|
|
|
@ -42,6 +59,10 @@ public class ShiroConfiguration {
|
|
|
|
return daap;
|
|
|
|
return daap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "securityManager" 的Bean,创建DefaultWebSecurityManager实例,
|
|
|
|
|
|
|
|
// DefaultWebSecurityManager是Shiro在Web应用中用于管理安全相关操作的核心组件,
|
|
|
|
|
|
|
|
// 在这里设置了它所使用的Realm(通过调用getShiroRealm()方法获取前面定义的MemberRealm实例)以及缓存管理器(通过getEhCacheManager()获取EhCacheManager实例),
|
|
|
|
|
|
|
|
// 这样SecurityManager在进行认证和授权操作时就会使用指定的Realm进行验证,并利用缓存管理器来缓存相关信息,提高效率。
|
|
|
|
@Bean(name = "securityManager")
|
|
|
|
@Bean(name = "securityManager")
|
|
|
|
public DefaultWebSecurityManager getDefaultWebSecurityManager() {
|
|
|
|
public DefaultWebSecurityManager getDefaultWebSecurityManager() {
|
|
|
|
DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
|
|
|
|
DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
|
|
|
@ -50,22 +71,36 @@ public class ShiroConfiguration {
|
|
|
|
return dwsm;
|
|
|
|
return dwsm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "authorizationAttributeSourceAdvisor" 的Bean,创建AuthorizationAttributeSourceAdvisor实例,
|
|
|
|
|
|
|
|
// AuthorizationAttributeSourceAdvisor是Shiro与Spring AOP集成的一个关键组件,用于在方法级别进行权限控制,
|
|
|
|
|
|
|
|
// 它通过设置securityManager(调用getDefaultWebSecurityManager()获取配置好的SecurityManager)来关联整个权限管理体系,
|
|
|
|
|
|
|
|
// 使得在Spring应用中可以基于注解等方式方便地进行权限控制,比如在方法上添加Shiro的权限注解来限制哪些用户可以访问该方法。
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {
|
|
|
|
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {
|
|
|
|
AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
|
|
|
|
AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
|
|
|
|
aasa.setSecurityManager(getDefaultWebSecurityManager());
|
|
|
|
aasa.setSecurityManager(getDefaultWebSecurityManager());
|
|
|
|
return new AuthorizationAttributeSourceAdvisor();
|
|
|
|
return new AuthorizationAttributeSourceAdvisor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
|
|
|
|
* ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
|
|
|
|
* @return
|
|
|
|
* 这个方法定义了一个名为 "shiroDialect" 的Bean,返回ShiroDialect实例,
|
|
|
|
*/
|
|
|
|
* ShiroDialect是用于在Thymeleaf模板引擎中使用Shiro标签的一个方言类,
|
|
|
|
@Bean
|
|
|
|
* 当项目中使用Thymeleaf作为页面模板并且需要在页面中进行Shiro相关的权限判断、显示等操作时(比如根据用户权限显示或隐藏页面元素),
|
|
|
|
public ShiroDialect shiroDialect(){
|
|
|
|
* 通过将这个Bean注册到Spring容器中,Thymeleaf就能识别并解析Shiro相关的标签,实现相应的功能。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public ShiroDialect shiroDialect() {
|
|
|
|
return new ShiroDialect();
|
|
|
|
return new ShiroDialect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个名为 "shiroFilter" 的Bean,创建ShiroFilterFactoryBean实例,
|
|
|
|
|
|
|
|
// ShiroFilterFactoryBean是Shiro在Web应用中用于配置URL级别的访问控制过滤器链的核心组件,
|
|
|
|
|
|
|
|
// 它通过设置securityManager(关联前面配置好的DefaultWebSecurityManager)来建立整个权限过滤的基础框架,
|
|
|
|
|
|
|
|
// 同时设置了登录页面的URL(通过setLoginUrl方法设置为 "/login",当用户未登录访问受保护资源时会重定向到这个登录页面)、
|
|
|
|
|
|
|
|
// 登录成功后的默认跳转页面URL(通过setSuccessUrl方法设置为 "/index"),
|
|
|
|
|
|
|
|
// 并且将前面定义的filterChainDefinitionMap(包含了URL路径和对应权限过滤器的映射关系)设置进去,
|
|
|
|
|
|
|
|
// 这样Shiro就能根据这些配置对不同的URL请求进行权限验证和相应的处理,比如哪些URL需要认证才能访问,哪些URL可以匿名访问等。
|
|
|
|
@Bean(name = "shiroFilter")
|
|
|
|
@Bean(name = "shiroFilter")
|
|
|
|
public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
|
|
|
|
public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
|
|
|
|
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
|
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
|