shiro 整合spring
来源:互联网 发布:ubuntu 16.04 syslog 编辑:程序博客网 时间:2024/06/06 07:11
Shiro 集成 Spring
1. 加入 Spring 的开发环境.
2. 加入 SpringMVC 的配置.
- Spring 集成 Shiro.
1). 打开 shiro\shiro-root-1.2.2\samples\spring\src\main\webapp\WEB-INF\applicationContext.xml 文件.2). 加入 ehcache-core-2.4.3.jar3). 自定义的 Realm 需要继承 org.apache.shiro.realm.AuthorizingRealm 类①. 其中 doGetAuthorizationInfo 方法用于授权。 doGetAuthenticationInfo 方法用于认证.
4). 在 web.xml 文件中配置 Shiro 的 Filter
<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param></filter><filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
- 登陆的流程:
1). 需要发送请求到 SpringMVC 的 Handler 或 Struts2 的 Action 或 Servlet. 而不是直接到 Shiro 的某一个组件.
2). 在 Handler 的方法中完成:
①. 获取前端输入的用户名和密码
②. 其他操作来自于 QuickStart 中的代码.
3). 完成 realm 的 doGetAuthenticationInfo 方法
4). 还可以做得: 密码的盐值加密. 根据用户在数据表中的情况抛出对应的异常.
5). 密码的加密: 可以为当前的 Realm 配置一个 CredentialsMatcher 类型的 bean.
6). 加盐值: 在 doGetAuthenticationInfo 方法构建返回值对象时使用如下构造器即可.
SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName)
其中 credentialsSalt 参数可以调用 ByteSource.Util.bytes(salt); 工具方法来生成
7). 如何计算盐值加密后的密码呢 ?
new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
8). 配置受保护的资源: 使用 shiro 内置的过滤器.
anon
authc
logout
9). 注意: URL 权限采取第一次匹配优先的方式
10). 使用 shiro 的标签来隐藏受保护的资源
在jsp页面可以使用如下taglib指令加入shiro标签
<%@ taglib prefix ="shiro" uri="http://shiro.apache.org/tags" %>
使用shiro标签保护资源
<shiro:hasRole name= "admin"> <a href="admin.jsp"> To admin</a > <br>< br>
11). 使用 shiro 的注解来进行权限的保护.
在java方法中可以加入注解的方式对java方法进行权限保护
@RequiresRoles ({ “admin”})
在spring配置shiro的信息
<!-- 配置 cacheManager --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" > <property name= "cacheManagerConfigFile" value="classpath:ehcache.xml" /> </bean > <!-- 配置 Realm 实例. 以完成自定义的认证和授权 --> <bean id="realm" class= "com.atguigu.shiro.spring.MyRealm" > <property name= "credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" > <property name= "hashAlgorithmName" value="MD5" ></property> <property name= "hashIterations" value="1024" ></property> </bean> </property> </bean> <!-- 配置 SecurityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" > <property name= "cacheManager" ref="cacheManager" /> <property name= "realm" ref ="realm"/> </bean > <!-- 配置 LifecycleBeanPostProcessor 实例. 会自动的调用 Shiro 的 bean 的 init 和 destroy 方法 --> <bean id="lifecycleBeanPostProcessor" class ="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 启用 shiro 注解的两个 bean. 注意: 必须在配置 LifecycleBeanPostProcessor 的前提下使用 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor" > <property name= "securityManager" ref="securityManager" /> </bean ><!--创建工厂方法为filterChainDefinitionMap属性创建map--><bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap"></bean> <!-- 配置 WEB 环境下的 Shiro Filter! 将配置哪些资源被拦截, 哪个是登陆页面, 如何登出 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" > <property name= "securityManager" ref="securityManager" /> <!-- 配置登陆页面 --> <property name= "loginUrl" value ="/login.jsp"/> <!-- 配置没有权限访问时的响应页面 --> <property name= "unauthorizedUrl" value="/unauthorized.jsp" /> <!-- 可以使用实例工厂方法的方式为 ShiroFilterFactoryBean 的 filterChainDefinitionMap 属性配置 bean--> <!-- <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property> --> <!-- 配置哪些面需要被拦截 --> <property name= "filterChainDefinitions" > <value> # 匿名即可访问的资源 /login.jsp = anon / shiro-login=anon #登出 /logout=logout /admin.jsp = roles[ admin] /user.jsp = roles[user] # 其他页面必须经过授权才可以访问 /** = authc </value> </property> </bean >
0 0
- 【shiro】--- spring整合shiro
- spring + Shiro 整合
- spring整合shiro
- Spring与Shiro整合
- spring 整合shiro
- shiro 整合spring
- Shiro+Spring MVC整合
- Shiro整合spring
- Shiro+Spring MVC整合
- Spring整合Shiro(一)
- Spring整合Shiro(二)
- shiro整合spring+springmvc
- shiro 整合 spring
- Shiro整合Spring maven
- Spring+Shiro权限整合
- Shiro与Spring整合
- shiro整合spring
- Spring整合Shiro
- 概率dp
- 初次邂逅sqlmap
- fck实现htm在线编辑
- 【杭电-oj】-1084-What Is Your Grade?(结构体)
- 【NOIP模拟】弄提纲
- shiro 整合spring
- 第一篇博客
- HDU 1166 敌兵布阵【线段树,树状数组入门题,单点更新,区间求和】
- 图片放大镜
- MYSQL之子查询
- 图和树
- Css中的绝对定位与相对定位
- SpringMVC小扎
- C从文件读取数据初始化数据对象