How to build an OAuth2 authentication server using Spring Boot and LDAP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm developing a Google Home Automation Skill.
Part of the task is to set up an OAuth2 server.



Considering that we already have an LDAP server set up, I assume this is relatively easy.



I went for the Spring Boot framework.



application.java



@SpringBootApplication
@RestController
@EnableOAuth2Client
@EnableAuthorizationServer
@Order(200)
public class Application {

private static final Log logger = LogFactory.getLog(Application.class);

public static void main(String args) {
SpringApplication.run(Application.class, args);
}

@RequestMapping("/user")
public Principal user(Principal user)
{
logger.info("/user has been called");
logger.debug("user info: " + user.toString());
return user;
}


LdapConfig.java



@Configuration
public class LdapConfig extends GlobalAuthenticationConfigurerAdapter {

@Value("${ldap.url}")
private String url;

@Value("${ldap.search_filter}")
private String searchFilter;

/*
@Value("${ldap.domain}")
private String domain;
*/

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchFilter(searchFilter)
.contextSource().url(url);
}

}


WebSecurityConfig.java



public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
OAuth2ClientContext oauth2ClientContext;

@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
.authenticated().and().exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
.logoutSuccessUrl("/").permitAll().and().csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());//.and()
//.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
// @formatter:on
}

}


I can curl my localhost and get a token if I comment out the LdapConfig




curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=client_credentials -d username=user -d password=password



{"access_token":"0710dd10-ae02-4e26-989af120b01bafa0","token_type":"bearer","expires_in":43199,"scope":"read write"}




But that obviously doesn't get my ldap involved.










share|improve this question





























    0















    I'm developing a Google Home Automation Skill.
    Part of the task is to set up an OAuth2 server.



    Considering that we already have an LDAP server set up, I assume this is relatively easy.



    I went for the Spring Boot framework.



    application.java



    @SpringBootApplication
    @RestController
    @EnableOAuth2Client
    @EnableAuthorizationServer
    @Order(200)
    public class Application {

    private static final Log logger = LogFactory.getLog(Application.class);

    public static void main(String args) {
    SpringApplication.run(Application.class, args);
    }

    @RequestMapping("/user")
    public Principal user(Principal user)
    {
    logger.info("/user has been called");
    logger.debug("user info: " + user.toString());
    return user;
    }


    LdapConfig.java



    @Configuration
    public class LdapConfig extends GlobalAuthenticationConfigurerAdapter {

    @Value("${ldap.url}")
    private String url;

    @Value("${ldap.search_filter}")
    private String searchFilter;

    /*
    @Value("${ldap.domain}")
    private String domain;
    */

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
    .userSearchFilter(searchFilter)
    .contextSource().url(url);
    }

    }


    WebSecurityConfig.java



    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    OAuth2ClientContext oauth2ClientContext;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
    .authenticated().and().exceptionHandling()
    .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
    .logoutSuccessUrl("/").permitAll().and().csrf()
    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());//.and()
    //.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
    // @formatter:on
    }

    }


    I can curl my localhost and get a token if I comment out the LdapConfig




    curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=client_credentials -d username=user -d password=password



    {"access_token":"0710dd10-ae02-4e26-989af120b01bafa0","token_type":"bearer","expires_in":43199,"scope":"read write"}




    But that obviously doesn't get my ldap involved.










    share|improve this question

























      0












      0








      0








      I'm developing a Google Home Automation Skill.
      Part of the task is to set up an OAuth2 server.



      Considering that we already have an LDAP server set up, I assume this is relatively easy.



      I went for the Spring Boot framework.



      application.java



      @SpringBootApplication
      @RestController
      @EnableOAuth2Client
      @EnableAuthorizationServer
      @Order(200)
      public class Application {

      private static final Log logger = LogFactory.getLog(Application.class);

      public static void main(String args) {
      SpringApplication.run(Application.class, args);
      }

      @RequestMapping("/user")
      public Principal user(Principal user)
      {
      logger.info("/user has been called");
      logger.debug("user info: " + user.toString());
      return user;
      }


      LdapConfig.java



      @Configuration
      public class LdapConfig extends GlobalAuthenticationConfigurerAdapter {

      @Value("${ldap.url}")
      private String url;

      @Value("${ldap.search_filter}")
      private String searchFilter;

      /*
      @Value("${ldap.domain}")
      private String domain;
      */

      @Override
      public void init(AuthenticationManagerBuilder auth) throws Exception {
      auth.ldapAuthentication()
      .userSearchFilter(searchFilter)
      .contextSource().url(url);
      }

      }


      WebSecurityConfig.java



      public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

      @Autowired
      OAuth2ClientContext oauth2ClientContext;

      @Override
      protected void configure(HttpSecurity http) throws Exception {
      // @formatter:off
      http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
      .authenticated().and().exceptionHandling()
      .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
      .logoutSuccessUrl("/").permitAll().and().csrf()
      .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());//.and()
      //.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
      // @formatter:on
      }

      }


      I can curl my localhost and get a token if I comment out the LdapConfig




      curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=client_credentials -d username=user -d password=password



      {"access_token":"0710dd10-ae02-4e26-989af120b01bafa0","token_type":"bearer","expires_in":43199,"scope":"read write"}




      But that obviously doesn't get my ldap involved.










      share|improve this question














      I'm developing a Google Home Automation Skill.
      Part of the task is to set up an OAuth2 server.



      Considering that we already have an LDAP server set up, I assume this is relatively easy.



      I went for the Spring Boot framework.



      application.java



      @SpringBootApplication
      @RestController
      @EnableOAuth2Client
      @EnableAuthorizationServer
      @Order(200)
      public class Application {

      private static final Log logger = LogFactory.getLog(Application.class);

      public static void main(String args) {
      SpringApplication.run(Application.class, args);
      }

      @RequestMapping("/user")
      public Principal user(Principal user)
      {
      logger.info("/user has been called");
      logger.debug("user info: " + user.toString());
      return user;
      }


      LdapConfig.java



      @Configuration
      public class LdapConfig extends GlobalAuthenticationConfigurerAdapter {

      @Value("${ldap.url}")
      private String url;

      @Value("${ldap.search_filter}")
      private String searchFilter;

      /*
      @Value("${ldap.domain}")
      private String domain;
      */

      @Override
      public void init(AuthenticationManagerBuilder auth) throws Exception {
      auth.ldapAuthentication()
      .userSearchFilter(searchFilter)
      .contextSource().url(url);
      }

      }


      WebSecurityConfig.java



      public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

      @Autowired
      OAuth2ClientContext oauth2ClientContext;

      @Override
      protected void configure(HttpSecurity http) throws Exception {
      // @formatter:off
      http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest()
      .authenticated().and().exceptionHandling()
      .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
      .logoutSuccessUrl("/").permitAll().and().csrf()
      .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());//.and()
      //.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
      // @formatter:on
      }

      }


      I can curl my localhost and get a token if I comment out the LdapConfig




      curl acme:acmesecret@localhost:8080/oauth/token -d grant_type=client_credentials -d username=user -d password=password



      {"access_token":"0710dd10-ae02-4e26-989af120b01bafa0","token_type":"bearer","expires_in":43199,"scope":"read write"}




      But that obviously doesn't get my ldap involved.







      spring-boot spring-security ldap spring-oauth2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 14:13









      Joris SchellekensJoris Schellekens

      6,25511443




      6,25511443
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448267%2fhow-to-build-an-oauth2-authentication-server-using-spring-boot-and-ldap%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448267%2fhow-to-build-an-oauth2-authentication-server-using-spring-boot-and-ldap%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini