3

如何將以下自定義UserDetailsService添加到this Spring OAuth2 sample將自定義UserDetailsS​​ervice添加到Spring Security OAuth2應用程序

authserver應用程序的application.properties文件中定義了默認user,默認password

不過,我想下面的自定義UserDetailsService添加到the demo package of the authserver app用於測試目的:

package demo; 

import java.util.List; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.AuthorityUtils; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 
import org.springframework.security.provisioning.UserDetailsManager; 
import org.springframework.stereotype.Service; 

@Service 
class Users implements UserDetailsManager { 

    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
     String password; 
     List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"); 
     if (username.equals("Samwise")) { 
      auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT"); 
      password = "TheShire"; 
     } 
     else if (username.equals("Frodo")){ 
      auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT"); 
      password = "MyRing"; 
     } 
     else{throw new UsernameNotFoundException("Username was not found. ");} 
     return new org.springframework.security.core.userdetails.User(username, password, auth); 
    } 

    @Override 
    public void createUser(UserDetails user) {// TODO Auto-generated method stub 
    } 

    @Override 
    public void updateUser(UserDetails user) {// TODO Auto-generated method stub 
    } 

    @Override 
    public void deleteUser(String username) {// TODO Auto-generated method stub 
    } 

    @Override 
    public void changePassword(String oldPassword, String newPassword) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public boolean userExists(String username) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

正如你可以看到,這UserDetailsService不是autowired着呢,它故意使用不安全的密碼,因爲它是唯一設計用於測試目的。

需要什麼樣的具體變化the GitHub sample app被製成,以便用戶可以爲username=Samwisepassword=TheShire登錄,或作爲​​與password=MyRing是否需要對AuthserverApplication.java或其他地方進行更改?


幾點建議:


Spring OAuth2 Developer Guide說,使用一個GlobalAuthenticationManagerConfigurer全局配置一個UserDetailsService。但是,使用這些名稱搜索結果並不會有幫助。

而且,不同的應用程序,使用OAuth INSTEAD OF內部彈簧安全使用以下語法掛鉤的UserDetailsService,但我不知道如何它的語法調整到目前的OP:

@Order(Ordered.HIGHEST_PRECEDENCE) 
@Configuration 
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter { 

    @Autowired 
    private Users users; 

    @Override 
    public void init(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(users); 
    } 
} 

我使用@AutowireOAuth2AuthorizationConfig裏面Users連接到AuthorizationServerEndpointsConfigurer如下嘗試:

@Autowired//THIS IS A TEST 
private Users users;//THIS IS A TEST 

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints.authenticationManager(authenticationManager) 
     .accessTokenConverter(jwtAccessTokenConverter()) 
     .userDetailsService(users)//DetailsService)//THIS LINE IS A TEST 
     ; 
} 

但是春天啓動日誌表明用戶Samwise WA沒有找到,這意味着UserDetailsService未成功連接。下面是從春天啓動日誌相關的摘錄:

2016-04-20 15:34:39.998 DEBUG 5535 --- [nio-9999-exec-9] o.s.s.a.dao.DaoAuthenticationProvider : 
     User 'Samwise' not found 
2016-04-20 15:34:39.998 DEBUG 5535 --- [nio-9999-exec-9] 
     w.a.UsernamePasswordAuthenticationFilter : 
     Authentication request failed: 
     org.springframework.security.authentication.BadCredentialsException: 
     Bad credentials 

我還能嘗試一下呢?

回答

2

我在用Spring Security開發我的oauth服務器時遇到了類似的問題。我的情況稍有不同,因爲我想添加UserDetailsService來驗證刷新令牌,但我認爲我的解決方案也會對您有所幫助。

和你一樣,我第一次嘗試使用AuthorizationServerEndpointsConfigurer指定UserDetailsService,但這不起作用。我不確定這是一個錯誤還是設計,但UserDetailsService需要設置在AuthenticationManager爲了各種oauth2類找到它。這爲我工作:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    Users userDetailsService; 

    @Autowired 
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
    // other stuff to configure your security 
    } 

} 

我認爲,如果你改變後,開始在73行,它可能爲你工作:

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.parentAuthenticationManager(authenticationManager) 
     .userDetailsService(userDetailsService); 
} 

你也當然會需要在WebSecurityConfigurerAdapter

添加 @Autowired Users userDetailsService;某處

其他的事情我想提一提:

  1. 這可能是特定版本,我m on spring-security-oauth2 2.0.12
  2. 我不能引用任何消息來源,爲什麼這是它的方式,我甚至不確定我的解決方案是真正的解決方案還是黑客。
  3. 指南中提到的GlobalAuthenticationManagerConfigurer幾乎肯定是一個錯字,在源代碼中的任何位置找不到該字符串中的任何內容。
相關問題