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>
这样就可以修改认证的策略


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 u盘在电脑上打不开怎么办 电脑桌面上文档剪切了怎么办 苹果官网查不到保修日期怎么办 吃了发霉的花生怎么办 鸡吃大蒜多了怎么办 玖瑰花叶子黄怎么办 羊偷吃腥油和花生饼吃多了怎么办 肉牛眼睛有点变黄少吃东西怎么办 黑坑草鱼不开口怎么办 花生和瓜子受潮皮了怎么办? 菜叶上长了腻虫怎么办 磨辊耐磨层脱落怎么办 磨辊耐磨层小块脱落怎么办 密封胶皮圈松了怎么办 汽筒里胶皮垫密封不严怎么办 磁耦气缸脱磁了怎么办 无杆气缸行程大怎么办 c4d中模型变成线怎么办 内径槽异性需要车一刀怎么办 轴承太紧影响转速怎么办 电动车前轮蝶刹抱死怎么办 摩托三轮车油刹抱死怎么办 手动档汽车离合抱死怎么办 别克gl8后轮吃胎怎么办 扭力梁后轮吃胎怎么办 非独立悬挂吃胎怎么办 货车半轴法兰盘裂纹怎么办 小天才平板裂屏了怎么办 新车撞了个坑怎么办 新车碰了个坑怎么办 汽车顶被砸了个坑怎么办 途观l前减震异响怎么办 锦明8代声音太大怎么办 手机网页无法加载插件怎么办 微信公众号被投诉怎么办 住了酒店的尾房怎么办 喜欢前任的闺蜜怎么办 闺蜜给介绍对象怎么办 喜欢对象的发小怎么办 山东省直医保卡丢失怎么办 高铁票如果错过了怎么办