Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ OAuth2Configuration
\
https://java-api.pocketmsg.ru
http://localhost:8888
https://localhost:8888
https://localhost:8888/swagger-ui/
24 changes: 10 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<start-class>ru.geekbrains.pocket.backend.PocketBackendApplication</start-class>
<swagger.version>2.9.2</swagger.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -57,10 +56,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>

<!-- Spring Security -->
<dependency>
Expand Down Expand Up @@ -90,6 +85,12 @@
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>

<!-- Logger -->
<!-- log4j2 -->
Expand All @@ -102,27 +103,22 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<version>1.18.42</version>
<scope>provided</scope>
</dependency>

<!-- JSON -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<version>2.13.2</version>
</dependency>

<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void configure(WebSecurity web) throws Exception {
web.httpFirewall(allowHttpMethodsFirewall());
web.ignoring().antMatchers("/webjars/**", "/v2/api-docs/**",
"/configuration/ui/**", "/configuration/security/**",
"/swagger-resources/**", "/swagger-ui.html/**", "/swagger-ui.html#/**");
"/swagger-resources/**", "/swagger-ui/**");
}

@Override
Expand Down
54 changes: 0 additions & 54 deletions src/main/java/ru/geekbrains/pocket/backend/domain/db/Role.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ru.geekbrains.pocket.backend.security;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

//https://www.baeldung.com/spring-security-block-brute-force-authentication-attempts
Expand All @@ -16,40 +15,29 @@
public class LoginAttemptService {

private final int MAX_ATTEMPT = 10;
private LoadingCache<String, Integer> attemptsCache;
private final LoadingCache<String, Integer> attemptsCache;

public LoginAttemptService() {
super();
attemptsCache = CacheBuilder.newBuilder()
attemptsCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.DAYS)
.build(new CacheLoader<String, Integer>() {
@Override
public Integer load(final String key) {
return 0;
}
});
.build(key -> 0); // Дефолтное значение
}

public void loginSucceeded(final String key) {
attemptsCache.invalidate(key);
}

private int getAttempts(String key) {
return Optional.ofNullable(attemptsCache.get(key)).orElse(0);
}

public void loginFailed(final String key) {
int attempts = 0;
try {
attempts = attemptsCache.get(key);
} catch (final ExecutionException e) {
attempts = 0;
}
int attempts = getAttempts(key);
attempts++;
attemptsCache.put(key, attempts);
}

public boolean isBlocked(final String key) {
try {
return attemptsCache.get(key) >= MAX_ATTEMPT;
} catch (final ExecutionException e) {
return false;
}
return getAttempts(key) >= MAX_ATTEMPT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Component
public class JwtTokenUtil implements Serializable {
private static final long ACCESS_TOKEN_VALIDITY_SECONDS = 30*24*60*60;//30 дней
private static final long ACCESS_TOKEN_VALIDITY_MILLISECONDS = 30*24*60*60*1000L;//30 дней
private static final String SIGNING_KEY = "pocket";

public String getUsernameFromToken(String token) {
Expand Down Expand Up @@ -54,7 +54,7 @@ private String doGenerateToken(User user) {
.setClaims(claims)
.setIssuer("http://geekbrains.ru")
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + ACCESS_TOKEN_VALIDITY_SECONDS*1000))
.setExpiration(new Date(System.currentTimeMillis() + ACCESS_TOKEN_VALIDITY_MILLISECONDS))
.signWith(SignatureAlgorithm.HS256, SIGNING_KEY)
.compact();

Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ server.port=8888
#server.tomcat.remote_ip_header=x-forwarded-for
#server.tomcat.protocol_header=x-forwarded-proto
#
# ��������� ������� �� ������ � SSL/TLS � ������������� HTTPS
#security.require-ssl=true
#server.ssl.key-alias=tomcat
#server.ssl.key-store=classpath:keystore.jks
#server.ssl.key-store-password=pocket
#server.ssl.key-password=pocket
# Configure the server to run with SSL/TLS and use HTTPS
security.require-ssl=true
server.ssl.key-alias=tomcat
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=pocket
server.ssl.key-password=pocket
#
# DATA PROPERTIES
#mongodb
spring.data.mongodb.repositories.enabled=true
#spring.data.mongodb.uri=mongodb://localhost:27017/pocketdb
#spring.data.mongodb.uri=mongodb://pocket:pocket@89.208.196.184:27017/pocketdb
spring.data.mongodb.uri=mongodb+srv://pocket:pocket@cluster0-vsjrv.mongodb.net/pocketdb?retryWrites=true&w=majority
spring.data.mongodb.uri=mongodb://pocket:pocket@localhost:27017/pocketdb
#spring.data.mongodb.uri=mongodb://pocket:pocket@192.168.1.37:27017/pocketdb
#spring.data.mongodb.uri=mongodb+srv://pocket:pocket@cluster0-vsjrv.mongodb.net/pocketdb?retryWrites=true&w=majority
#
# OTHER
spring.jackson.serialization.indent_output=true
Expand Down
Binary file modified src/main/resources/keystore.jks
Binary file not shown.