这篇文章主要介绍了详解spring与shiro集成,需要的朋友可以参考下
Shiro的组件都是JavaBean/POJO式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web应用的集成。
在示例之前,需要导入shiro-spring及spring-context依赖,具体请参考pom.xml。
spring-beans.xml配置文件提供了基础组件如DataSource、DAO、Service组件的配置。
JavaSE应用
spring-shiro.xml提供了普通JavaSE独立应用的Spring配置:
<!-- 缓存管理器 使用Ehcache实现 --> <!-- 凭证匹配器 --> <!-- Realm实现 --> <!-- 会话ID生成器 --><!-- 会话DAO --> <!-- 会话验证调度器 --> <!-- 会话管理器 --> <!-- 安全管理器 --> <!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) --> <!-- Shiro生命周期处理器-->
可以看出,只要把之前的ini配置翻译为此处的spring xml配置方式即可,无须多解释。LifecycleBeanPostProcessor用于在实现了Initializable接口的Shiro bean初始化时调用Initializable接口回调,在实现了Destroyable接口的Shiro bean销毁时调用 Destroyable接口回调。如UserRealm就实现了Initializable,而DefaultSecurityManager实现了Destroyable。具体可以查看它们的继承关系。
测试用例请参考com.github.zhangkaitao.shiro.chapter12.ShiroTest。
Web应用
Web应用和普通JavaSE应用的某些配置是类似的,此处只提供一些不一样的配置,详细配置可以参考spring-shiro-web.xml。
<!-- 会话Cookie模板 --> <!-- 会话管理器 --> <!-- 安全管理器 -->
1、sessionIdCookie是用于生产Session ID Cookie的模板;
2、会话管理器使用用于web环境的DefaultWebSessionManager;
3、安全管理器使用用于web环境的DefaultWebSecurityManager。
<!-- 基于Form表单的身份验证过滤器 --> <!-- Shiro的Web过滤器 --> /index.jsp = anon /unauthorized.jsp = anon /login.jsp = authc /logout = logout /** = user
1、formAuthenticationFilter为基于Form表单的身份验证过滤器;此处可以再添加自己的Filter bean定义;
2、shiroFilter:此处使用ShiroFilterFactoryBean来创建ShiroFilter过滤器;filters属性用于定义自己的过滤器,即ini配置中的[filters]部分;filterChainDefinitions用于声明url和filter的关系,即ini配置中的[urls]部分。
接着需要在web.xml中进行如下配置:
contextConfigLocation classpath:spring-beans.xml, classpath:spring-shiro-web.xml org.springframework.web.context.ContextLoaderListener
通过ContextLoaderListener加载contextConfigLocation指定的Spring配置文件。
shiroFilterorg.springframework.web.filter.DelegatingFilterProxy targetFilterLifecycletrue shiroFilter/*
DelegatingFilterProxy会自动到Spring容器中查找名字为shiroFilter的bean并把filter请求交给它处理。
Shiro权限注解
Shiro提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP的功能来进行判断,如Spring AOP;Shiro提供了Spring AOP集成用于权限注解的解析和验证。
为了测试,此处使用了Spring MVC来测试Shiro注解,当然Shiro注解不仅仅可以在web环境使用,在独立的JavaSE中也是可以用的,此处只是以web为例了。
在spring-mvc.xml配置文件添加Shiro Spring AOP权限注解的支持:
如上配置用于开启Shiro Spring AOP权限注解的支持;表示代理类。
接着就可以在相应的控制器(AnnotationController)中使用如下方来源gao@dai!ma.com搞$代^码网式进行注解:
@RequiresRoles("admin") @RequestMapping("/hello2") public String hello2() { return "success"; }
访问hello2方法的前提是当前用户有admin角色。
当验证失败,其会抛出UnauthorizedException异常,此时可以使用Spring的ExceptionHandler(DefaultExceptionHandler)来进行拦截处理:
@ExceptionHandler({UnauthorizedException.class}) @ResponseStatus(HttpStatus.UNAUTHORIZED) public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) { ModelAndView mv = new ModelAndView(); mv.addObject("exception", e); mv.setViewName("unauthorized"); return mv; }
权限注解
@RequiresAuthentication
表示当前Subject已经通过login进行了身份验证;即Subject. isAuthenticated()返回true。
@RequiresUser
表示当前Subject已经身份验证或者通过记住我登录的。
@RequiresGuest
表示当前Subject没有身份验证或通过记住我登录过,即是游客身份。
@RequiresRoles(value={“admin”, “user”}, logical= Logical.AND)
表示当前Subject需要角色admin和user。
@RequiresPermissions (value={“user:a”, “user:b”}, logical= Logical.OR)
表示当前Subject需要权限user:a或user:b。
总结
以上就是详解spring与shiro集成的详细内容,更多请关注gaodaima搞代码网其它相关文章!