Pages

Subscribe:

Ads 468x60px

Featured Posts

Tuesday, February 7, 2017

Functions as First Class Citizen Variables

Hello all, In this post we are going to talk about functions as first class citizens and it's usages.
taken from - https://www.linkedin.com/topic/functional-programming
The easiest way to understand is to analyze a demonstration. Package java.util.function  in java 8 contains all kinds of single method interfaces. In this samples we are going to use the java.util.function.Function and java.util.function.BiFunction interfaces.

1. Create  an inline lambda expression and store in a variable.
BiFunction<Integer, Integer, Integer> addFunction = (int1, int2) -> {
            return int1 + int2;
        };

to call the above method we can do as follows.

addFunction.apply(100, 200);

2. Create a static method that fits into the signature of the BiFunction interface, then assign it to the variable and invoke.

import java.util.function.BiFunction;

/**
 * Created by aruna on 2/7/17.
 */
public class Main {

    public static void main(String[] args) throws Exception {
        //create the variable
        BiFunction<Integer, Integer, Integer> addFunctionVariable;
        //assign the function to variable
        addFunctionVariable = Main::addStaticFunction;
        //invoke
        addFunctionVariable.apply(100, 200);
    }

    private static int addStaticFunction(int int1, int int2) {
        return int1 + int2;
    }
}

3. Create a instance method that fits to the signature of the BiFunction interface. In this approach, first we have to create the instance to acquire the function variable.

import java.util.function.BiFunction;

/**
 * Created by aruna on 2/7/17.
 */
public class Main {

    public static void main(String[] args) throws Exception {
        //create the variable
        BiFunction<Integer, Integer, Integer> addFunctionVariable;
        //create Main Object
        Main main = new Main();
        //assign the instance method to variable
        addFunctionVariable = main::addFunction;
        //invoke
        addFunctionVariable.apply(100, 200);
    }

    private int addFunction(int int1, int int2) {
        return int1 + int2;
    }
}

Now take an example, how to pass functions as variables and do some stuff.

import java.util.function.Function;

/**
 * Created by aruna on 2/7/17.
 */
public class Main {

    public static void main(String[] args) throws Exception {

        //define a function that multiply integer by 100
        Function<Integer, Integer> function = (intValue) -> { return intValue * 100;};
        //pass that function as a variable
        multiplyAndAdd(100 , 200, function);
    }
    private static int multiplyAndAdd(int int1, int int2, Function<Integer, Integer> function){

        if(function != null) {
            int1 = function.apply(int1);
            int2 = function.apply(int2);
        }

        return int1+ int2;
    }
}

Saturday, November 5, 2016

Spring Boot Application connect to LDAP Userstore

In this blog post we are going to connect a sample spring boot application with LDAP based userstore to do the authentication.
First create a LDAP server. I've created a sample server using Apache Directory Studio.
Then create a sample spring-boot application with the following dependencies.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath/> 
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-server-jndi</artifactId>
            <version>1.5.5</version>
        </dependency>
    </dependencies>

Then in your sample application extend the WebSecurityConfigurerAdapter class and override the below two methods. Provide the connection details as per the ldap server created above.

@Configuration
@EnableWebSecurity
public class LdapSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf().disable();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.ldapAuthentication()
                .contextSource().url("ldap://localhost:10389/dc=example,dc=com")
                .managerDn("uid=admin,ou=system").managerPassword("secret")
                .and()
                .userSearchBase("ou=users")
                .userSearchFilter("(cn={0})");
    }
}

That's it the spring will engage basic authenticate your requests to the webapp.
Now start the app using mvn spring-boot:run or using the java -jar spring-boot-ldap-sample.jar, Access the webapp using http://localhost:8080/ and provide the credentials of a user in the ldap user base. You'll see the authenticated user's details after a successful authentication.


Full source to the sample can be found here.