zoukankan      html  css  js  c++  java
  • Symfony2 UserSecurityEncoder实现自己的验证方式

    fosuserbundle默认使用sha512加密
    
    如果要实现自己的加密方式 需要继承SymfonyComponentSecurityCoreEncoderBasePasswordEncoder
    
    <?php
    
    namespace McAdminBundleSecurityEncoder;
    
    use SymfonyComponentSecurityCoreEncoderBasePasswordEncoder;
    use SymfonyComponentSecurityCoreExceptionBadCredentialsException;
    
    class JoomlaPasswordEncoder extends BasePasswordEncoder
    {
        private $cost;
    
        public function __construct( $cost)
        {
            $cost = intval( $cost);
            if( $cost < 4 || $cost > 31 )
            {
                throw new InvalidArgumentException('Cost too long , it must be in the range of 4-31');
            }
            $this->cost = sprintf('%02d' , $cost);
        }
    
        public function encodePassword( $raw , $salt = null )
        {
            if( $this->isPasswordTooLong($raw) )
            {
                throw new BadCredentialsException('Invalid password.');
            }
            return md5( md5( $raw ) . $salt );
        }
    
        public function isPasswordValid($encoded, $raw, $salt = null)
        {
            if ($this->isPasswordTooLong($raw)) 
            {
                return false;
            }
    
    
            return md5( md5( $raw).$salt) === $encoded;
        }
    }
    然后写入service
    
    在bundle下面的Resources/config/services.yml(或者xml)添加一个服务:
    
        mc_user.security.core.encoder:
            class: McAdminBundleSecurityEncoderJoomlaPasswordEncoder
            arguments: [6]
    也可以在DependencyInjection/Configuration.php中添加参数:
    
            $rootNode->children()
                        ->scalarNode('cost')->defaultValue(6)->end()
                        ->end()
            ;
    最后在app/config/security.yml中设置自己的加密方式 这里用户组件是FOSUserBundle:
    
    security:
        encoders:
            SymfonyComponentSecurityCoreUserUser: plaintext
            FOSUserBundleModelUserInterface:
                id: mc_user.security.core.encoder
    这里的id是service名 即 mc_user.encoder
    
    done
  • 相关阅读:
    MyEclipse中的Tomcat跑大项目时内存溢出:permgen space
    MyEclipse新建工作空间后的配置详细步骤
    解决eclipse复制粘贴js代码卡死的问题
    eclipse复制工作空间配置
    maven项目检出后报错(包括编译报错和运行报错)的常见检查处理方式
    MyEclipse中引用的maven配置文件只访问私服的配置
    图标网站
    afasf
    一个权限管理模块的设计(转载)
    奇艺下载
  • 原文地址:https://www.cnblogs.com/huidaoli/p/3980497.html
Copyright © 2011-2022 走看看