2014-09-24 89 views
9

我對Spring Oauth和Spring Security頗爲陌生。我正在嘗試在我的項目中使用client_credentials流程。現在我設法使用我自己的CustomDetailsS​​ervice來從我的系統中已經存在的數據庫中獲取client_id和密碼(祕密)。唯一的問題是我無法更改AuthorizationServer使用的DaoAuthenticationProvider中的密碼編碼器 - 它默認設置爲PlaintextPasswordEncoder。我無法按照它的方式配置它,例如SHAPasswordEncoder。它總是使用明文編碼器。我可能不太瞭解這個流程,因爲我是Spring的新手。Spring 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 { 

    ... 
} 
} 

而定製ClientDetailsS​​ervice類:

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中設置用於檢查憑證的密碼編碼器?或者我必須編寫自己的AuthenticationProvider,它會按照我想要的方式進行檢查?

+1

我有完全相同的問題你有沒有找到一個解決覆蓋configure? – Leon 2015-10-04 11:47:52

回答

14

,我發現這個問題的解決辦法是在AuthorizationServerConfigurerAdapter

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

請注意,我還必須在'WebControlsConfigurerAdapter'子類的'configure(AuthenticationManagerBuilder auth)'覆蓋中像這樣設置它:'auth.userDetailsS​​ervice(userDetailsS​​ervice).passwordEncoder(passwordEncoder)'否則只有client_secret被編碼,但不是用戶的密碼。 – 2016-02-04 19:46:10

+0

thx爲client_secret提示,我忘了編碼它 – davey 2017-06-25 14:19:59

-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

我不是在做同樣的事情,而是使用基於Java的配置。我正在設置authenticationProvider: auth.authenticationProvider(daoAuthenticationProvider()); 以前配置爲使用SHAPasswordEncoder。或者我錯了嗎? – gajos 2014-09-24 11:02:59

+0

您可以使用任何passwordEncoder進行配置。只要記住用戶密碼應該使用相同的passwordEncoder類編碼方法進行加密。 – 2014-09-24 11:33:31

+0

在XML配置我們可以參考另一個的PasswordEncoder,<豆ID = 「DaoAuthenticationProvider還可以爲」 類= 「org.springframework.security.providers.dao.DaoAuthenticationProvider」> <屬性名= 「的UserDetailsS​​ervice」 REF = 「inMemoryDaoImpl」/> <屬性名= 「saltSource」 REF豆= 「saltSource」/> <屬性名= 「的PasswordEncoder」 REF = 「的PasswordEncoder」/> – 2014-09-24 11:38:10