🔨 Add swagger

This commit is contained in:
SebClem 2022-05-24 22:59:52 +02:00
parent 62db3f0f6d
commit 5ba9ad39a3
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
7 changed files with 37 additions and 5 deletions

View File

@ -39,6 +39,9 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-log4j2") implementation("org.springframework.boot:spring-boot-starter-log4j2")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client") implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springdoc:springdoc-openapi-ui:1.6.8")
implementation("org.springdoc:springdoc-openapi-security:1.6.8")
implementation('org.liquibase:liquibase-core') implementation('org.liquibase:liquibase-core')
implementation('io.jsonwebtoken:jjwt-api:0.11.5') implementation('io.jsonwebtoken:jjwt-api:0.11.5')

View File

@ -1,5 +1,7 @@
package net.Broken.Api.Controllers; package net.Broken.Api.Controllers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
@ -10,10 +12,12 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/api/v2/hello") @RequestMapping("/api/v2/hello")
@CrossOrigin(origins = "*", maxAge = 3600) @CrossOrigin(origins = "*", maxAge = 3600)
public class HelloController { public class HelloController {
@GetMapping("world") @GetMapping("world")
@Operation(security = { @SecurityRequirement(name = "jwt") })
public String helloWorld(Authentication authentication){ public String helloWorld(Authentication authentication){
UserEntity principal = (UserEntity) authentication.getPrincipal(); UserEntity principal = (UserEntity) authentication.getPrincipal();
return "Hello " + principal.getUsername(); return "Hello " + principal.getUsername();

View File

@ -0,0 +1,18 @@
package net.Broken.Api.OpenApi;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI().components(new Components()
.addSecuritySchemes("jwt",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT").name("JWT")))
.addSecurityItem(new SecurityRequirement().addList("jwt"));
}
}

View File

@ -26,7 +26,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/api/v2/auth/**").permitAll() .antMatchers("/api/v2/auth/**").permitAll()
.anyRequest().authenticated(); .antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/v3/api-docs/**").permitAll()
.anyRequest().denyAll();
http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class); http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class);

View File

@ -123,15 +123,15 @@ public class DiscordOauthService {
public UserEntity updateUserInfo(DiscordOauthUserInfo discordOauthUserInfo, UserEntity userEntity){ public UserEntity updateUserInfo(DiscordOauthUserInfo discordOauthUserInfo, UserEntity userEntity){
boolean updated = false; boolean updated = false;
if(!userEntity.getUsername().equals(discordOauthUserInfo.username())){ if(userEntity.getUsername() == null || !userEntity.getUsername().equals(discordOauthUserInfo.username())){
userEntity.setUsername(discordOauthUserInfo.username()); userEntity.setUsername(discordOauthUserInfo.username());
updated = true; updated = true;
} }
if(!userEntity.getDiscriminator().equals(discordOauthUserInfo.discriminator())){ if(userEntity.getDiscriminator() == null || !userEntity.getDiscriminator().equals(discordOauthUserInfo.discriminator())){
userEntity.setDiscriminator(discordOauthUserInfo.discriminator()); userEntity.setDiscriminator(discordOauthUserInfo.discriminator());
updated = true; updated = true;
} }
if(!userEntity.getAvatar().equals(discordOauthUserInfo.avatar())){ if(userEntity.getAvatar() == null || !userEntity.getAvatar().equals(discordOauthUserInfo.avatar())){
userEntity.setAvatar(discordOauthUserInfo.avatar()); userEntity.setAvatar(discordOauthUserInfo.avatar());
updated = true; updated = true;
} }

View File

@ -40,6 +40,8 @@ public class JwtService {
return Jwts.builder() return Jwts.builder()
.setSubject(user.getUsername()) .setSubject(user.getUsername())
.claim("discord_id", user.getDiscordId()) .claim("discord_id", user.getDiscordId())
.claim("avatar", user.getAvatar())
.claim("discriminator", user.getDiscriminator())
.setId(uuid.toString()) .setId(uuid.toString())
.setIssuedAt(iat) .setIssuedAt(iat)
.setNotBefore(nbf) .setNotBefore(nbf)

View File

@ -23,4 +23,7 @@ discord:
client-secret: ${CLIENT_SECRET} client-secret: ${CLIENT_SECRET}
token-endpoint: https://discord.com/api/oauth2/token token-endpoint: https://discord.com/api/oauth2/token
tokenRevokeEndpoint: https://discord.com/api/oauth2/token/revoke tokenRevokeEndpoint: https://discord.com/api/oauth2/token/revoke
userInfoEnpoint: https://discord.com/api/users/@me userInfoEnpoint: https://discord.com/api/users/@me
springdoc:
paths-to-match: /api/v2/**