forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
17 changed files
with
516 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...boot-modules/spring-boot-springdoc-2/src/main/java/com/baeldung/cats/CatsApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.baeldung.cats; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.security.SecurityScheme; | ||
|
||
@SpringBootApplication | ||
@OpenAPIDefinition(info = @Info(title = "Automated Testing for OpenAPI Endpoints Using CATS", version = "1.0.0"), security = { @SecurityRequirement(name = "Authorization") }) | ||
@SecurityScheme(type = SecuritySchemeType.HTTP, name = "Authorization", in = SecuritySchemeIn.HEADER, scheme = "Bearer") | ||
public class CatsApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(CatsApplication.class, args); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...modules/spring-boot-springdoc-2/src/main/java/com/baeldung/cats/config/JacksonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.baeldung.cats.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonFactoryBuilder; | ||
import com.fasterxml.jackson.core.StreamReadConstraints; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Configuration | ||
public class JacksonConfig { | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
JsonFactory factory = new JsonFactoryBuilder().streamReadConstraints(StreamReadConstraints.builder() | ||
.maxStringLength(100) | ||
.build()) | ||
.build(); | ||
|
||
ObjectMapper mapper = new ObjectMapper(factory); | ||
mapper.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); | ||
mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
|
||
mapper.disable(DeserializationFeature.ACCEPT_FLOAT_AS_INT); | ||
mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); | ||
return mapper; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...odules/spring-boot-springdoc-2/src/main/java/com/baeldung/cats/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.baeldung.cats.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.jwt.JwtDecoder; | ||
import org.springframework.security.oauth2.jwt.JwtException; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
private static final String TEST_TOKEN = "valid-token"; | ||
private static final String ACCESS_DENIED = "access denied"; | ||
|
||
private final Logger log = LoggerFactory.getLogger(SecurityConfig.class); | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
return http.authorizeHttpRequests(auth -> auth.requestMatchers("/swagger-ui-custom.html", "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", "/swagger-ui/index.html", "/api-docs/**") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated()) | ||
.oauth2ResourceServer(rs -> rs.jwt(jwt -> jwt.decoder(jwtDecoder()))) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
JwtDecoder jwtDecoder() { | ||
return token -> { | ||
if (!TEST_TOKEN.equals(token)) { | ||
log.warn(ACCESS_DENIED); | ||
throw new JwtException(ACCESS_DENIED); | ||
} | ||
|
||
return Jwt.withTokenValue(token) | ||
.claim("token", TEST_TOKEN) | ||
.header("token", TEST_TOKEN) | ||
.build(); | ||
}; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ing-boot-springdoc-2/src/main/java/com/baeldung/cats/config/ZeroWidthIntDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.cats.config; | ||
|
||
import java.io.IOException; | ||
|
||
import com.baeldung.cats.utils.RegexUtils; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
public class ZeroWidthIntDeserializer extends JsonDeserializer<Integer> { | ||
|
||
@Override | ||
public Integer deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
return Integer.valueOf(RegexUtils.removeZeroWidthChars(parser.getText())); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...-boot-springdoc-2/src/main/java/com/baeldung/cats/config/ZeroWidthStringDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.cats.config; | ||
|
||
import java.io.IOException; | ||
|
||
import com.baeldung.cats.utils.RegexUtils; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
public class ZeroWidthStringDeserializer extends JsonDeserializer<String> { | ||
|
||
@Override | ||
public String deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
return RegexUtils.removeZeroWidthChars(parser.getText()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...es/spring-boot-springdoc-2/src/main/java/com/baeldung/cats/controller/ItemController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.baeldung.cats.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.baeldung.cats.model.BadApiRequest; | ||
import com.baeldung.cats.model.Item; | ||
import com.baeldung.cats.service.ItemService; | ||
|
||
import io.swagger.v3.oas.annotations.media.ArraySchema; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import jakarta.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping("/api/v2/item") | ||
@ApiResponse(responseCode = "401", description = "Unauthorized", content = { @Content(mediaType = MediaType.TEXT_PLAIN_VALUE, schema = @Schema(implementation = String.class)) }) | ||
@ApiResponse(responseCode = "400", description = "Bad Request", content = { @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = BadApiRequest.class)) }) | ||
public class ItemController { | ||
|
||
private ItemService service; | ||
|
||
public ItemController(ItemService service) { | ||
this.service = service; | ||
} | ||
|
||
@PostMapping | ||
@ApiResponse(responseCode = "200", description = "Success", content = { @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Item.class)) }) | ||
public ResponseEntity<Item> post(@Valid @RequestBody Item item) { | ||
service.insert(item); | ||
return ResponseEntity.ok(item); | ||
} | ||
|
||
@GetMapping | ||
@ApiResponse(responseCode = "200", description = "Success", content = { @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, array = @ArraySchema(schema = @Schema(implementation = Item.class))) }) | ||
public ResponseEntity<List<Item>> get() { | ||
return ResponseEntity.ok(service.findAll()); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@ApiResponse(responseCode = "200", description = "Success", content = { @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Item.class)) }) | ||
public ResponseEntity<Item> get(@PathVariable String id) { | ||
try { | ||
return ResponseEntity.ok(service.findById(id)); | ||
} catch (EntityNotFoundException e) { | ||
return ResponseEntity.status(404) | ||
.build(); | ||
} | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ApiResponse(responseCode = "204", description = "Success") | ||
public ResponseEntity<Void> delete(@PathVariable String id) { | ||
try { | ||
service.deleteById(id); | ||
|
||
return ResponseEntity.status(204) | ||
.build(); | ||
} catch (EntityNotFoundException e) { | ||
return ResponseEntity.status(404) | ||
.build(); | ||
} | ||
} | ||
} |
Oops, something went wrong.