// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/services/RestUserDetailsService.java
// @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);
//     }
// 
//     @Override
//     public void createUser(UserDetails userDetails) {
//         restTemplate.postForLocation("/", userDetails);
//     }
// 
//     @Override
//     public void updateUser(UserDetails userDetails) {
//         restTemplate.put("/{0}", userDetails, userDetails.getUsername());
//     }
// 
//     @Override
//     public void deleteUser(String username) {
//         restTemplate.delete("/{0}", username);
//     }
// 
//     @Override
//     public void changePassword(String username, String password) {
// 
//         User user = new User();
//         user.setUsername(username);
//         user.setPassword(password);
// 
//         HttpEntity<User> requestEntity = new HttpEntity<>(user);
// 
//         restTemplate.exchange("/{0}", HttpMethod.PATCH, requestEntity, Void.class, username);
//     }
// 
//     @Override
//     public boolean userExists(String username) {
// 
//         try {
//             ResponseEntity<User> responseEntity = restTemplate.getForEntity("/{0}", User.class, username);
//             return HttpStatus.OK.equals(responseEntity.getStatusCode());
//         } catch (HttpClientErrorException e) {
//             // On HTTP 404 NOT FOUND or any HTTP error, assume user does not exist
//             return false;
//         }
//     }
// }


import org.dontpanic.spanners.springmvc.services.RestUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
package org.dontpanic.spanners.springmvc.config;


/**
 * Spring security configuration
 * Created by stevie on 08/06/16.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String[] PERMIT_ALL_URLS = {"/", "/css/**", "/img/**", "/js/**", "/signup", "/version.txt"};

    @Autowired
// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/services/RestUserDetailsService.java
// @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);
//     }
// 
//     @Override
//     public void createUser(UserDetails userDetails) {
//         restTemplate.postForLocation("/", userDetails);
//     }
// 
//     @Override
//     public void updateUser(UserDetails userDetails) {
//         restTemplate.put("/{0}", userDetails, userDetails.getUsername());
//     }
// 
//     @Override
//     public void deleteUser(String username) {
//         restTemplate.delete("/{0}", username);
//     }
// 
//     @Override
//     public void changePassword(String username, String password) {
// 
//         User user = new User();
//         user.setUsername(username);
//         user.setPassword(password);
// 
//         HttpEntity<User> requestEntity = new HttpEntity<>(user);
// 
//         restTemplate.exchange("/{0}", HttpMethod.PATCH, requestEntity, Void.class, username);
//     }
// 
//     @Override
//     public boolean userExists(String username) {
// 
//         try {
//             ResponseEntity<User> responseEntity = restTemplate.getForEntity("/{0}", User.class, username);
//             return HttpStatus.OK.equals(responseEntity.getStatusCode());
//         } catch (HttpClientErrorException e) {
//             // On HTTP 404 NOT FOUND or any HTTP error, assume user does not exist
//             return false;
//         }
//     }
// }


// Path: spanners-mvc/src/main/java/org/dontpanic/spanners/springmvc/config/WebSecurityConfig.java
import org.dontpanic.spanners.springmvc.services.RestUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

package org.dontpanic.spanners.springmvc.config;


/**
 * Spring security configuration
 * Created by stevie on 08/06/16.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String[] PERMIT_ALL_URLS = {"/", "/css/**", "/img/**", "/js/**", "/signup", "/version.txt"};

    @Autowired
    private RestUserDetailsService userDetailsService;