shiro框架整合spring boot
时间: 2023-09-09 19:13:25 浏览: 143
shiro学习视屏,课件,思维导图,全套资料,内有shiro整合srpingboot项目
Shiro框架与Spring Boot的整合相对简单,可以通过一些配置和依赖来实现。以下是一个基本的整合示例:
1. 在Spring Boot的pom.xml文件中添加Shiro和Web依赖:
```xml
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.8.0</version>
</dependency>
```
2. 创建一个Shiro配置类,用于配置Shiro相关的Bean和过滤器:
```java
@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
return new MyRealm(); // 自定义的Realm实现
}
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager);
// 配置过滤规则等
// factoryBean.setFilterChainDefinitionMap(...);
return factoryBean;
}
@Bean
public DefaultWebSecurityManager securityManager(Realm realm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(realm);
return securityManager;
}
}
```
3. 创建一个自定义的Realm实现,用于处理身份认证和权限授权逻辑:
```java
public class MyRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 处理授权逻辑
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// 处理认证逻辑
return null;
}
}
```
4. 在Spring Boot的application.properties或application.yml文件中配置Shiro相关属性:
```yaml
shiro:
loginUrl: /login
successUrl: /home
unauthorizedUrl: /unauthorized
```
这样,你就完成了Shiro框架与Spring Boot的整合。你可以根据自己的需求继续配置Shiro的过滤规则、权限配置等。
阅读全文