LaravelÖÐÔõÑùÉèÖÃÓû§ÃÜÂë
Ëæ×Å»¥ÁªÍøµÄ·ÉËÙÉú³¤£¬Óû§µÄСÎÒ˽ÈËÐÅÏ¢Çå¾²³ÉΪԽÀ´Ô½Ö÷ÒªµÄ»°ÌâÖ®Ò»¡£ÆäÖУ¬ÕË»§ÃÜÂëÉèÖþÍÏÔµÃÓÈΪÖ÷Òª¡£×÷Ϊһ¸öʹÓà laravel ¿ª·¢ÍøÕ¾»òÓ¦ÓóÌÐòµÄ¿ª·¢Õߣ¬ÄãÐèҪȷ±£ÄãËù¿ª·¢µÄÓ¦ÓóÌÐòÌṩÁË×ã¹»µÄÇå¾²°ü¹Ü£¬ÒÔ±£»¤Óû§µÄСÎÒ˽ÈËÐÅÏ¢¡£Òò´Ë£¬±¾ÎĽ«Îª¸÷ÈËÏÈÈÝ laravel ÖÐÔõÑùÉèÖÃÓû§ÃÜÂë¡£
Laravel ΪÎÒÃÇÌṩÁËһЩ»ù±¾µÄ¼ÓÃÜÀ࣬ÆäÖаüÀ¨ Hash ºÍ Bcrypt ¼ÓÃÜ¡£ÕâЩ¼ÓÃÜÀà¿ÉÒÔÓÃÓÚ¶ÔÓû§ÃÜÂë¾ÙÐйþÏ£¼ÓÃÜ´¦Àí£¬´Ó¶ø°ü¹ÜÓû§ÃÜÂëµÄÇå¾²ÐÔ¡£ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ·½·¨½«ÃÜÂë¾ÙÐйþÏ£¼ÓÃÜ£º
$hashedPassword = Hash::make('password');
µÇ¼ºó¸´ÖÆ
ͨ¹ýÕâ¸ö¼òÆӵĴúÂ룬Äã¿ÉÒÔ½«ÔʼÃÜÂë password ¾ÙÐмÓÃÜ´¦Àí£¬²¢·µ»ØÒ»¸ö¹þÏ£×Ö·û´®¡£Õâ¸ö¹þÏ£×Ö·û´®¾ÍÊÇÎÒÃÇ´æ´¢ÔÚÊý¾Ý¿âÖеÄÃÜÂë¡£µ±Óû§µÇ¼ʱ£¬ÎÒÃÇÐèÒª½«ÆäÊäÈëµÄÃÜÂëÓëÊý¾Ý¿âÖеĹþÏ£×Ö·û´®¾ÙÐбȶÔÀ´ÑéÖ¤ÆäÉí·Ý¡£
±ðµÄ£¬Laravel »¹ÌṩÁËÒ»¸ö check() ÒªÁ죬ÓÃÓÚÑéÖ¤ÃÜÂëÊÇ·ñÓë¹þÏ£×Ö·û´®Ò»Ö£¬Ê¾ÀýÈçÏ£º
$hashedPassword = '$2y$10$VvMCYuh0JSzJkmKHjKw/8OETs/75WiyrMphIMkto.UoS6Nc1C9X06'; if (Hash::check('password', $hashedPassword)) { // ÑéÖ¤ÀÖ³É } else { // Ñé֤ʧ°Ü }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý check() ÒªÁ콫ÔʼÃÜÂë password Óë¹þÏ£×Ö·û´®¾ÙÐбȶԣ¬ÈôÊÇÁ½ÕßÒ»Ö£¬ÔòÌåÏÖÃÜÂëÑéÖ¤Àֳɡ£
³ýÁËÉÏÊöʾÀýÖеĹþÏ£¼ÓÃÜÒªÁìÍ⣬Laravel »¹ÌṩÁË BCrypt¡¢Argon2 ºÍ Sodium µÈ¼ÓÃÜ·½·¨£¬Ê¹ÓÃÒªÁìÒ²»ùÄÚÇéËÆ£¬Ö»ÐèŲÓöÔÓ¦µÄº¯Êý¼´¿É¡£ÈôÊÇÐèÒªÔ½·¢ÏêϸµÄÏÈÈÝ£¬¿ÉÒÔÉó²é Laravel ÎĵµÖеļÓÃܲ¿·Ö¡£
³ýÁËʹÓùþÏ£¼ÓÃÜÀ´±£»¤Óû§µÄÃÜÂ룬ÉÐÓÐһЩÆäËûµÄÇå¾²²½·¥¿ÉÒÔÓÃÓÚÌá¸ßÓû§ÃÜÂëµÄÇå¾²ÐÔ¡£ÏÂÃæÎÒÃÇÀ´¼òÆÓÏÈÈÝһЩÊÊÓõÄÃÜÂë±£»¤²½·¥¡£
ÃÜÂ볤¶È
ÃÜÂëµÄ³¤¶ÈÊÇ°ü¹ÜÃÜÂëÇå¾²ÐÔµÄÖ÷ÒªÒòËØÖ®Ò»¡£Í¨³£À´Ëµ£¬Ò»¸öÇ¿ÃÜÂëÓ¦¸ÃÖÁÉÙ°üÀ¨ 8 ¸ö×Ö·û£¬ÇÒÓ¦½ÓÄɾÞϸд×Öĸ¡¢Êý×ÖÒÔ¼°·ûºÅ×éºÏ¶ø³É¡£Äã¿ÉÒÔ½èÖú Laravel µÄ Str ÀàÀ´ÌìÉúËæÉñÃØÂ룬ʾÀý´úÂëÈçÏ£º
$password = Str::random(12); // ÌìÉúÒ»¸ö°üÀ¨ 12 ¸ö×Ö·ûµÄËæ»ú´®
µÇ¼ºó¸´ÖÆ
ÃÜÂëÕ½ÂÔ
³ýÁËÃÜÂ볤¶ÈÍ⣬ÃÜÂëÕ½ÂÔÒ²ÊÇÌá¸ßÃÜÂëÇå¾²ÐÔµÄÖ÷ÒªÒòËØ¡£Äã¿ÉÒÔÏÞÖÆÓû§Ê¹ÓÃÌض¨µÄ×Ö·û¼¯À´ÉèÖÃÃÜÂ룬ºÃ±ÈÖ»ÔÊÐíʹÓÃÊý×Ö¡¢×Öĸ¡¢·ûºÅµÈ£¬Õ¥È¡Ê¹ÓÿոñºÍÖÐÎÄ×Ö·ûµÈ¡£
ÔÚ Laravel ÖУ¬Äã¿ÉÒÔʹÓà Regex ¹æÔòÀ´ÏÞÖÆÓû§ÃÜÂëµÄ×Ö·û¼¯£¬Ê¾Àý´úÂëÈçÏ£º
$rules = [ 'password' => [ 'required', 'regex:/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!$%@#?€*?&]{8,}$/' ] ];
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëʹÓÃÕýÔò±í´ïʽÏÞÖÆÁËÃÜÂë±ØÐè°üÀ¨×ÖĸºÍÊý×Ö£¬ÇÒ³¤¶ÈÖÁÉÙΪ8¸ö×Ö·û¡£
ÃÜÂë¼ÓÑÎ
ÃÜÂë¼ÓÑÎÊÇÒ»ÖÖ³£¼ûµÄÃÜÂë±£»¤·½·¨¡£¼ÓÑοÉÒÔʹ¹þÏ£¸üÄѱ»ÍƲâ»òÆƽ⡣ÔÚ Laravel ÖУ¬ÃÜÂë¼ÓÑοÉÒÔͨ¹ý Hash::make() ÒªÁìÖеĵڶþ¸ö²ÎÊý¾ÙÐÐÉèÖã¬Ê¾Àý´úÂëÈçÏ£º
$hashedPassword = Hash::make('password', ['salt' => 'your_salt']);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ýµÚ¶þ¸ö²ÎÊýÉèÖÃÁËÃÜÂëµÄÑÎֵΪ your_salt¡£ÕâÑù¾Í¿ÉÒÔÔöÌíÃÜÂëµÄÇå¾²ÐÔ¡£
×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÐÅÍи÷ÈË¶Ô Laravel ÖÐÔõÑùÉèÖÃÓû§ÃÜÂëÓÐÁËÒ»¶¨µÄÏàʶ¡£ÃÜÂëÇå¾²ÊÇÒ»¸ö¾³£±»ºöÊÓµÄÎÊÌ⣬¿ÉÊÇÔÚ¶ÔÓû§Ð¡ÎÒ˽ÈËÐÅÏ¢¾ÙÐб£»¤ÉÏÊDz»¿É»òȱµÄÒ»²¿·Ö¡£×÷Ϊ¿ª·¢Õߣ¬ÎÒÃÇÓ¦¸Ã¾¡¿ÉÄܵØÌá¸ßÓ¦ÓóÌÐòµÄÇå¾²ÐÔ£¬ÎªÓû§Ìṩ×îºÃµÄÇå¾²°ü¹Ü¡£
ÒÔÉϾÍÊÇLaravelÖÐÔõÑùÉèÖÃÓû§ÃÜÂëµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