รหัสผ่านเข้ารหัสความปลอดภัยสปริงด้วยอัลกอริธึม bcrypt

ฉันได้รับสิ่งแปลก ๆ... ในการรักษาความปลอดภัยฤดูใบไม้ผลิสำหรับการเข้ารหัสรหัสผ่าน ..

ฉันกำลังพยายามเปลี่ยนรหัสผ่านและบันทึกลงในฐานข้อมูล..แต่ฉันได้รับข้อผิดพลาดเสมอเนื่องจากสตริงที่ต่างกัน..

แบบนี้..

ในตัวควบคุม ..

println "password  = "+oldPass
println "password 1 = "+springSecurityService.encodePassword('password')
println "password 2 = "+springSecurityService.encodePassword('password')
println "password  = "+springSecurityService.encodePassword(oldPass)

และผลลัพธ์นี้

ป้อนคำอธิบายรูปภาพที่นี่

มันแปลก...ทุกครั้งที่ฉันเข้ารหัสรหัสผ่าน ฉันจะได้ผลลัพธ์ที่แตกต่างออกไป

ฉันใช้ grails 3.0.5 และใช้อัลกอริธึม bcrypt

grails.plugin.springsecurity.password.algorithm = 'bcrypt'

ฉันใส่บรรทัดนี้ใน application.groovy

แบบนี้

    // Added by the Spring Security Core plugin:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.akiong.security.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'com.akiong.security.UserRole'
grails.plugin.springsecurity.authority.className = 'com.akiong.security.Role'
grails.plugin.springsecurity.requestMap.className = 'com.akiong.security.RequestMap'
grails.plugin.springsecurity.securityConfigType = 'Requestmap'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    '/':                ['permitAll'],
    '/error':           ['permitAll'],
    '/index':           ['permitAll'],
    '/index.gsp':       ['permitAll'],
    '/shutdown':        ['permitAll'],
    '/assets/**':       ['permitAll'],
    '/**/js/**':        ['permitAll'],
    '/**/css/**':       ['permitAll'],
    '/**/images/**':    ['permitAll'],
    '/**/favicon.ico':  ['permitAll']
]
grails.plugin.springsecurity.password.algorithm = 'bcrypt'

แต่เมื่อฉันสร้างบัญชีผู้ใช้ด้วย bootstrap และบันทึกลงในฐานข้อมูล.. จากนั้นฉันก็เข้าสู่ระบบ ...มันทำงานอย่างถูกต้อง..

ป้อนคำอธิบายรูปภาพที่นี่

ป้อนคำอธิบายรูปภาพที่นี่


person Community    schedule 23.10.2015    source แหล่งที่มา
comment
อย่างไรก็ตาม มันง่ายกว่ามากสำหรับคนอื่นถ้าคุณใส่ตัวอย่างโค้ด เอาต์พุต ฯลฯ ในรูปแบบข้อความธรรมดาแทนที่จะเป็นรูปภาพ จึงสามารถอ้างอิงหรือนำไปใช้ทดลองก่อนตอบได้   -  person Igor Artamonov    schedule 23.10.2015


คำตอบ (1)


มันเป็นคุณลักษณะ bcrypt ใช้เกลือแบบสุ่ม ดังนั้นในแต่ละครั้งจะสร้างแฮชที่แตกต่างกันแม้จะใช้รหัสผ่านเดียวกันก็ตาม

หากคุณต้องการตรวจสอบว่ารหัสผ่านที่ป้อนนั้นถูกต้องหรือไม่ คุณต้องใช้ passwordEncoder.isPasswordvalid สำหรับ Grails เช่น:

assert passwordEncoder.isPasswordValid( 
       '$2a$10$Qb7ENpWOSsFUS2UvwT1BRefZhn55roXPgUI8fjJRm6c/nR3JIQP8a',
       'password', null)
assert passwordEncoder.isPasswordValid(
       '$2a$10$sC3.yrmNn2VLS2Aer359rei/DxoLlwFq7s6ndAHm10ncyQpIr3MfO',
       'password', null)

