2014-09-24 13 views
9

में सेट नहीं है मैं वसंत ओथ और वसंत सुरक्षा के लिए काफी नया हूं। मैं अपनी परियोजना में क्लाइंट_credentials प्रवाह का उपयोग करने की कोशिश कर रहा हूँ। अभी के लिए मैं अपने सिस्टम में क्लाइंट_आईडी और पासवर्ड (गुप्त) लाने के लिए अपने स्वयं के कस्टम डिस्प्ले सेवा को उपयोगकर्ता में प्रबंधित करने में कामयाब रहा हूं जो पहले से ही मेरे सिस्टम में मौजूद है। एकमात्र समस्या यह है कि मैं DaoAuthenticationProvider में पासवर्ड एन्कोडर नहीं बदल सकता जो कि AuthorizationServer द्वारा उपयोग किया जाता है - यह डिफ़ॉल्ट रूप से PlaintextPasswordEncoder पर सेट होता है। मैं इसे इस तरह से कॉन्फ़िगर करने में सक्षम नहीं था, कि यह उदाहरण के लिए SHAPasswordEncoder का उपयोग करेगा। यह हमेशा सादा पाठ एन्कोडर का उपयोग करता है। मैं शायद प्रवाह को अच्छी तरह समझ नहीं पा रहा हूं, क्योंकि मैं वसंत में नौसिखिया हूं।वसंत Oauth2। पासवर्ड एन्कोडर DaoAuthenticationProvider

यहाँ मेरा कुछ कोड है (कार्य DaoAuthenticationProvider के विन्यास नहीं के साथ):

SecurityConfig.java

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

private static final String RESOURCE_ID = "restservice"; 

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/register/**"); 

} 

@Override 
public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(daoAuthenticationProvider()); 
} 

@Bean 
public DaoAuthenticationProvider daoAuthenticationProvider() { 
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); 
    daoAuthenticationProvider.setUserDetailsService(userDetailsService()); 
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder()); 
    return daoAuthenticationProvider; 
} 

@Bean 
public PasswordEncoder passwordEncoder() { 
    return new ShaPasswordEncoder(); 
} 

@Configuration 
@EnableAuthorizationServer 
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private MyCustomClientDetailsService myCustomClientDetailsService; 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
      throws Exception { 
     endpoints.tokenStore(tokenStore()); 
    } 

    @Bean 
    public ResourceServerTokenServices defaultTokenServices() { 
     final DefaultTokenServices defaultTokenServices = new DefaultTokenServices(); 
     defaultTokenServices.setSupportRefreshToken(true); 
     defaultTokenServices.setTokenStore(tokenStore()); 
     return defaultTokenServices; 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new InMemoryTokenStore(); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.withClientDetails(myCustomClientDetailsService); 
    } 

    @Bean 
    public MyCustomClientDetailsService detailsService() { 
     return new MyCustomClientDetailsService(); 
    } 
} 

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    ... 
} 
} 

और कस्टम ClientDetailsService वर्ग:

public class MyCustomClientDetailsService implements ClientDetailsService { 

@Autowired 
private UserService userService; 

@Override 
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException { 

    User fan = userService.getFan(clientId); 

    if (fan == null) { 
     throw new NoSuchClientException("No client with requested id: " + clientId); 
    } 

    BaseClientDetails details = new BaseClientDetails(clientId, restservice, "write", "client_credentials", "USER"); 

    details.setClientSecret(fan.getEncodedPassword()); 

    return details; 
} 
} 

encodedPassword कि लिया जाता है मेरे UserService से हमेशा एक खराब प्रमाण पत्र होता है, क्योंकि DaoAuthenticationProvider के पास डिफ़ॉल्ट रूप से एक PlaintextPasswordEncoder सेट होता है।

मुझे वहां क्या याद आ रहा है? क्या DaoAuthenticationProvider में पासवर्ड एन्कोडर सेट करना संभव है जिसका उपयोग यहां क्रेडेंशियल्स की जांच के लिए किया जाता है? या क्या मुझे अपना खुद का प्रमाणीकरण प्रदाता लिखना है, जो मुझे वैसे ही जांचने के लिए जांच करेगा?

+1

मैं ठीक उसी समस्या हो रही है क्या तुमने कभी एक ठीक मिला पर configure ओवरराइड करने के लिए है? – Leon

उत्तर

14

समाधान मैं इस समस्या पाया AuthorizationServerConfigurerAdapter

@Override 
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
    oauthServer.passwordEncoder(passwordEncoder); 
} 
+1

ध्यान दें कि मुझे इसे 'WebSecurityConfigurerAdapter' subclass' के ओवरराइड को 'कॉन्फ़िगरेशन (प्रमाणीकरण प्रबंधक बैडर ऑथ)' में सेट करना था: 'auth.userDetailsService (userDetailsService) .passwordEncoder (passwordEncoder) अन्यथा केवल क्लाइंट_सेक्रेट एन्कोड किया गया था, लेकिन नहीं उपयोगकर्ता का पासवर्डक्लाइंट_सेक्रेट संकेत के लिए –

+0

thx, मैं इसे एन्कोड करना भूल गया – davey

-1

यदि आप बस एक और पास एन्कोडर के साथ वसंत प्रमाणीकरण को कॉन्फ़िगर करना चाहते हैं तो इस कॉन्फ़िगरेशन का उपयोग करें।

<bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/> 

<authentication-manager alias="authenticationManager"> 
     <authentication-provider user-service-ref="authenticationService"> 
     <password-encoder ref ="encoder" /> 

      <!-- <user-service> 
       <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/> 
      </user-service> --> 
     </authentication-provider> 
    </authentication-manager> 

नोट: - के दौरान उपयोगकर्ता आप इस एक ही पासवर्ड का एनकोडर वर्ग के साथ userpassword एन्क्रिप्ट करना पैदा करते हैं।

+0

क्या मैं वही काम नहीं कर रहा हूं लेकिन जावा आधारित कॉन्फ़िगरेशन का उपयोग कर रहा हूं। मैं प्रमाणीकरण स्थापित कर रहा हूंप्रदाता: auth.authenticationProvider (daoAuthenticationProvider()); जिसे पहले SHAPasswordEncoder का उपयोग करने के लिए कॉन्फ़िगर किया गया था। या मैं इसके बारे में गलत हूँ? – gajos

+0

आप किसी भी पासवर्ड एन्कोडर के साथ कॉन्फ़िगर कर सकते हैं। बस याद रखें कि उपयोगकर्ता पासवर्ड को उसी पासवर्ड एन्कोडर क्लास एन्कोड विधि का उपयोग करके एन्क्रिप्ट किया जाना चाहिए। –

+0

एक्सएमएल विन्यास हम एक और passwordEncoder, उल्लेख कर सकते हैं में <सेम आईडी = "daoAuthenticationProvider" वर्ग = "org.springframework.security.providers.dao.DaoAuthenticationProvider"> <संपत्ति नाम = "userDetailsService" रेफरी = "inMemoryDaoImpl" /> <संपत्ति का नाम = "नमक स्रोत" रेफ बीन = "नमक स्रोत" /> <संपत्ति का नाम = "पासवर्ड एन्कोडर" ref = "passwordEncoder" /> –

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