Browse Source

refactor to webflux

reactive
Dmitriy Sim 3 years ago
parent
commit
22dbff4a4e
11 changed files with 71 additions and 69 deletions
  1. +1
    -1
      demo-client-app/.eslintcache
  2. +1
    -0
      demo-client-app/package.json
  3. +1
    -1
      demo-client-app/src/App.js
  4. +5
    -3
      demo-resource-server/build.gradle
  5. +2
    -0
      demo-resource-server/lombok.config
  6. +21
    -17
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/WebSecurityConfig.java
  7. +18
    -32
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/model/UserEntity.java
  8. +6
    -5
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/repository/IUserEntityRepository.java
  9. +3
    -4
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/service/UserService.java
  10. +3
    -4
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/web/controller/UserInfoController.java
  11. +10
    -2
      demo-resource-server/src/main/resources/application.yml

+ 1
- 1
demo-client-app/.eslintcache View File

@ -1 +1 @@
[{"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/reportWebVitals.js":"1","/Users/dima/Projects/jce/auth-flow/demo-client-app/src/App.js":"2","/Users/dima/Projects/jce/auth-flow/demo-client-app/src/index.js":"3"},{"size":362,"mtime":1611244349228,"results":"4","hashOfConfig":"5"},{"size":1216,"mtime":1611250955596,"results":"6","hashOfConfig":"5"},{"size":1404,"mtime":1611250251472,"results":"7","hashOfConfig":"5"},{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"ewyx7q",{"filePath":"10","messages":"11","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"12","messages":"13","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/reportWebVitals.js",[],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/App.js",[],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/index.js",[]]
[{"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/reportWebVitals.js":"1","/Users/dima/Projects/jce/auth-flow/demo-client-app/src/App.js":"2","/Users/dima/Projects/jce/auth-flow/demo-client-app/src/index.js":"3"},{"size":362,"mtime":1611244349228,"results":"4","hashOfConfig":"5"},{"size":1212,"mtime":1611308679793,"results":"6","hashOfConfig":"5"},{"size":1404,"mtime":1611250251472,"results":"7","hashOfConfig":"5"},{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"ewyx7q",{"filePath":"10","messages":"11","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"12","messages":"13","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/reportWebVitals.js",[],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/App.js",[],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/index.js",[]]

+ 1
- 0
demo-client-app/package.json View File

@ -2,6 +2,7 @@
"name": "demo-client-app",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:8081",
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",


+ 1
- 1
demo-client-app/src/App.js View File

@ -4,7 +4,7 @@ import "./App.css";
function App({ keycloak }) {
const callApi = useCallback(async () => {
const response = await fetch("http://localhost:8081/api/userinfo", {
const response = await fetch("http://localhost:8081/userinfo", {
headers: {
Authorization: `Bearer ${keycloak.token}`,
},


+ 5
- 3
demo-resource-server/build.gradle View File

@ -2,6 +2,7 @@ plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "io.freefair.lombok" version "5.3.0"
}
group = 'ru.digitalbanana'
@ -18,10 +19,11 @@ repositories {
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
runtimeOnly 'io.r2dbc:r2dbc-postgresql'
// runtimeOnly 'org.postgresql:postgresql'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


+ 2
- 0
demo-resource-server/lombok.config View File

@ -0,0 +1,2 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true

+ 21
- 17
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/WebSecurityConfig.java View File

@ -1,25 +1,29 @@
package ru.digitalbanana.demoresourceserver.config;
import static org.springframework.security.config.Customizer.withDefaults;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@EnableWebFluxSecurity
public class WebSecurityConfig {
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.anyExchange().authenticated()
.and()
.httpBasic().disable()
.oauth2ResourceServer()
.jwt(withDefaults());
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/userinfo", "/user/**")
.authenticated()
// .hasAuthority("SCOPE_web-api")
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer()
.jwt();
}
return http.build();
}
}

+ 18
- 32
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/model/UserEntity.java View File

@ -1,39 +1,25 @@
package ru.digitalbanana.demoresourceserver.persistence.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_entity")
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("user_entity")
public class UserEntity {
@Id
@Column(name = "id")
@Column("id")
private String id;
@Column(name = "email")
public String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Foo [id=" + id + ", email=" + email + "]";
}
@Column("email")
private String email;
}

+ 6
- 5
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/repository/IUserEntityRepository.java View File

@ -1,11 +1,12 @@
package ru.digitalbanana.demoresourceserver.persistence.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity;
public interface IUserEntityRepository extends CrudRepository<UserEntity, String> {
List<UserEntity> findByEmailContainingIgnoreCase(String email);
@Repository
public interface IUserEntityRepository extends ReactiveCrudRepository<UserEntity, String> {
Flux<UserEntity> findByEmailContainingIgnoreCase(String email);
}

+ 3
- 4
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/service/UserService.java View File

@ -1,10 +1,9 @@
package ru.digitalbanana.demoresourceserver.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity;
import ru.digitalbanana.demoresourceserver.persistence.repository.IUserEntityRepository;
@ -13,11 +12,11 @@ public class UserService {
@Autowired IUserEntityRepository userRepository;
public Iterable<UserEntity> findAll() {
public Flux<UserEntity> findAll() {
return userRepository.findAll();
}
public List<UserEntity> findByEmail(String email) {
public Flux<UserEntity> findByEmail(String email) {
return userRepository.findByEmailContainingIgnoreCase(email);
}
}

+ 3
- 4
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/web/controller/UserInfoController.java View File

@ -1,7 +1,6 @@
package ru.digitalbanana.demoresourceserver.web.controller;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
@ -13,10 +12,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity;
import ru.digitalbanana.demoresourceserver.service.UserService;
@CrossOrigin(origins = "*")
@CrossOrigin
@RestController
public class UserInfoController {
@ -36,9 +36,8 @@ public class UserInfoController {
}
@GetMapping(value = "/user/{email}")
public List<UserEntity> getMethodName(@PathVariable String email) {
public Flux<UserEntity> getMethodName(@PathVariable String email) {
logger.debug("Request user by email {}", email);
return userService.findByEmail(email);
}
}

+ 10
- 2
demo-resource-server/src/main/resources/application.yml View File

@ -1,12 +1,20 @@
server:
port: 8081
servlet:
context-path: /api
forward-headers-strategy: framework # nginx reverse proxy
logging.level:
'[org.springframework.web]': debug
####### resource server configuration properties
spring:
r2dbc:
username: postgres
password: P4$$W0RddD
url: r2dbc:postgresql://localhost:5432/keycloak
pool.enabled: true
pool.initial-size: 10
pool.max-idle-time: 1m
pool.max-size: 30
data.r2dbc.repositories.enabled: true
jpa.hibernate.ddl-auto: none
datasource:
initialization-mode: always


Loading…
Cancel
Save