หรือสำหรับ Spring Security ธรรมดา passwordEncoder.matches:

assert passwordEncoder.matches('password', 
       '$2a$10$Qb7ENpWOSsFUS2UvwT1BRefZhn55roXPgUI8fjJRm6c/nR3JIQP8a')
assert passwordEncoder.matches('password', 
       '$2a$10$sC3.yrmNn2VLS2Aer359rei/DxoLlwFq7s6ndAHm10ncyQpIr3MfO')

หากต้องการ autowire passwordEncoder bean เพียงกำหนดให้มันเป็นคุณสมบัติของคลาสของคุณ:

def passwordEncoder
person Igor Artamonov    schedule 23.10.2015
comment
รหัสผ่าน println 1 = +passwordEncoder.matches('password','$2a$10$JNUJ3pRiwnOMkVYPRpbdeujyuBLTavozVLinHRnf5MK8VHkif2IGG')... ฉันไม่สามารถใช้passwordEncoderได้ - person ; 23.10.2015
comment
คุณเชื่อมต่อมันเข้ากับบริการ / คอนโทรลเลอร์ / ฯลฯ ของคุณหรือไม่? มันเป็นถั่วที่ให้มาในฤดูใบไม้ผลิ - person Igor Artamonov; 23.10.2015
comment
ยังไง? ฉันไม่รู้ .. ฉันแค่เพิ่ม grails.plugin.springsecurity.password.algorithm = 'bcrypt' ใน application.groovy - person ; 23.10.2015
comment
โอ้ คุณหมายถึง call def springSecurityService ใช่หรือไม่ ฉันต่อสายแล้ว...นั่นคือเหตุผลว่าทำไมฉันจึงใช้ springSecurityService.encodePassword ได้ - person ; 23.10.2015
comment
ไม่ ฉันหมายถึง passwordEncoder ไม่ใช่ springSecurityService นอกจากนี้ จริงๆ แล้วฉันคิดว่า Grails มีการใช้งานที่แตกต่างกัน ดังนั้นเมธอดจึงสามารถเรียกว่า .isPasswordValid(hashed, 'password') ได้ ดูการอัปเดตคำตอบ - person Igor Artamonov; 23.10.2015
comment
ดูโพสต์อัปเดตของฉัน..ฉันได้รับข้อผิดพลาดเช่นนั้น... ฉันได้เพิ่ม defpasswordEncoder หลังจาก def cityService แล้ว - person ; 23.10.2015
comment
เมื่อฉันลองยืนยันนี้ passwordEncoder.matches('password','$2a$10$JNUJ3pRiwnOMkVYPRpbdeujyuBLTavozVLinHRnf5MK8VHkif2IGG').. ฉันได้รับข้อผิดพลาดนี้ No signature of method: grails.plugin.springsecurity.authentication.encoding.BCryptPasswordEncoder.matches() - person ; 23.10.2015
comment
ใช่ ลอง isPasswordValid(hashed, 'password', null) - person Igor Artamonov; 23.10.2015
comment
ฉันจะรู้ได้ไหม isPasswordValid(<this is encrypted string>, <this is real string>,<what is this?>)? - person ; 23.10.2015
comment
isPasswordValid(hashed, plainpassword, salt). คุณไม่สามารถใช้เกลือสำหรับ bcrypt ได้ ดังนั้นจึงต้องเป็น null - person Igor Artamonov; 23.10.2015
comment
หาก bcrypt ใช้เกลือแบบสุ่มในแต่ละครั้ง จะทราบได้อย่างไรว่าผู้ใช้ป้อนรหัสผ่านที่ถูกต้อง มันจะแฮชกับสิ่งที่แตกต่างออกไปทุกครั้ง...ฉันไม่เข้าใจ - person temporary_user_name; 24.05.2016
comment
@Aerovistae เพิ่งได้รับเกลือนี้จากแฮชของเขา มันเป็น 128 บิต / 22 ตัวอักษรแรกจากแฮช หรือใช้ให้ดีกว่า passwordEncoder.matches - person Igor Artamonov; 25.05.2016