|
|
|
@ -15,59 +15,63 @@ import org.springframework.context.annotation.Configuration;
|
|
|
|
|
/**
|
|
|
|
|
* Shiro 配置类
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
public class ShiroConfiguration {
|
|
|
|
|
|
|
|
|
|
// 定义过滤链定义映射
|
|
|
|
|
// 定义过滤链定义映射,用于配置 Shiro 的权限控制规则
|
|
|
|
|
private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 Shiro 领域对象
|
|
|
|
|
* 获取 Shiro 领域对象,即自定义的 MemberRealm 实例
|
|
|
|
|
* @return MemberRealm 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "shiroRealm")
|
|
|
|
|
public MemberRealm getShiroRealm() {
|
|
|
|
|
// 创建 MemberRealm 实例
|
|
|
|
|
return new MemberRealm();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 EhCache 管理器
|
|
|
|
|
* 获取 EhCache 管理器,用于缓存 Shiro 的相关数据
|
|
|
|
|
* @return EhCacheManager 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "shiroEhcacheManager")
|
|
|
|
|
public EhCacheManager getEhCacheManager() {
|
|
|
|
|
// 创建 EhCacheManager 实例,并设置其配置文件路径
|
|
|
|
|
EhCacheManager em = new EhCacheManager();
|
|
|
|
|
em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
|
|
|
|
|
return em;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取生命周期 Bean 后处理器
|
|
|
|
|
* 获取生命周期 Bean 后处理器,用于管理 Shiro 相关 Bean 的生命周期
|
|
|
|
|
* @return LifecycleBeanPostProcessor 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "lifecycleBeanPostProcessor")
|
|
|
|
|
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
|
|
|
|
|
// 创建 LifecycleBeanPostProcessor 实例
|
|
|
|
|
return new LifecycleBeanPostProcessor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取默认顾问自动代理创建器
|
|
|
|
|
* 获取默认顾问自动代理创建器,用于自动创建代理对象
|
|
|
|
|
* @return DefaultAdvisorAutoProxyCreator 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
|
|
|
|
|
// 创建 DefaultAdvisorAutoProxyCreator 实例,并设置代理目标类为 true
|
|
|
|
|
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
|
|
|
|
|
daap.setProxyTargetClass(true);
|
|
|
|
|
return daap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取默认 Web 安全管理器
|
|
|
|
|
* 获取默认 Web 安全管理器,用于整合 Shiro 的各种组件
|
|
|
|
|
* @return DefaultWebSecurityManager 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "securityManager")
|
|
|
|
|
public DefaultWebSecurityManager getDefaultWebSecurityManager() {
|
|
|
|
|
// 创建 DefaultWebSecurityManager 实例,并设置相关属性
|
|
|
|
|
DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
|
|
|
|
|
dwsm.setRealm(getShiroRealm());
|
|
|
|
|
dwsm.setCacheManager(getEhCacheManager());
|
|
|
|
@ -75,27 +79,29 @@ public class ShiroConfiguration {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取授权属性源顾问
|
|
|
|
|
* 获取授权属性源顾问,用于在 Spring AOP 中进行授权检查
|
|
|
|
|
* @return AuthorizationAttributeSourceAdvisor 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {
|
|
|
|
|
// 创建 AuthorizationAttributeSourceAdvisor 实例,并设置相关属性
|
|
|
|
|
AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
|
|
|
|
|
aasa.setSecurityManager(getDefaultWebSecurityManager());
|
|
|
|
|
return aasa;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 Shiro 过滤器工厂 Bean
|
|
|
|
|
* 获取 Shiro 过滤器工厂 Bean,用于配置 Shiro 的过滤器链
|
|
|
|
|
* @return ShiroFilterFactoryBean 对象
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "shiroFilter")
|
|
|
|
|
public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
|
|
|
|
|
// 创建 ShiroFilterFactoryBean 实例,并设置相关属性
|
|
|
|
|
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
|
|
shiroFilterFactoryBean.setSecurityManager(getDefaultWebSecurityManager());
|
|
|
|
|
shiroFilterFactoryBean.setLoginUrl("/login");
|
|
|
|
|
shiroFilterFactoryBean.setSuccessUrl("/index");
|
|
|
|
|
// 设置过滤链定义
|
|
|
|
|
// 设置过滤链定义映射
|
|
|
|
|
filterChainDefinitionMap.put("/member/**", "authc");
|
|
|
|
|
filterChainDefinitionMap.put("/**", "anon");
|
|
|
|
|
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
|
|
|
|