2011-12-15 13 views
31

Have config (applicationContext-security.xml):स्प्रिंग सुरक्षा: डीबी में और applicationContext में पासवर्ड एन्कोडिंग

... 
    public static final String DEF_USERS_BY_USERNAME_QUERY = 
      "select username,password,enabled " + 
      "from users " + 
      "where username = ?"; 
... 

: दूसरी तरफ से

<authentication-manager alias="authenticationManager"> 
    <authentication-provider> 
    <password-encoder hash="sha"/> 
     <jdbc-user-service data-source-ref="dataSource"/> 
    </authentication-provider> 
</authentication-manager> 

मेरी dataSource (यह JdbcDaoImpl है) से SQLs है इस कोड में अब sha के बारे में शब्द है, इसलिए मानक स्प्रिंग सुरक्षा users तालिका से चयनित पासवर्ड एन्कोड नहीं किया गया है। अब उसी रूप में डीबी को बचाया पासवर्ड के लिए

<class name="model.UserDetails" table="users"> 
    <id name="id"> 
     <generator class="increment"/> 
    </id> 
    <property name="username" column="username"/> 
    <property name="password" column="password"/> 
    <property name="enabled" column="enabled"/> 
    <property name="mail" column="mail"/> 
    <property name="city" column="city"/> 
    <property name="confirmed" column="confirmed"/> 
    <property name="confirmationCode" column="confirmation_code"/> 

    <set name="authorities" cascade="all" inverse="true"> 
     <key column="id" not-null="true"/> 
     <one-to-many class="model.Authority"/> 
    </set> 

</class> 

, लेकिन इनकोडिंग किया जाना चाहिए:

शायद, मैं password स्तंभ के लिए कुछ sha विशेषता मेरी हाइबरनेट मानचित्रण config में यहाँ प्रदान करना चाहिए।

applicationContext कॉन्फ़िगरेशन और डीबी प्रश्नों को एक ही पासवर्ड एन्कोडिंग के रूप में कैसे करें? datasource को service और service अंक के authentication-provider अंक:

उत्तर

73

यदि आप किसी मौजूदा डेटाबेस जो पहले से ही टुकड़ों में बंटी पासवर्ड होते का उपयोग करके ऐप्लिकेशन का निर्माण एक हैशिंग प्रणाली अपने आप को चुन रहे हैं, तो बजाय, तो आप सुनिश्चित करें कि आपके हैशिंग एल्गोरिथ्म भी एक नमक का उपयोग करता है बनाना चाहिए। एक सादा पाचन का उपयोग न करें।

अच्छा चुनाव जो हम अब BCryptPasswordEncoder (jBCrypt का उपयोग करके लागू) के माध्यम से वसंत सुरक्षा 3.1 में सीधे समर्थन bcrypt है। यह स्वचालित रूप से एक नमक उत्पन्न करता है और इसे एक स्ट्रिंग में हैश मान के साथ जोड़ता है।

कुछ डेटाबेस में हैशिंग के लिए अंतर्निहित समर्थन है (उदा। Postgres)। अन्यथा, आप यह JDBC को पार करने से पहले पासवर्ड खुद हैश करने के लिए की जरूरत है:

String password = "plaintextPassword"; 
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 
String hashedPassword = passwordEncoder.encode(password); 

सब आप पासवर्ड एन्कोड करने के लिए जब आप एक उपयोगकर्ता बनाने के क्या करने की जरूरत है यही कारण है कि।

प्रमाणीकरण के लिए, आप की तरह कुछ का प्रयोग करेंगे:

<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 

<bean id="authProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <property name="userDetailsService" ref="yourJdbcUserService" /> 
    <property name="passwordEncoder" ref="encoder" /> 
</bean> 
+0

ल्यूक टेलर, धन्यवाद, कि मैं जानना चाहता था - कि मुझे इसे डीबी में डालने से पहले हैश पासवर्ड होना चाहिए। – sergionni

+0

नमक के बारे में, मुझे पता है, यह मेरी सुरक्षा चुनौती का अगला चरण है)) – sergionni

+4

यदि आप bcrypt की तरह कुछ उपयोग करते हैं, तो यह स्वचालित रूप से आपके लिए संभाला जाता है, इसलिए वास्तव में कोई चुनौती नहीं है :-)। आपको खुद को नमक को संभालना नहीं है। –

5

स्प्रिंग सुरक्षा 3.1 का उपयोग करना, इस प्रयास करें:

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="service"> 
     <password-encoder hash="sha"/> 
     <jdbc-user-service data-source-ref="dataSource"/> 
    </authentication-provider> 
</authentication-manager> 

<beans:bean id="dataSource" ...> 
    ... 
</beans:bean> 

<beans:bean id="service" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> 
     <beans:property name="dataSource" ref="dataSource"/> 
     ... 
</beans:bean> 

नया क्या है।

संपादित करें: जावा में आप कुछ इस तरह के साथ पासवर्ड सांकेतिक शब्दों में बदलना होगा:

DigestUtils.sha(request.getParameter("password")); 

चेतावनी: सावधान रहें! SHAMD5 के साथ मिश्रण न करें!

आप authentication-provider रूप की password-encoder SHA सेट करते हैं, आप जावा में एक ही तरह से बनाए रखने के लिए सांकेतिक शब्दों में बदलना करने की जरूरत है। लेकिन यदि आप जावा में MD5 के रूप में एन्कॉन्डे करते हैं, तो आपको मिले नमूने के रूप में, हैश "md5" पर सेट करना न भूलें। DigestUtils भी md5 एनकोडर प्रदान करता है:

