title: How do you save users who have logged in with OAuth 2 (Spring)?
body: <p>My main objective is to store the client-id of the each user, once they login with google. This <a href="https://github.com/OKaluzny/spring-boot-security-oauth2-google" rel="noreferrer">github repo</a> contains most of what I needed till now. The two main files of concern are <a href="https://github.com/OKaluzny/spring-boot-security-oauth2-google/blob/master/src/main/java/com/kaluzny/oauth2/config/OAuthSecurityConfig.java" rel="noreferrer">OAuthSecurityConfig.java</a> and <a href="https://github.com/OKaluzny/spring-boot-security-oauth2-google/blob/master/src/main/java/com/kaluzny/oauth2/web/UserRestController.java" rel="noreferrer">UserRestController.java</a>.</p>
  
  <p>When I navigate to <code>/user</code>, the Principal contains all the details I need on the user. Thus I can use the following snippets to get the data I need:</p>
  
  <pre><code>Authentication a = SecurityContextHolder.getContext().getAuthentication();
  String clientId = ((OAuth2Authentication) a).getOAuth2Request().getClientId();
  </code></pre>
  
  <p>I can then store the clientId in a repo</p>
  
  <pre><code>User user = new User(clientId);
  userRepository.save(user);
  </code></pre>
  
  <p>The problem with this is that users do not have to navigate to <code>/user</code>. Thus, one can navigate to  <code>/score/user1</code> without being registered. </p>
  
  <p>This API is meant to be a backend for an android application in the future, so a jquery redirect to <code>/user</code> would be insecure and would not work.</p>
  
  <hr>
  
  <p><strong>Things I have tried:</strong></p>
  
  <p><strong>Attempt 1</strong></p>
  
  <p>I created the following class:</p>
  
  <pre><code>@Service
  public class CustomUserDetailsService implements UserDetailsService {
  
  private final UserRepository userRepository;
  
  @Autowired
  public CustomUserDetailsService(UserRepository userRepository) {
      this.userRepository = userRepository;
  }
  
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      User user = userRepository.findByUsername(username);
      if (user == null) {
          throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
      }
      return new UserRepositoryUserDetails(user);
  }
  }
  </code></pre>
  
  <p>and overrode the <code>WebSecurityConfigurerAdapter</code>with:</p>
  
  <pre><code>@Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(customUserDetailsService);
  }
  </code></pre>
  
  <p>Both overridden methods are not called when a user logs in (I checked with a <code>System.out.println</code>)</p>
  
  <hr>
  
  <p><strong>Attempt 2</strong></p>
  
  <p>I tried adding <code>.userDetailsService(customUserDetailsService)</code></p>
  
  <p>to:</p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity http) throws Exception {
  
      http
              // Starts authorizing configurations.
              .authorizeRequests()
              // Do not require auth for the "/" and "/index.html" URLs
              .antMatchers("/", "/**.html", "/**.js").permitAll()
              // Authenticate all remaining URLs.
              .anyRequest().fullyAuthenticated()
              .and()
              .userDetailsService(customUserDetailsService)
              // Setting the logout URL "/logout" - default logout URL.
              .logout()
              // After successful logout the application will redirect to "/" path.
              .logoutSuccessUrl("/")
              .permitAll()
              .and()
              // Setting the filter for the URL "/google/login".
              .addFilterAt(filter(), BasicAuthenticationFilter.class)
              .csrf()
              .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
  }
  </code></pre>
  
  <p>Both methods were still not called, and I don't feel like I am any closer to the solution. Any help will be greatly appreciated.</p>