搜索

pdf文档 Shiro support for the Spring Security plugin - Reference Documentation

16.72 KB 5 页 0 下载 75 浏览 0 评论 0 收藏
所属分类: 后端开发 / Java
语言 格式 评分
英语
.pdf
3
摘要
The document provides reference documentation for the Spring Security Shiro Plugin, which integrates Apache Shiro with Spring Security. It explains how the plugin combines Spring Security for authentication and authorization while using Shiro's Access Control List (ACL) for permissions. Key features include configuration options for permissions, support for Shiro annotations in services, and direct use of Shiro's Subject methods. The plugin requires setting up a permission domain class and offers flexibility through a custom ShiroPermissionResolver.
AI总结
# Shiro 支持 Spring Security 插件 - 参考文档总结 ## 1. 介绍 - **功能**:该插件允许在 Spring Security 和 Shiro 之间采用混合模式,主要支持 Shiro 的访问控制列表(ACL),因为 Spring Security 的 ACL 虽然功能强大,但使用较为繁琐。 - **认证与授权**:主要的认证和授权工作仍由 Spring Security 完成。插件通过监听 Spring Security 的认证事件,使用 Spring Security 的认证实例创建和注册 Shiro 的 `Subject` 实例。 - **版本历史**: - 1.0.0:2015 年 12 月 7 日发布 - 1.0-RC1:2013 年 10 月 5 日发布 - 0.1:2013 年 1 月 6 日发布 --- ## 2. 使用 ### 2.1 添加依赖 在 `BuildConfig.groovy` 中添加插件依赖: ```groovy plugins { ... compile 'spring-security-shiro:1.0.0' } ``` ### 2.2 配置权限 - **关联角色与权限**:需要配置权限以使用 Shiro 的注解和方法。Spring Security 已经处理了角色部分,因此需要手动配置权限。 - **Permission 类**:创建一个简单的 `Permission` 类(例如 `com.mycompany.myapp.Permission`),并确保用户和权限之间是一对多关系。 - **注册配置**:在 `Config.groovy` 中注册权限类: ```groovy grails.plugin.springsecurity.shiro.permissionDomainClassName = 'com.mycompany.myapp.Permission' ``` ### 2.3 自定义权限解析器 如果需要更多灵活性,可以实现 `ShiroPermissionResolver` 接口,并在 `resources.groovy` 中注册自定义 Bean: ```groovy import com.mycompany.myapp.MyShiroPermissionResolver beans = { shiroPermissionResolver(MyShiroPermissionResolver) } ``` ### 2.4 注解服务方法 - **支持范围**:目前仅支持 Grails 服务和其他 Spring Bean,不支持控制器。 - **可用注解**: - `RequiresAuthentication` - `RequiresGuest` - `RequiresPermissions` - `RequiresRoles` - `RequiresUser` ### 2.5 直接使用 Shiro - **推荐方式**:优先使用注解以减少代码冗余。 - **示例代码**: ```java import org.apache.shiro.SecurityUtils import org.apache.shiro.subject.Subject ... Subject subject = SecurityUtils.getSubject() subject.checkPermission('printer:print:lp7200') subject.isPermitted('printer:print:lp7200') subject.checkRole('ROLE_ADMIN') subject.hasRole('ROLE_ADMIN') subject.isAuthenticated() ``` --- ## 3. 配置 在 `Config.groovy` 中配置以下选项: | 名称 | 默认值 | 含义 | |---|---|---| | `grails.plugin.springsecurity.shiro.active` | `true` | 如果为 `false`,插件将被禁用 | | `grails.plugin.springsecurity.shiro.permissionDomainClassName` | 无,必须设置 | 权限领域类的全类名 | | `grails.plugin.springsecurity.shiro.useCache` | `true` | 是否缓存权限查找;如果禁用,权限将从数据库加载 | --- ## 总结 该插件通过结合 Spring Security 和 Shiro,提供了一种灵活的权限管理方案。插件支持 Shiro 的 ACL,并通过配置和注解简化权限管理。开发者可以根据需求自定义权限解析器,并通过配置选项灵活调整插件行为。
P1
P2
P3
P4
P5
下载文档到本地,方便使用
文档评分
请文明评论,理性发言.