zoukankan      html  css  js  c++  java
  • 每天学点Shiro-多realm

    1. 新建第二个realm,加密算法改为SHA1

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
                throws AuthenticationException {
    
            System.out.println("=========>SecondRealm doGetAuthenticationInfo");
            UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            String username = token.getUsername() ;
    
            if("unknown".equals(username)){
                throw new UnknownAccountException("用户名不存在") ;
            }
    
            Object principal = username ;
            Object credentials= "3416bb24c2d3e88cc79e261ec63c1c324e6614b9" ;
            ByteSource credentialsSalt = ByteSource.Util.bytes(username);
            String realmName = getName() ;
    
            AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(principal,credentials,credentialsSalt,realmName) ;
    
            return authenticationInfo;
        }

     2. spring-context-shiro.xml中对secondRealm进行配置

         2.1 声明SecondRealm

    <bean id="secondRealm" class="com.pawn.shiro.realm.SecondRealm">
            <property name="credentialsMatcher">
                <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                    <property name="hashAlgorithmName" value="SHA1"/>
                    <property name="hashIterations" value="1"/>
                </bean>
            </property>
        </bean>

        2.2 SecurityManager进行多Realm的配置

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <property name="cacheManager" ref="cacheManager"/>
            <property name="authenticator">
                <bean class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
                    <property name="realms">
                        <list>
                            <ref bean="jdbcRealm"/>
                            <ref bean="secondRealm"/>
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
  • 相关阅读:
    软件测试人员的年终绩效考核怎么应对
    收藏
    顶踩组件 前后两版
    订阅组件
    hdu 1963 Investment 完全背包
    hdu 4939 Stupid Tower Defense 动态规划
    hdu 4405 Aeroplane chess 动态规划
    cf 414B Mashmokh and ACM 动态规划
    BUPT 202 Chocolate Machine 动态规划
    hdu 3853 LOOPS 动态规划
  • 原文地址:https://www.cnblogs.com/xpawn/p/7616040.html
Copyright © 2011-2022 走看看