139
‹#› © 2016 Pivotal Software, Inc. All rights reserved. Spring Boot Tips Toshiaki Maki (@making) [email protected] Spring 2017 Feb 2017-02-27

実例で学ぶ、明日から使えるSpring Boot Tips #jsug

Embed Size (px)

Citation preview

Page 1: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved. ‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot Tips

Toshiaki Maki (@making) [email protected] Spring 2017 Feb 2017-02-27

Page 2: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Who am I ?• Toshiaki Maki (@making) https://ik.am

•Sr. Solutions Architect @Pivotal

•Spring ☘ / Cloud Foundry ☁ / Concourse ✈ / BOSH 🐚

bit.ly/hajiboot2

Page 3: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot is ....•Opinionated Framework on Spring Ecosystem

Page 4: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot is ....•Opinionated Framework on Spring Ecosystem

Spring Boot Way

Page 5: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

https://goo.gl/Ey1y3X

https://github.com/openenquete/enquete

Page 6: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.jar

Thymeleaf

Spr

ing

Dat

a JP

A

Spring Data REST

Spring MVC

MySQL

Spr

ing

Sec

urity

O

Aut

h

Cloud Foundry

Page 7: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.jar

Thymeleaf

Spr

ing

Dat

a JP

A

Spring Data REST

Spring MVC

MySQL

Spr

ing

Sec

urity

O

Aut

h

Cloud Foundry

Page 8: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.jar

Thymeleaf

Spr

ing

Dat

a JP

A

Spring Data REST

Spring MVC

MySQL

Spr

ing

Sec

urity

O

Aut

h

Cloud Foundry

AJAX / REST

Page 9: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.jar

Thymeleaf

Spr

ing

Dat

a JP

A

Spring Data REST

Spring MVC

MySQL

Spr

ing

Sec

urity

O

Aut

h

Cloud Foundry

AJAX / REST

GitHub

Page 10: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.jar

Thymeleaf

Spr

ing

Dat

a JP

A

Spring Data REST

Spring MVC

MySQL

Spr

ing

Sec

urity

O

Aut

h

Cloud Foundry

AJAX / REST

GitHub

Page 11: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 12: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 13: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error

Page 14: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error

Page 15: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error

Page 16: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error

Page 17: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Errororg.springframework.boot.autoconfigure.web.DefaultErrorViewResolver

src/main/resources /static/error/403.html 404.html 40x.html 500.html 50x.html

Page 18: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Errororg.springframework.boot.autoconfigure.web.DefaultErrorViewResolver

src/main/resources /templates/error/403.<ext> 404.<ext> 40x.<ext> 500.<ext> 50x.<ext>

Page 19: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="${@environment.acceptsProfiles('default')}"> <tr> <th>Status</th><td th:text="${status}"></td> </tr> <tr> <th>Error</th><td th:text="${error}"></td> </tr> <tr> <th>Exception</th><td th:text="${exception}"></td> </tr> <tr> <th>Message</th><td th:text="${message}"></td> </tr> </table>

Page 20: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="${@environment.acceptsProfiles('default')}"> <tr> <th>Status</th><td th:text="${status}"></td> </tr> <tr> <th>Error</th><td th:text="${error}"></td> </tr> <tr> <th>Exception</th><td th:text="${exception}"></td> </tr> <tr> <th>Message</th><td th:text="${message}"></td> </tr> </table>

profile default

disabled

Page 21: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="${@environment.acceptsProfiles('default')}"> <tr> <th>Status</th><td th:text="${status}"></td> </tr> <tr> <th>Error</th><td th:text="${error}"></td> </tr> <tr> <th>Exception</th><td th:text="${exception}"></td> </tr> <tr> <th>Message</th><td th:text="${message}"></td> </tr> </table>

profile default

disabled

Page 22: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="..."> <!-- --> <tr th:if="${trace}"> <th>Trace</th> <td><pre th:text="${trace}"></pre></td> </tr>

Page 23: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="..."> <!-- --> <tr th:if="${trace}"> <th>Trace</th> <td><pre th:text="${trace}"></pre></td> </tr>

server.error.include-stacktrace=alwaysapplication-default.properties

Page 24: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

<table th:if="..."> <!-- --> <tr th:if="${trace}"> <th>Trace</th> <td><pre th:text="${trace}"></pre></td> </tr>

server.error.include-stacktrace=alwaysapplication-default.properties

