Loading...

org.apache.shiro.crypto.CryptoException: Unable to execute ‘doFinal’ with cipher instance

在整合 shiro的时候 每次登录和登出都出现这样的问题….

org.apache.shiro.crypto.CryptoException: Unable to execute 'doFinal' with cipher instance

从两个红箭头 可以猜测出应该是rememberMe管理器的配置问题 ,而且异常部分也是rememberMe管理器里面配置的

 配置如下:

  org.apache.shiro.crypto.CryptoException: Unable to execute 'doFinal' with cipher instance

红箭头处的值有格式要求的 官文如下:

 org.apache.shiro.crypto.CryptoException: Unable to execute 'doFinal' with cipher instance

   你要么用base64生成字符串然后在decode,要么直接以0x开头的16进制窜.

 网上找到另个方法是在realm配置一下属性值 如下:

 org.apache.shiro.crypto.CryptoException: Unable to execute 'doFinal' with cipher instance

为啥好用呢 作用如下说明:

  Sets the indicator if this system’s stored credential hash is Hex encoded or not. <p/> A value of true

 will cause this class to decode the system credential from Hex, a value of false will cause this class to

 decode the system credential from Base64. <p/> Unless overridden via this method, the default value

 is true for convenience – all of Shiro’s Hash.toString() implementations return Hex encoded values by

 default, making this class’s use with those implementations easier.

如果是true就使用realm所加密的hex 否则就使用base64 hex

shiro所有配置如下:

      <?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 id=”securityManager” class=”org.apache.shiro.web.mgt.DefaultWebSecurityManager”>

        <property name=”sessionManager” ref=”sessionManager”></property>

        <property name=”cacheManager” ref=”cacheManager”></property>

        <property name=”realm” ref=”realm”></property>

    

        <!– <property name=”rememberMeManager” ref=”rememberMeManager”></property> –>

    </bean>

    <!– 配置cookies –>

    <!– <bean id=”rememberCookies” class=”org.apache.shiro.web.servlet.SimpleCookie”>

      <constructor-arg value=”rememberMe”></constructor-arg>

      <property name=”httpOnly” value=”true”></property>

      <property name=”maxAge” value=”#{60*60*24}”></property>

    </bean>

    配置记住我管理器

    <bean id=”rememberMeManager” class=”org.apache.shiro.web.mgt.CookieRememberMeManager”>

        <property name=”cipherKey” value=”#{T(org.apache.shiro.codec.Base64).decode(‘6ZmI6I2j5Y+R5aSn5ZOlAA==’)}”/>

        <property name=”cookie” ref=”rememberCookies”/>     

    </bean> –>

 <!– 配置 realm  –>

     <bean id=”realm” class=”com.yy.ssm.realm.ShiroRealm”>

         <property name=”credentialsMatcher”>

             <bean class=”org.apache.shiro.authc.credential.HashedCredentialsMatcher”>

                 <property name=”hashAlgorithmName” value=”MD5″></property>

                 <property name=”hashIterations” value=”1024″></property>

                 

                 <property name=”storedCredentialsHexEncoded” value=”true”></property>            

             </bean>

         </property>

     </bean>

    <bean id=”sessionManager”

        class=”org.apache.shiro.web.session.mgt.DefaultWebSessionManager”>

        <property name=”sessionIdUrlRewritingEnabled” value=”false”></property>

    </bean>

    <bean id=”cacheManager” class=”org.apache.shiro.cache.ehcache.EhCacheManager”>

        <property name=”cacheManagerConfigFile” value=”classpath:ehcache.xml”></property>

    </bean>

    

    <!– 配置spring 自动调用init destroy方法 –>

    <bean id=”lifeCycleProcesser” class=”org.apache.shiro.spring.LifecycleBeanPostProcessor”></bean>

    <!– 使用注解生效 –>

    <bean

        class=”org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”

        depends-on=”lifeCycleProcesser” />

    <bean

        class=”org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor”>

        <property name=”securityManager” ref=”securityManager” />

    </bean>

        <bean id=”shiroFilter” class=”org.apache.shiro.spring.web.ShiroFilterFactoryBean”>

            <property name=”securityManager” ref=”securityManager”></property>

            <property name=”loginUrl” value=”login.jsp”></property>

            <property name=”successUrl” value=”/WEB-INF/views/index.jsp”></property>

        

            <property name=”unauthorizedUrl” value=”/WEB-INF/views/fail.jsp”></property>

            <property name=”filterChainDefinitions”>

                <value>

                    /login.jsp = anon

                    /css/*=anon

                    /login=anon

                    /logout=logout

                    /add_user=anon

                    /emp-list=anon

                    /register=anon

                    /register.jsp=anon

                    /images/*=anon

                    /jquery/*=anon

                    /static/**=anon

                    /**=authc

                </value>

            </property>

        </bean>
</beans>

ps:  public HashedCredentialsMatcher() {
        this.hashAlgorithm = null;
        this.hashSalted = false;
        this.hashIterations = 1;
        this.storedCredentialsHexEncoded = true; //false means Base64-encoded
    }

 默认为true 挺懵逼的,第一个方法要清除缓存,关闭浏览器,要么直接换一个就好了.

本文来源 爱码网,其版权均为 原网址 所有 与本站无关,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源。

© 版权声明

相关文章