// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/domain/User.java
// public class User implements UserDetails {
// 
//     private String username;
//     private String password;
//     private Boolean enabled;
// 
//     public User() {
//     }
// 
//     public User(String username, String password, Boolean enabled) {
//         this.username = username;
//         this.password = password;
//         this.enabled = enabled;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public Boolean getEnabled() {
//         return enabled;
//     }
// 
//     public void setEnabled(Boolean enabled) {
//         this.enabled = enabled;
//     }
// 
//     @Override
//     public boolean isAccountNonExpired() {
//         return true;
//     }
// 
//     @Override
//     public boolean isAccountNonLocked() {
//         return true;
//     }
// 
//     @Override
//     public boolean isCredentialsNonExpired() {
//         return true;
//     }
// 
//     @Override
//     public boolean isEnabled() {
//         return enabled;
//     }
// 
//     @Override
//     public Collection<? extends GrantedAuthority> getAuthorities() {
//         return Collections.emptySet();
//     }
// }


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dontpanic.spanners.springmvc.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
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;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
package org.dontpanic.spanners.springmvc.services;


/**
 * Retrieve user details from a REST service
 * Created by stevie on 14/07/16.
 */
@Service
public class RestUserDetailsService implements UserDetailsManager {

    private RestTemplate restTemplate;

    public RestUserDetailsService(RestTemplateBuilder builder,
                                  @Value("${app.service.url.users}") String rootUri) {

        restTemplate = builder.messageConverters(halAwareMessageConverter())
                                .rootUri(rootUri).build();
    }

    private HttpMessageConverter halAwareMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.registerModule(new Jackson2HalModule());

        return new MappingJackson2HttpMessageConverter(mapper);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/domain/User.java
// public class User implements UserDetails {
// 
//     private String username;
//     private String password;
//     private Boolean enabled;
// 
//     public User() {
//     }
// 
//     public User(String username, String password, Boolean enabled) {
//         this.username = username;
//         this.password = password;
//         this.enabled = enabled;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public Boolean getEnabled() {
//         return enabled;
//     }
// 
//     public void setEnabled(Boolean enabled) {
//         this.enabled = enabled;
//     }
// 
//     @Override
//     public boolean isAccountNonExpired() {
//         return true;
//     }
// 
//     @Override
//     public boolean isAccountNonLocked() {
//         return true;
//     }
// 
//     @Override
//     public boolean isCredentialsNonExpired() {
//         return true;
//     }
// 
//     @Override
//     public boolean isEnabled() {
//         return enabled;
//     }
// 
//     @Override
//     public Collection<? extends GrantedAuthority> getAuthorities() {
//         return Collections.emptySet();
//     }
// }


// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/services/RestUserDetailsService.java
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dontpanic.spanners.springmvc.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
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;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

package org.dontpanic.spanners.springmvc.services;


/**
 * Retrieve user details from a REST service
 * Created by stevie on 14/07/16.
 */
@Service
public class RestUserDetailsService implements UserDetailsManager {

    private RestTemplate restTemplate;

    public RestUserDetailsService(RestTemplateBuilder builder,
                                  @Value("${app.service.url.users}") String rootUri) {

        restTemplate = builder.messageConverters(halAwareMessageConverter())
                                .rootUri(rootUri).build();
    }

    private HttpMessageConverter halAwareMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.registerModule(new Jackson2HalModule());

        return new MappingJackson2HttpMessageConverter(mapper);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return restTemplate.getForObject("/{0}", User.class, username);