Page 25: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@ControllerAdvice(annotations = Controller.class) public class ErrorControllerAdvice { @ExceptionHandler(NoSuchElementException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public String noSuchEelemtException() { return "error/404"; } }

Page 26: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@ControllerAdvice(annotations = Controller.class) public class ErrorControllerAdvice { @ExceptionHandler(NoSuchElementException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public String noSuchEelemtException() { return "error/404"; } }

Page 27: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@Autowired Environment env; @ExceptionHandler(NoSuchElementException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public String ex(NoSuchElementException e, Model model) { addErrors(e, HttpStatus.NOT_FOUND, model); return "error/404"; }

Page 28: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

void addErrors(Exception e, HttpStatus status, Model model) { if (env.acceptsProfiles("default")) { StringWriter stackTrace = new StringWriter(); e.printStackTrace(new PrintWriter(stackTrace)); stackTrace.flush(); model.addAttribute("status", status.value()); model.addAttribute("error", status.getReasonPhrase()); model.addAttribute("exception", e.getClass()); model.addAttribute("message", e.getMessage()); model.addAttribute("trace", stackTrace.toString()); } }

Page 29: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

void addErrors(Exception e, HttpStatus status, Model model) { if (env.acceptsProfiles("default")) { StringWriter stackTrace = new StringWriter(); e.printStackTrace(new PrintWriter(stackTrace)); stackTrace.flush(); model.addAttribute("status", status.value()); model.addAttribute("error", status.getReasonPhrase()); model.addAttribute("exception", e.getClass()); model.addAttribute("message", e.getMessage()); model.addAttribute("trace", stackTrace.toString()); } }

Page 30: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Profile

• java -jar app.jar --spring.profiles.active=prod• java -jar -Dspring.profiles.active=prod app.jar• export SPRING_PRPFILES_ACTIVE=prod

Cloud Foundry

cloud

Page 31: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 32: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 33: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Data REST REST API

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>

Page 34: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public interface FooRepository extends CrudRepository<Foo, Long> {}

Repository --> REST API

Page 35: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public interface FooRepository extends CrudRepository<Foo, Long> {}

Repository --> REST API

GET /foosPOST /foosGET /foos/{fooId}PUT /foos/{fooId}DELETE /foos/{fooId}PATCH /foos/{fooId}...

Page 36: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends PagingAndSortingRepository<Foo, Long> {}

Page 37: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends PagingAndSortingRepository<Foo, Long> {}

GET /foos?page={page} &size={size}&sort={sort}...

Page 38: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends PagingAndSortingRepository<Foo, Long> { List<Foo> findByName(String name); }

Page 39: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends PagingAndSortingRepository<Foo, Long> { List<Foo> findByName(String name); }

GET /foos/search/findByName?name={name}...

Page 40: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends Repository<Foo, Long> { @RestResource(exported = false) List<Foo> findBySeminarId(Long seminarId); Optional<Foo> findOne(Long id); void save(Foo foo); }

Page 41: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Repository --> REST API

public interface FooRepository extends Repository<Foo, Long> { @RestResource(exported = false) List<Foo> findBySeminarId(Long seminarId); Optional<Foo> findOne(Long id); void save(Foo foo); } GET /foos/{id}

POST /foos

Page 42: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RepositoryEventHandlerTraditional

Spring Data REST

Controller Service Repository

Repository EventHandler

Page 43: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RepositoryEventHandler@RepositoryEventHandler @Componentpublic class UsernameEventHandler { @HandleReforeCreate void setUsername(UsernameHolder holder) { String username = SecurityContextHolder .getContext().getAuthentication() .getName(); holder.setUsername(username); } }

Page 44: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

RepositoryEventHandler@RepositoryEventHandler @Componentpublic class UsernameEventHandler { @HandleReforeCreate void setUsername(UsernameHolder holder) { String username = SecurityContextHolder .getContext().getAuthentication() .getName(); holder.setUsername(username); } }

UserHolderusername

Page 45: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Bean Validation@Configuration public class RestConfig extends RepositoryRestConfigurerAdapter { private final Validator vldtr; /* constructor */ @Override public void configureValidatingRepositoryEventListener(...) { lstnr.addValidator("beforeCreate", vldtr); lstnr.addValidator("beforeSave", vldtr); }}

Page 46: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Bean Validation@Configuration public class RestConfig extends RepositoryRestConfigurerAdapter { private final Validator vldtr; /* constructor */ @Override public void configureValidatingRepositoryEventListener(...) { lstnr.addValidator("beforeCreate", vldtr); lstnr.addValidator("beforeSave", vldtr); }}

{ "errors": [{ "entity": "ResponseForSession", "property": "difficulty", "invalidValue": null, "message": "may not be null" }, { "entity": "ResponseForSession", "property": "satisfaction", "invalidValue": null, "message": "may not be null" } ]}

Page 47: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Traditional

Spring Data REST

Controller Service Repository

Repository EventHandler

@Transactional

Page 48: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Traditional

Spring Data REST

Controller Service Repository

Repository EventHandler

@Transactional

RepositoryEntity Controller

Page 49: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Traditional

Spring Data REST

Controller Service Repository

Repository EventHandler

@Transactional

RepositoryEntity Controller

Page 50: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Traditional

Spring Data REST

Controller Service Repository

Repository EventHandler

@Transactional

RepositoryEntity Controller

AOP

Page 51: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

• https://blog.ik.am/entries/403

Page 52: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 53: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 54: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

CSRF•Spring Security CSRF

•Ajax / SPA

Page 55: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

CSRF•Spring Security CSRF

•Ajax / SPApublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable()/* ... */; }}

Page 56: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

CSRF•Spring Security CSRF

•Ajax / SPApublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable()/* ... */; }}

