Browse Source

Move to lombok

master
Dmitriy Sim 3 years ago
parent
commit
5aa68ace97
11 changed files with 682 additions and 714 deletions
  1. +11
    -0
      .vscode/extensions.json
  2. +1
    -1
      demo-client-app/.eslintcache
  3. +15
    -5
      demo-client-app/src/App.js
  4. +618
    -631
      demo-client-app/yarn.lock
  5. +1
    -0
      demo-resource-server/build.gradle
  6. +0
    -26
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/LoggingConfiguration.java
  7. +11
    -11
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/WebSecurityConfig.java
  8. +8
    -22
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/model/UserEntity.java
  9. +0
    -3
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/persistence/repository/IUserEntityRepository.java
  10. +0
    -4
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/service/UserService.java
  11. +17
    -11
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/web/controller/UserInfoController.java

+ 11
- 0
.vscode/extensions.json View File

@ -0,0 +1,11 @@
{
"recommendations": [
"dbaeumer.vscode-eslint",
"msjsdiag.debugger-for-chrome",
"esbenp.prettier-vscode",
"vscjava.vscode-java-pack",
"pivotal.vscode-boot-dev-pack",
"gabrielbb.vscode-lombok",
"ms-azuretools.vscode-docker"
]
}

+ 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":1464,"mtime":1611406172036,"results":"6","hashOfConfig":"5"},{"size":1404,"mtime":1611250251472,"results":"7","hashOfConfig":"5"},{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"10"},"2g1olq",{"filePath":"11","messages":"12","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"13","messages":"14","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"10"},"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/reportWebVitals.js",[],["15","16"],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/App.js",[],"/Users/dima/Projects/jce/auth-flow/demo-client-app/src/index.js",[],{"ruleId":"17","replacedBy":"18"},{"ruleId":"19","replacedBy":"20"},"no-native-reassign",["21"],"no-negated-in-lhs",["22"],"no-global-assign","no-unsafe-negation"]

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

@ -3,14 +3,14 @@ import logo from "./logo.svg";
import "./App.css"; import "./App.css";
function App({ keycloak }) { function App({ keycloak }) {
const callApi = useCallback(async () => {
const response = await fetch("http://localhost:8081/api/userinfo", {
const fetchFromApi = useCallback(async (url) => {
const response = await fetch(url, {
headers: { headers: {
Authorization: `Bearer ${keycloak.token}`, Authorization: `Bearer ${keycloak.token}`,
}, },
}); });
if (response.ok) { if (response.ok) {
alert(`Response is: ${await response.text()}`);
alert(await response.text());
} else { } else {
console.error(response.statusText); console.error(response.statusText);
} }
@ -27,10 +27,20 @@ function App({ keycloak }) {
className="App-link" className="App-link"
onClick={(e) => { onClick={(e) => {
e.preventDefault(); e.preventDefault();
callApi();
fetchFromApi("http://localhost:8081/api/userinfo");
}} }}
> >
Call API
Get user email
</span>
<br />
<span
className="App-link"
onClick={(e) => {
e.preventDefault();
fetchFromApi("http://localhost:8081/api/users");
}}
>
Get all users
</span> </span>
<br /> <br />
<span <span


+ 618
- 631
demo-client-app/yarn.lock
File diff suppressed because it is too large
View File


+ 1
- 0
demo-resource-server/build.gradle View File

@ -2,6 +2,7 @@ plugins {
id 'org.springframework.boot' version '2.4.2' id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java' id 'java'
id "io.freefair.lombok" version "5.3.0"
} }
group = 'ru.digitalbanana' group = 'ru.digitalbanana'


+ 0
- 26
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/LoggingConfiguration.java View File

@ -1,26 +0,0 @@
package ru.digitalbanana.demoresourceserver.config;
import org.slf4j.*;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.context.annotation.*;
import org.springframework.core.MethodParameter;
import static java.util.Optional.*;
import java.lang.reflect.Field;
@Configuration
public class LoggingConfiguration {
@Bean
@Scope("prototype")
public Logger logger(final InjectionPoint ip) {
return LoggerFactory.getLogger(of(ip.getMethodParameter())
.<Class>map(MethodParameter::getContainingClass)
.orElseGet(
() -> ofNullable(ip.getField())
.map(Field::getDeclaringClass)
.orElseThrow(IllegalArgumentException::new)
)
);
}
}

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

@ -10,16 +10,16 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { 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();
http.cors()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/userinfo", "/user/**")
.authenticated()
// .hasAuthority("SCOPE_web-api")
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer()
.jwt();
} }
} }

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

@ -5,6 +5,13 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity @Entity
@Table(name = "user_entity") @Table(name = "user_entity")
public class UserEntity { public class UserEntity {
@ -14,26 +21,5 @@ public class UserEntity {
private String id; private String id;
@Column(name = "email") @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 + "]";
}
private String email;
} }

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

@ -1,11 +1,8 @@
package ru.digitalbanana.demoresourceserver.persistence.repository; package ru.digitalbanana.demoresourceserver.persistence.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity; import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity;
public interface IUserEntityRepository extends CrudRepository<UserEntity, String> { public interface IUserEntityRepository extends CrudRepository<UserEntity, String> {
List<UserEntity> findByEmailContainingIgnoreCase(String email);
} }

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

@ -16,8 +16,4 @@ public class UserService {
public Iterable<UserEntity> findAll() { public Iterable<UserEntity> findAll() {
return userRepository.findAll(); return userRepository.findAll();
} }
public List<UserEntity> findByEmail(String email) {
return userRepository.findByEmailContainingIgnoreCase(email);
}
} }

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

@ -3,8 +3,11 @@ package ru.digitalbanana.demoresourceserver.web.controller;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.jwt.Jwt;
@ -13,32 +16,35 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity; import ru.digitalbanana.demoresourceserver.persistence.model.UserEntity;
import ru.digitalbanana.demoresourceserver.service.UserService; import ru.digitalbanana.demoresourceserver.service.UserService;
@Slf4j
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@RestController @RestController
public class UserInfoController { public class UserInfoController {
private final UserService userService; private final UserService userService;
private final Logger logger;
@Autowired @Autowired
public UserInfoController(final Logger logger, final UserService userService) {
this.logger = logger;
public UserInfoController(final UserService userService) {
this.userService = userService; this.userService = userService;
} }
@GetMapping(value = "/userinfo") @GetMapping(value = "/userinfo")
public Map<String, Object> getUser(@AuthenticationPrincipal Jwt principal) {
return Collections.singletonMap("user_name", principal.getClaimAsString("email"));
public String userinfo(@AuthenticationPrincipal Jwt principal) {
return principal.getClaimAsString("email");
} }
@GetMapping(value = "/user/{email}")
public List<UserEntity> getMethodName(@PathVariable String email) {
logger.debug("Request user by email {}", email);
return userService.findByEmail(email);
@GetMapping(value = "/users")
public List<UserEntity> allUsers() {
log.debug("Request all users");
return StreamSupport
.stream(userService.findAll().spliterator(), false)
.filter(user -> user.getEmail() != null)
.collect(Collectors.toList())
;
} }
} }

Loading…
Cancel
Save