Spring Boot 整合 Shiro ,两种方式全总结!
Spring Boot整合Shiro主要有两种方式:
- 使用Shiro Spring Boot Starter
- 手动配置
1. 使用Shiro Spring Boot Starter
首先,添加依赖到你的pom.xml
:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>
然后,在application.properties
或application.yml
中配置Shiro:
# 配置Shiro
shiro.sessionManager.globalSessionTimeout = 800000
shiro.unauthorizedUrl = /unauthorized
shiro.user.cookie = user
shiro.user.cookie.maxAge = 3600
shiro.user.cookie.path = /
2. 手动配置
在Spring Boot的配置类中添加Shiro配置:
@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
// 配置你的Realm
return new YourRealm();
}
@Bean
public DefaultWebSecurityManager securityManager() {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
manager.setRealm(realm());
// 配置其他组件
return manager;
}
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager);
// 配置过滤链
return factoryBean;
}
}
在application.properties
或application.yml
中配置Shiro:
# 配置Shiro
shiro.sessionManager.globalSessionTimeout = 800000
shiro.unauthorizedUrl = /unauthorized
shiro.user.cookie = user
shiro.user.cookie.maxAge = 3600
shiro.user.cookie.path = /
以上两种方式均可以整合Shiro到Spring Boot项目中,具体使用哪一种取决于你的项目需求和偏好。
评论已关闭