🙅

Page 57: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

$.post('/user', { firstName: 'Fred', lastName: 'Flintstone', _csrf: $('input[name=_csrf]').val() });

$(document).ajaxSend(function(e, xhr, options) { var token = $('input[name=_csrf]').val(); xhr.setRequestHeader('X-CSRF-TOKEN', token);});

OR

Page 58: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

I don't like jQuery😟

Page 59: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http./* ... * /and().csrf() .csrfTokenRepository( CookieCsrfTokenRepository .withHttpOnlyFalse()); } }

Page 60: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 61: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

HTTP (_csrf) HTTP

(X-CSRF-TOKEN) CSRF HTTP

( )

Page 62: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

HTTP (_csrf) HTTP

(X-CSRF-TOKEN) CSRF HTTP

( )

HTTP (_csrf) HTTP

(X-XSRF-TOKEN) CSRF

Cookie(name=XSRF-TOKEN, httpOnly)

Page 63: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Axios• https://github.com/mzabriskie/axios

• node.js Promise HTTP

• XSRF-TOKEN Cookie HTTP

X-XSRF-TOKEN (AngularJS )

Page 64: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Axios• https://github.com/mzabriskie/axios

• node.js Promise HTTP

• XSRF-TOKEN Cookie HTTP

X-XSRF-TOKEN (AngularJS )

axios.post('/response_for_seminar', { comment: 'Great!', request: 'Sushi!' }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });

Page 65: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 66: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 67: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 68: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 69: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 70: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 71: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 72: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

OAuth2 SSO with GitHub

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency>

Page 73: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

Page 74: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

Page 75: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

Page 76: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

code

Page 77: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

codecode

Page 78: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

codecode

token

Page 79: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

codecode

token

token

Page 80: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Authorization Code (grant_type=authorization_code)

Authorization Server (GitHub)

Web UI

Resource Server (GitHub API)

authorize

redirect

codecode

token

token

response

Page 81: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@EnableOAuth2Sso@SpringBootApplication@EnableOAuth2Sso public class EnqueteApplication { public static void main(String[] args) { SpringApplication.run(EnqueteApplication.class, args); }}

Page 82: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

security.oauth2.client.client-id=xxxxxsecurity.oauth2.client.client-secret=xxxxxsecurity.oauth2.client.access-token-uri=https://github.com/login/oauth/access_tokensecurity.oauth2.client.user-authorization-uri=https://github.com/login/oauth/authorize security.oauth2.resource.user-info-uri=https://api.github.com/user

Page 83: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 84: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 85: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

PrincipalExtractor / AuthoritiesExtractor

public interface PrincipalExtractor { Object extractPrincipal(Map<String, Object> map);}

public interface AuthoritiesExtractor { List<GrantedAuthority> extractAuthorities(Map<String, Object> map);}

Page 86: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public EnqUserPrincipalExtractor implements AuthoritiesExtractor { @Override Object extractPrincipal(Map<String, Object> map){ return new EnqUser(map.get("name"), map.get("email"), map.get("avatar_url")); }}

Page 87: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public EnqUserPrincipalExtractor implements AuthoritiesExtractor { @Override Object extractPrincipal(Map<String, Object> map){ return new EnqUser(map.get("name"), map.get("email"), map.get("avatar_url")); }}

security.oauth2.resource.user-info-uri

Page 88: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public EnqUserAuthoritiesExtractor implements PrincipalExtractor { @Override List<GrantedAuthority> extractAuthorities(Map<String, Object> map){ return Arrays.asList( !"making".equals(map.get("login")) ? new SimpleGrantAuthority("ROLE_USER") : new SimpleGrantAuthority("ROLE_ADMIN") ) }

Page 89: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

Page 90: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

Page 91: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

Page 92: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

code

Page 93: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

codecode

Page 94: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

codecode

token

Page 95: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

codecode

token

token

Page 96: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

codecode

token

token

response

Page 97: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

GitHub OK

Authorization Server

Web UI

Resource Server

authorize

redirect

codecode

token

token

response

@EnableAuthorizationServer

@EnableResourceServer

Page 98: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

UI SSO

Authorization Server

Web UI

Resource ServerAnother Web UI

Page 99: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

• https://github.com/Pivotal-Japan/spring-security-oauth-workshop

Page 100: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 101: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 102: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

public interface UrlShortenerClient { String shorten(String longUrl);}

Page 103: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 104: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@ConditionalOnProperty

Page 105: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@Component@ConditionalOnProperty(name = "enquete.bitly.access-token")public BitlyClient implements UrlShortenerClient { @Override String shorten(String longUrl) { String token = props.getBitly() .getAccessToken(); /* ... */ }}

