Shiro中的加密和验证策略

上篇中我们说了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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值