dubbo shiro
时间: 2023-12-28 18:24:36 浏览: 88
Dubbo Shiro是一种基于Dubbo和Shiro框架的权限管理解决方案。它可以帮助开发人员快速实现基于角色的访问控制和权限管理。下面是一个简单的演示:
1.在Dubbo服务提供者中配置Shiro过滤器链
```java
@Bean
public FilterRegistrationBean<DelegatingFilterProxy> shiroFilter() {
FilterRegistrationBean<DelegatingFilterProxy> filterRegistration = new FilterRegistrationBean<>();
filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
filterRegistration.addUrlPatterns("/*");
filterRegistration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR);
return filterRegistration;
}
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/dubbo/**", "authc, roles[dubbo]");
filterChainDefinitionMap.put("/**", "anon");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
```
2.在Dubbo服务消费者中配置Shiro
```java
@Bean
public ReferenceBean<DemoService> demoService() {
ReferenceBean<DemoService> referenceBean = new ReferenceBean<>();
referenceBean.setInterface(DemoService.class);
referenceBean.setUrl("dubbo://localhost:20880");
referenceBean.setVersion("1.0.0");
referenceBean.setTimeout(5000);
referenceBean.setRetries(3);
referenceBean.setCluster("failover");
referenceBean.setLoadbalance("roundrobin");
referenceBean.setApplication(applicationConfig());
referenceBean.setRegistry(registryConfig());
referenceBean.setInterface(DemoService.class);
referenceBean.setCheck(false);
referenceBean.setFilter("shiro");
return referenceBean;
}
```
3.在Shiro配置文件中配置权限
```ini
[users]
admin=admin,admin
guest=guest,guest
[roles]
dubbo=guest
```
阅读全文