Spring Boot

Qu'est-ce qu'un framework ?

Préambule

$ git clone https://github.com/lolo101/formation-spring-boot-framework

Spring Boot

Injection

Introspection

Proxy

Spring Boot

> Java 8 <

Control - Enter

Unzip

cd

mvn package

java -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot

Pas très intéressant...

Jetons le starter web dans le POM pour voir

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot

Exposons un web service

sous la forme d'un controller REST

@RestController
@RequestMapping("books")
public class Books {
    @GetMapping
    public String books() {
        return "a list of books";
    }
}
$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

http://localhost:8080/books

Spring Boot

Ajoutons un peu de sécurité

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot

Propriétés

- application.properties
- application.yml

Exemple : LDAP embarqué (UnboundID)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
	<groupId>com.unboundid</groupId>
	<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-ldap</artifactId>
</dependency>

Spring Boot

Exemple : LDAP embarqué (UnboundID)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
	<groupId>com.unboundid</groupId>
	<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-ldap</artifactId>
</dependency>
spring.ldap.embedded.base-dn=dc=example,dc=com
spring.ldap.embedded.port=8389

application.properties

Spring Boot

spring.ldap.embedded.base-dn=dc=example,dc=com
spring.ldap.embedded.port=8389

application.properties

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
                .userSearchFilter("(uid={0})")
                .contextSource()
                .url("ldap://localhost:8389/dc=example,dc=com");
    }
}

Configuration de l'AuthenticationManager

Spring Boot

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
                .userSearchFilter("(uid={0})")
                .contextSource()
                .url("ldap://localhost:8389/dc=example,dc=com");
    }
}

Configuration de l'AuthenticationManager

dn: dc=example,dc=com
dc: example
description: Serveur exemple
objectClass: dcObject
objectClass: organization
o: Serveur exemple

dn: ou=people, dc=example,dc=com
ou: people
objectClass: organizationalUnit

dn: uid=user,ou=people,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: user
cn: Lucky
sn: Luke
userPassword: dwssap
mail: user@example.com

schema.ldif (https://gist.github.com/lolo101/)

Spring Boot

dn: dc=example,dc=com
dc: example
description: Serveur exemple
objectClass: dcObject
objectClass: organization
o: Serveur exemple

dn: ou=people, dc=example,dc=com
ou: people
objectClass: organizationalUnit

dn: uid=user,ou=people,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: user
cn: Lucky
sn: Luke
userPassword: dwssap
mail: user@example.com
$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

schema.ldif (https://gist.github.com/lolo101/)

Spring Boot

Stateless service

$ git clone https://github.com/lolo101/formation-spring-boot-demo.git

Servlet Filter

https://jwt.io/

https://www.postman.com/

Spring Boot

Interceptors

Spring Boot

Exception handling

Spring Boot

Actuator

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
$ mvn package
$ java -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot

Monitoring

https://glowroot.org/

https://visualvm.github.io/

Spring Boot

µ services

$ git clone https://github.com/lolo101/formation-spring-boot-cloud.git

$ mvn package
$ java -jar say-hello/target/...jar
$ java -jar user/target/...jar

F5 (x3)

Spring Boot

µ services

ServicesRegistry

$ git checkout eureka
$ mvn clean package
$ java -jar discovery/target/...jar
$ java -jar say-hello/target/...jar
$ java -jar user/target/...jar

http://localhost:8761

Spring Boot

µ services

API Gateway

$ git checkout api-gateway
$ mvn package
$ java -jar discovery/target/...jar
$ java -jar say-hello/target/...jar
$ java -jar user/target/...jar
$ java -jar gateway/target/...jar

http://localhost:8080

Spring Boot

µ services

Configuration centralisée

Spring Boot

By Loïc Broquet

Spring Boot

  • 154