All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.config.OAuth2ServerConfig Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package com.formkiq.server.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

import com.formkiq.server.service.OAuthUserDetailsService;

/**
 * OAuth2ServerConfig.
 *
 */
@Configuration
@EnableAuthorizationServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {

    /** AuthenticationManager. */
    @Autowired
    private AuthenticationManager authenticationManager;

    /** DataSource. */
    @Autowired
    private DataSource dataSource;

    /** OAuthUserDetailsService. */
    @Autowired
    private OAuthUserDetailsService oauthUserDetailsService;

    @Override
    public void configure(final AuthorizationServerSecurityConfigurer security)
            throws Exception {
        security.passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(
            final AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {

        JdbcTokenStore tokenStore = new JdbcTokenStore(this.dataSource);
        endpoints.tokenStore(tokenStore);

        endpoints.authenticationManager(this.authenticationManager);
        endpoints.userDetailsService(this.oauthUserDetailsService);
    }

    @Override
    public void configure(final ClientDetailsServiceConfigurer client)
            throws Exception {
        client.withClientDetails(jdbcClientDetailsService());
//        client.withClientDetails(this.clientDetailsService);
//        clients.inMemory()
//                .withClient("acme")
//                .secret("acmesecret")
//                .authorizedGrantTypes("authorization_code", "refresh_token",
//                        "implicit", "password", "client_credentials")
//                .scopes("webshop").and()
//                .withClient("app")
//                .secret("secret")
//                .authorizedGrantTypes("authorization_code", "refresh_token",
//                        "implicit", "password", "client_credentials")
//                .scopes("webshop");
    }

    /**
     * @return JdbcClientDetailsService
     */
    @Bean
    public JdbcClientDetailsService jdbcClientDetailsService() {

        JdbcClientDetailsService service = new JdbcClientDetailsService(
                this.dataSource);

        service.setPasswordEncoder(passwordEncoder());
        return service;
    }

    /**
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy