Skip to content

Commit 51af0da

Browse files
authored
Merge pull request #19 from Throyer/development
chore: swagger auth
2 parents a940cdb + 69086d1 commit 51af0da

File tree

8 files changed

+48
-32
lines changed

8 files changed

+48
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ docker-compose -p common-api-development -f docker-compose.dev.yml up -d
125125
Building image for production
126126
```bash
127127
cd docker
128-
DOCKER_BUILDKIT=1 docker build -f Dockerfile.prod -t common-api:4.1.1 .
128+
DOCKER_BUILDKIT=1 docker build -f Dockerfile.prod -t common-api:4.1.1 ../
129129
```
130130

131131
docker compose for production

docker/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DB_URL=common_app
22
DB_USERNAME=root
3-
DB_PASSWORD=root
3+
DB_PASSWORD=root
4+
TOKEN_SECRET=secret

docker/docker-compose.prod.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ services:
2828
DB_URL: database:5432/${DB_NAME}
2929
DB_USERNAME: ${DB_USERNAME}
3030
DB_PASSWORD: ${DB_PASSWORD}
31+
TOKEN_SECRET: ${TOKEN_SECRET}
32+
DB_SHOW_SQL: "false"
33+
PRIVATE_SWAGGER: "true"

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<description>Exemplo de api simples com Spring Boot</description>
1616

1717
<properties>
18+
<springdoc.version>1.6.9</springdoc.version>
1819
<java.version>17</java.version>
1920
</properties>
2021

@@ -86,17 +87,17 @@
8687
<dependency>
8788
<groupId>org.springdoc</groupId>
8889
<artifactId>springdoc-openapi-ui</artifactId>
89-
<version>1.6.6</version>
90+
<version>${springdoc.version}</version>
9091
</dependency>
9192
<dependency>
9293
<groupId>org.springdoc</groupId>
9394
<artifactId>springdoc-openapi-webmvc-core</artifactId>
94-
<version>1.6.6</version>
95+
<version>${springdoc.version}</version>
9596
</dependency>
9697
<dependency>
9798
<groupId>org.springdoc</groupId>
9899
<artifactId>springdoc-openapi-security</artifactId>
99-
<version>1.6.5</version>
100+
<version>${springdoc.version}</version>
100101
</dependency>
101102

102103
<!-- Token JWT -->

src/main/java/com/github/throyer/common/springboot/configurations/SpringSecurityConfiguration.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import static com.github.throyer.common.springboot.constants.SECURITY.LOGOUT_URL;
99
import static com.github.throyer.common.springboot.constants.SECURITY.PASSWORD_ENCODER;
1010
import static com.github.throyer.common.springboot.constants.SECURITY.PASSWORD_PARAMETER;
11+
import static com.github.throyer.common.springboot.constants.SECURITY.PRIVATE_SWAGGER;
1112
import static com.github.throyer.common.springboot.constants.SECURITY.PUBLIC_API_ROUTES;
1213
import static com.github.throyer.common.springboot.constants.SECURITY.SESSION_COOKIE_NAME;
13-
import static com.github.throyer.common.springboot.constants.SECURITY.STATIC_FILES;
1414
import static com.github.throyer.common.springboot.constants.SECURITY.TOKEN_SECRET;
1515
import static com.github.throyer.common.springboot.constants.SECURITY.USERNAME_PARAMETER;
1616
import static com.github.throyer.common.springboot.utils.Responses.forbidden;
1717
import static org.springframework.http.HttpMethod.GET;
1818
import static org.springframework.http.HttpMethod.POST;
19-
import static org.springframework.security.config.Customizer.withDefaults;
2019
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
2120

2221
import com.github.throyer.common.springboot.domain.session.service.SessionService;
@@ -32,7 +31,6 @@
3231
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
3332
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3433
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
35-
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
3634
import org.springframework.security.web.SecurityFilterChain;
3735
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3836
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@@ -72,19 +70,13 @@ public AuthenticationManager authenticationManager(
7270
) throws Exception {
7371
return configuration.getAuthenticationManager();
7472
}
75-
76-
@Bean
77-
public WebSecurityCustomizer webSecurityCustomizer() {
78-
return (web) -> web.ignoring().antMatchers(STATIC_FILES);
79-
}
80-
73+
8174
@Bean
8275
@Order(1)
8376
public SecurityFilterChain api(HttpSecurity http) throws Exception {
8477
PUBLIC_API_ROUTES.injectOn(http);
8578

8679
http
87-
.httpBasic(withDefaults())
8880
.antMatcher("/api/**")
8981
.authorizeRequests()
9082
.anyRequest()
@@ -141,4 +133,25 @@ public SecurityFilterChain app(HttpSecurity http) throws Exception {
141133

142134
return http.build();
143135
}
136+
137+
@Bean
138+
@Order(4)
139+
public SecurityFilterChain swagger(HttpSecurity http) throws Exception {
140+
141+
if (PRIVATE_SWAGGER) {
142+
http
143+
.authorizeRequests()
144+
.antMatchers("/swagger-ui/**", "/swagger-ui.html", "/**.html", "/documentation/**")
145+
.authenticated()
146+
.and()
147+
.httpBasic();
148+
} else {
149+
http
150+
.authorizeRequests()
151+
.antMatchers("/swagger-ui/**", "/swagger-ui.html", "/**.html", "/documentation/**")
152+
.permitAll();
153+
}
154+
155+
return http.build();
156+
}
144157
}

src/main/java/com/github/throyer/common/springboot/constants/SECURITY.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,20 @@ public class SECURITY {
1818
public SECURITY(
1919
@Value("${token.secret}") String tokenSecret,
2020
@Value("${token.expiration-in-hours}") Integer tokenExpirationInHours,
21-
@Value("${token.refresh.expiration-in-days}") Integer refreshTokenExpirationInDays
21+
@Value("${token.refresh.expiration-in-days}") Integer refreshTokenExpirationInDays,
22+
@Value("${server.servlet.session.cookie.name}") String sessionCookieName,
23+
@Value("${swagger.is-private}") Boolean privateSwagger
2224
) {
2325
SECURITY.TOKEN_SECRET = tokenSecret;
2426
SECURITY.TOKEN_EXPIRATION_IN_HOURS = tokenExpirationInHours;
2527
SECURITY.REFRESH_TOKEN_EXPIRATION_IN_DAYS = refreshTokenExpirationInDays;
28+
SECURITY.SESSION_COOKIE_NAME = sessionCookieName;
29+
SECURITY.PRIVATE_SWAGGER = privateSwagger;
2630
}
2731

28-
public static final String[] STATIC_FILES = {
29-
"/robots.txt",
30-
"/font/**",
31-
"/css/**",
32-
"/webjars/**",
33-
"/js/**",
34-
"/favicon.ico",
35-
"/**.html",
36-
"/documentation/**"
37-
};
38-
3932
public static final PublicRoutes PUBLIC_API_ROUTES = create()
40-
.add(GET, "/api", "/api/documentation/**")
41-
.add(POST, "/api/users", "/api/sessions/**", "/api/recoveries/**", "/api/documentation/**");
33+
.add(GET, "/api")
34+
.add(POST, "/api/users", "/api/sessions/**", "/api/recoveries/**");
4235

4336
public static final Integer DAY_MILLISECONDS = 86400;
4437
public static final JsonWebToken JWT = new JsonWebToken();
@@ -52,6 +45,9 @@ public SECURITY(
5245
public static Integer TOKEN_EXPIRATION_IN_HOURS;
5346
public static Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
5447

48+
public static String SESSION_COOKIE_NAME;
49+
public static Boolean PRIVATE_SWAGGER;
50+
5551
public static final String USERNAME_PARAMETER = "email";
5652
public static final String PASSWORD_PARAMETER = "password";
5753

@@ -61,8 +57,6 @@ public SECURITY(
6157
public static final String ACESSO_NEGADO_URL = LOGIN_URL + "?denied=true";
6258
public static final String LOGOUT_URL = "/app/logout";
6359

64-
public static final String SESSION_COOKIE_NAME = "JSESSIONID";
65-
6660
public static final String SECURITY_TYPE = "Bearer";
6761
public static final String AUTHORIZATION_HEADER = "Authorization";
6862
public static final String ACCEPTABLE_TOKEN_TYPE = SECURITY_TYPE + " ";

src/main/resources/application.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ springdoc.api-docs.path=/documentation/schemas
2828
springdoc.default-produces-media-type=application/json
2929
springdoc.default-consumes-media-type=application/json
3030

31-
# token
31+
# security
3232
token.expiration-in-hours=${TOKEN_EXPIRATION_IN_HOURS:24}
3333
token.refresh.expiration-in-days=${REFRESH_TOKEN_EXPIRATION_IN_DAYS:7}
3434
token.secret=${TOKEN_SECRET:secret}
35+
server.servlet.session.cookie.name=API_EXAMPLE_SESSION_ID
36+
swagger.is-private=${PRIVATE_SWAGGER:true}
3537

3638
# smtp configurations
3739
spring.mail.host=${SMTP_HOST:smtp.gmail.com}

src/test/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ spring.jpa.hibernate.ddl-auto=none
2525
token.expiration-in-hours=24
2626
token.refresh.expiration-in-days=7
2727
token.secret=secret
28+
server.servlet.session.cookie.name=JSESSIONID
29+
swagger.is-private=false
2830

2931
# recovery email
3032
recovery.minutes-to-expire=20

0 commit comments

Comments
 (0)