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


 

在整合 shiro的時候 每次登錄和登出都出現這樣的問題....

 

從兩個紅箭頭 可以猜測出應該是rememberMe管理器的配置問題 ,而且異常部分也是rememberMe管理器里面配置的

 配置如下:

 

紅箭頭處的值有格式要求的 官文如下:

 

   你要么用base64生成字符串然后在decode,要么直接以0x開頭的16進制竄.

 網上找到另個方法是在realm配置一下屬性值 如下:

 

為啥好用呢 作用如下說明:

  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 挺懵逼的,第一個方法要清除緩存,關閉瀏覽器,要么直接換一個就好了.

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_21727627/article/details/72850391

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM