在NoSQL数据库中实现Spring oauth2客户端数据存储

时间:2016-12-20 19:15:14

标签: spring spring-security nosql

我有一个简单的Spring启动应用程序设置oauth 2.0,客户端详细信息正从mysql中获取。我正在寻找一种在NoSQL数据库中实现客户端数据存储的方法。我不确定如何实现这一点,我无法在网上找到任何资源?

1 个答案:

答案 0 :(得分:3)

您需要扩展 AuthorizationServerConfigurerAdapter 类,在那里配置使用自定义存储库的 UserDetailsS​​ervice

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private ApplicationConfigurationProperties configuration;

    private TokenStore tokenStore = new InMemoryTokenStore();

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints
                .tokenStore(this.tokenStore)
                .authenticationManager(this.authenticationManager)
                .userDetailsService(userDetailsService);
    }
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private UserRepository userRepository;


    @Override
    public UserDetails loadUserByUsername(String username) {
        // get user and create UserDetails object from that
    }

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}
import org.springframework.data.annotation.Id;


public class User {

    public String userName;
    public String password;

    public User() {}

    // getters and setters
}
import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserRepository extends MongoRepository<User, String> {

    public Customer findByUserName(String firstName);
}