Shiro中的加密和验证策略
来源:互联网 发布:海报拼图软件 潘哒 编辑:程序博客网 时间:2024/06/18 10:41
上篇中我们说了Shiro的框架的认证,这篇我们来说说Shiro框架的加密和验证策略
Shiro的加密有很多,现在常用的就MD5(消息摘要),要想给传入密码加密要在Spring的
applicationContext配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置securityManager的bean--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--配置Shiro缓存 --> <property name="cacheManager" ref="cacheManager"/> <!--配置realm(这个非常的重要)--> <property name="realm" ref="jdbcRealm"/> </bean> <!--配置缓存--> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean> <!--配置Realm--> <bean id="jdbcRealm" class="org.peter.realm.MyRealm"> <!--配置密码加密--> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"/> <property name="hashIterations" value="1024"/> </bean> </property> </bean> <!--配置Spring去管理Shiro中的bean的生命周期--> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!--开启Shiro的注解使用--> <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> <!--配置页面过滤规则--> <!--web.xml中filter-name的名字要和ShiroFilterFactoryBean的id一样,不一样会出错--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!--配置登录页面--> <property name="loginUrl" value="/Login.jsp"/> <!--配置登录成功后的页面--> <property name="successUrl" value="/index.jsp"/> <!--配置没有获得访问权限的页面--> <property name="unauthorizedUrl" value="/s/unauthorized"/> <property name="filterChainDefinitions"> <value> <!--anon 表示Login.jsp可以匿名访问--> /Login.jsp = anon /login=anon <!--配置退出--> /logout=logout <!--authc表示剩余的页面必须在登录后访问--> /** = authc </value> </property> </bean></beans>第二步:在配置完Spring之后,在配置MyRealm文件
package org.peter.realm;import org.apache.shiro.authc.*;import org.apache.shiro.crypto.hash.SimpleHash;import org.apache.shiro.realm.AuthenticatingRealm;import org.apache.shiro.util.ByteSource;/** * Created by Lenovo on 2017/8/1. */public class MyRealm extends AuthenticatingRealm { @Override public String getName() { return "MyRealm"; } @Override public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } // MD5消息摘要(密码加密) @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token1) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) token1; /*获取到用户名*/ String username = token.getUsername(); if ("zs".equals(username)){ throw new UnknownAccountException("用户名不存在"); } if ("ls".equals(username)){ throw new LockedAccountException("该用户已经被锁"); } // 取数据库中在查询 // .................... Object credentials = null; if ("admin".equals(username)){ credentials="c41d7c66e1b8404545aa3a0ece2006ac"; }else if("wangwu".equals(username)){ credentials="b6f8daff78cfcb486454e6914e666c71"; } ByteSource credentialsSalt = ByteSource.Util.bytes(username); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, credentials,credentialsSalt, getName()); return info; } public static void main(String[] args) {// 解密: ByteSource salt = ByteSource.Util.bytes("wangwu"); SimpleHash simpleHash = new SimpleHash("MD5","123",salt,1024); System.out.println(simpleHash); }}这样就可以实现密码加密
Shiro的验证策略
shiro的验证策略:
1.AuthenticationStrategy接口的默认实现:
2.FirstSuccessfulStrategy:只要有一个Realm 验证成功即可,只返回第一个Realm 身份验证成功的认证信息,其他的忽略;
3.AtLeastOneSuccessfulStrategy:只要有一个Realm验证成功即可,和FirstSuccessfulStrategy不同,将返回所有Realm身份验证成功的认证信息;
4.AllSuccessfulStrategy:所有Realm验证成功才算成功,且返回所有Realm身份验证成功的认证信息,如果有一个失败就失败了。
5.ModularRealmAuthenticator默认是AtLeastOneSuccessfulStrategy策略
为了验证修改策略的影响,我们创建了两个Realm文件
实现策略的修改,我们只需要修改Spring的applicationContext.xml的配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置securityManager的bean--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--配置Shiro缓存 --> <property name="cacheManager" ref="cacheManager"/> <!--调用配置策略的时候要放在realms前面--> <property name="authenticator" ref="authenticator"/> <!--配置realm(这个非常的重要)--> <!--<property name="realm" ref="jdbcRealm"/>--> <!--配置多个realms执行的顺序就是从上到下--> <property name="realms"> <list> <ref bean="jdbcRealm"/> <ref bean="SecondRealm"/> </list> </property> </bean> <!--认证策略有三种: 第一种:只要有一个认证成功就算成功 第二种:只要第一个认证成功就算成功 第三种:全部认证成功,才算成功 --> <!--修改认证策略--> <bean class="org.apache.shiro.authc.pam.ModularRealmAuthenticator" id="authenticator"> <property name="authenticationStrategy"> <bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/> </property> </bean> <!--配置缓存--> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean> <!--配置Realm--> <bean id="jdbcRealm" class="org.peter.realm.MyRealm"> <!--配置密码加密--> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"/> <property name="hashIterations" value="1024"/> </bean> </property> </bean> <bean id="SecondRealm" class="org.peter.realm.SecondRealm"> <!--配置密码加密--> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="sha1"/> <property name="hashIterations" value="1024"/> </bean> </property> </bean> <!--配置Spring去管理Shiro中的bean的生命周期--> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!--开启Shiro的注解使用--> <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> <!--配置页面过滤规则--> <!--web.xml中filter-name的名字要和ShiroFilterFactoryBean的id一样,不一样会出错--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <!--配置登录页面--> <property name="loginUrl" value="/Login.jsp"/> <!--配置登录成功后的页面--> <property name="successUrl" value="/index.jsp"/> <!--配置没有获得访问权限的页面--> <property name="unauthorizedUrl" value="/s/unauthorized"/> <property name="filterChainDefinitions"> <value> <!--anon 表示Login.jsp可以匿名访问--> /Login.jsp = anon /login=anon <!--配置退出--> /logout=logout <!--authc表示剩余的页面必须在登录后访问--> /** = authc </value> </property> </bean></beans>这样就可以修改认证的策略
阅读全文
0 0
- Shiro中的加密和验证策略
- Shiro密码加密验证服务
- shiro 加密和解密
- shiro 密码加密和解密
- 【笔记】shiro中的Password加密设置:
- APP手机应用(android和ios)接口调用 传输验证可用 shiro 的 MD5、SHA 等加密
- 支持APP手机应用(android和ios)接口调用 传输验证可用 shiro 的 MD5、SHA 等加密
- 【笔记】shiro中的验证码设置:
- Shiro加密
- shiro 用户权限管理(2)-----注册md5加密,登录验证
- (九) shiro采用AES加密和解密
- Shiro (五) Base64和Md5加密
- MD5密码加密和验证
- shiro的多数据源和认证策略
- 策略模式在表单验证中的应用
- 加密策略
- 加密策略
- 加密策略
- 计算机概述
- leetcode 168. Excel Sheet Column Title | 进制转化
- 关于Serializable的serialVersionUID
- js 树形结构数据 处理
- HDU 3466 Proud Merchants 贪心+dp
- Shiro中的加密和验证策略
- HDUoj 5113 Black And White (dfs&剪枝
- 欢迎使用CSDN-markdown编辑器
- vs2013 mfc Cstring转int、int转Cstring
- Buffer
- lfw数据验证
- mysql连接不到数据库
- ScalaHbase 使用scala 操作hbase
- BIO、NIO和AIO的区别