DigestUtils.md5(request.getParameter("password")); 
+0

हाँ, मुझे पता है के साथ यह करने के लिए एक टिप है, लेकिन आप प्रदान करने के लिए कैसे, उस उपयोगकर्ता पासवर्ड डीबी – sergionni

+1

खैर के लिए इनकोडिंग सहेजे मेरे सवाल पर नहीं पूछा, मुझे नहीं पता जानें कि प्रदान करके आपका क्या मतलब है, लेकिन जारी रखने के लिए, मैंने [DigestUtils] (http://commons.apache.org/codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html) का उपयोग किया है। – falsarella

+1

@sergionni: क्षमा करें, लेकिन मुझे एन्कोडेड पासवर्ड को स्वचालित रूप से सहेजने के लिए एप्लिकेशन कॉन्टेक्स्ट की कॉन्फ़िगरेशन नहीं पता है, जैसा कि मैंने कहा था, मैंने अपाचे कॉमन्स से डाइजेस्ट्यूट का उपयोग किया है। – falsarella

8

एक छोटे से अधिक स्पष्टीकरण स्वीकार किए जाते हैं जवाब पर। उम्मीद है कि यह किसी की मदद करता है।

हैश यह डेटाबेस के लिए डालने से पहले पासवर्ड अपने आप को:

String password = "plaintextPassword"; 
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 
String hashedPassword = passwordEncoder.encode(password); 

अपनी सुरक्षा-config.xml को BCryptPasswordEncoder सेम जोड़ें

प्रमाणीकरण प्रदाता वर्ग के लिए एक संपत्ति के रूप में passwordEncoder जोड़ें। इसे स्वचालित करें या सेटटर और गेटटर विधियां प्रदान करें।

@AutoWired 
private BCryptPasswordEncoder passwordEncoder; 

संपत्ति प्राप्त करें कि लॉगिन

<bean id="dbAuthenticationProvider" class="mypackage.auth.spring.DBAuthenticationProvider" > 
    <property name="dataSource" ref="routingDataSource"></property> 
    <property name="passwordEncoder" ref="encoder" /> 
    <property name="passwordQuery" 
     value="select password as password from tbl where username=:username"> 
    </property> 
</bean> 

के लिए और प्रमाणीकरण वर्ग के मैच में दोनों पासवर्ड

new BCryptPasswordEncoder().matches(plainTextPasswdFromUserInput, hashedPasswdFromDb) 
1

3.1.x के साथ प्रमाणन के लिए इस मानचित्रण does not काम आप authendicate उपयोगकर्ता। कार्य तरीका है:

<beans:bean id='bCryptPasswordEncoder' class='org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'></beans:bean> 

<authentication-manager> 
    <authentication-provider user-service-ref="userDetailsService"> 
      <password-encoder ref="bCryptPasswordEncoder"/> 
    </authentication-provider> 
</authentication-manager> 
5

एक आसान तरीका में आप applicationContext-security.xml में की तरह कुछ कर सकते हैं

<authentication-manager alias="authenticationManager"> 
    <authentication-provider> 
    <password-encoder ref="encoder"/> 
    <jdbc-user-service data-source-ref="dataSource" 
     users-by-username-query=" 
      select username,password, enabled 
      from principal where username=?" 
     authorities-by-username-query=" 
      select p.username, a.authority from principal p, authority a 
      where p.id = a.principal_id and p.username=?" 
    /> 
    </authentication-provider> 
</authentication-manager> 

    <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 

जावा में

public static String encodePasswordWithBCrypt(String plainPassword){ 
    return new BCryptPasswordEncoder().encode(plainPassword); 
} 

तो यह

System.out.println(encodePasswordWithBCrypt("fsdfd")); 
का परीक्षण
2

स्वीकृत उत्तर सही है। मैंने इसे वसंत 3.1 और बीक्रिप्ट एन्कोड एल्गोरिदम के साथ परीक्षण किया।

उपयोगकर्ता बनाते समय।

PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 
userEntity.setPassword(passwordEncoder.encode(userEntity.getPassword())); 
userDao.save(userEntity); 

जब उपयोगकर्ता लॉगिन, याद रखें, सादा पासवर्ड ( टुकड़ों में बांटा नहीं) का उपयोग करें। जैसे:

Authentication request = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword()); 
Authentication result = authenticationManager.authenticate(request); 
SecurityContextHolder.getContext().setAuthentication(result); 

यहाँ सुरक्षा-config है:

<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 

<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
     <property name="userDetailsService" ref="userService" /> 
     <property name="hideUserNotFoundExceptions" value="false" /> 
     <property name="passwordEncoder" ref="encoder" /> 
    </bean> 

आशा है कि यह किसी को मदद मिलेगी!

2

बस एनोटेशन

@Configuration 
@EnableWebSecurity 
@PropertySource("classpath://configs.properties") 
public class SecurityContextConfig extends WebSecurityConfigurerAdapter { 


@Autowired 
@Qualifier("userDetailsService") 
private UserDetailsService userDetailsService; 

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder()); 
} 


@Bean(name = "passwordEncoder") 
public PasswordEncoder getPasswordEncoder(){ 
    return new BCryptPasswordEncoder();  
} 

} 
संबंधित मुद्दे