Bit.ly URL

Page 106: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@Component@ConditionalOnProperty(name = "enquete.googl.api-key")public GooglClient implements UrlShortenerClient { @Override String shorten(String longUrl) { String apiKey = props.getGoogl().getApiKey(); /* ... */ }}

Goo.gl URL

Page 107: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.bitly.access-token=0123456789

enquete.googl.api-key=abcdef

BitlyClient

GooglClient

Page 108: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@Controllerpublic SeminarController { final Optional<UrlShortenerClient> shortener; @GetMapping("seminars/{seminarId}") public String list(...) { shortener.ifPresent(x -> { String shorten = x.shorten(url); model.addAttribute("shorten", shorten); }); /* ... */ }}

Page 109: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 110: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.bitly.access-token=0123456789

Page 111: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.bitly.access-token=0123456789

Page 112: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.bitly.access-token=0123456789enquete.googl.api-key=abcdef

Page 113: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

enquete.bitly.access-token=0123456789enquete.googl.api-key=abcdef

Page 114: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 115: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 116: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

@ConfigurationProperties@ConfigurationProperties(prefix = "enquete")@Component @Validatedpublic class EnqProps { private Set<String> adminUsers; private Bitly bitly; public static class Bitly {

private String accessToken; } // setter/getter}

Page 117: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Configuration Processor

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</scope>

</dependency>

Page 118: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 119: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 120: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 121: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 122: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 123: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot Actuator >= 1.5• (ROLE_ACTUATOR)

• /info, /health (status )

management.security.enabled=falseapplication-default.properties

Page 124: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Boot Actuator >= 1.5• (ROLE_ACTUATOR)

• /info, /health (status )

management.security.enabled=falseapplication-default.properties

profile default

disabled

Page 125: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

ROLE_ACTUATORpublic EnqUserAuthoritiesExtractor /* ... */ { @Override List<GrantedAuthority> extractAuthorities(Map<String, Object> map){ return !"making".equals(map.get("login")) ? asList(new SimpleGrantAuthority("ROLE_USER")) : asList(new SimpleGrantAuthority("ROLE_ADMIN"), new SimpleGrantAuthority("ROLE_ACTUATOR")) }}

Page 126: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

BeansViz<dependency> <groupId>am.ik.beansviz</groupId> <artifactId>beansviz-spring-boot-actuator</artifactId> <version>0.1.0</version></dependency>

Page 127: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

BeansViz<dependency> <groupId>am.ik.beansviz</groupId> <artifactId>beansviz-spring-boot-actuator</artifactId> <version>0.1.0</version></dependency>

Actuator /beansviz

/beans Graphviz

Page 128: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 129: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 130: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 131: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cloud Foundry Integration

Page 132: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Cloud Foundry Integration

Page 133: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 134: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Page 135: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

• https://blog.ik.am/entries/401

Page 136: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Error Spring Data REST

CSRF @EnableOAuth2Sso

@ConditionalOnProperty

Configuration Properties

Spring Boot Actuator

Cloud Foundry Integration

Page 137: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Check Source code!

https://github.com/openenquete/enquete

Page 138: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Spring Cloud Services in PWS• https://content.pivotal.io/blog/building-spring-microservices-

with-cloud-foundrys-new-container-networking-stack

Page 139: 実例で学ぶ、明日から使えるSpring Boot Tips #jsug

‹#›© 2016 Pivotal Software, Inc. All rights reserved.

Check Tutorials!!•https://github.com/Pivotal-Japan •https://pivotal-japan.connpass.com/