Skip to content

Ylab-001: init commit for chat#1

Open
AphexSign wants to merge 2 commits into
mainfrom
feature/Ylab-001
Open

Ylab-001: init commit for chat#1
AphexSign wants to merge 2 commits into
mainfrom
feature/Ylab-001

Conversation

@AphexSign

Copy link
Copy Markdown
Owner

No description provided.

Comment thread README.md
Comment on lines +76 to +83
docker run -d -p 6379:6379 --name chat-redis redis:7-alpine
```
Запуск приложения
```
mvn clean spring-boot:run
```
Приложение будет доступно по адресу: http://localhost:8080
H2 Console (для разработки):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше было написать dockerfile и добавить в docker compose чтобы запускалось без проблем вне зависимости от окружения.

Comment on lines +13 to +25
public class RedisConstants {

/**
* Redis stream key for the dead-letter queue.
*/
public static final String DEAD_LETTER_STREAM_KEY = "chat:dead-letter:stream";

/**
* Consumer group name for the dead-letter queue stream.
*/
public static final String DEAD_LETTER_CONSUMER_GROUP = "chat-dlq-group";

} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему не в конфигурации

Comment on lines +38 to +55

if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7);
try {
String username = jwtUtil.extractUsername(token);
if (username != null
&& SecurityContextHolder.getContext().getAuthentication() == null) {
if (jwtUtil.validateToken(token, username)) {
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(username, null,
new ArrayList<>());
SecurityContextHolder.getContext().setAuthentication(authToken);
}
}
} catch (Exception e) {
}
}
filterChain.doFilter(request, response);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно вынести в приватный метод и сделать инверсию условий, будет более читаемо

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

большая вложенность условий, попробую отрефакторить

Comment on lines +57 to +74
@Around("@annotation(io.ylab.chat.aop.annotation.WithUserContext)")
public Object withUserContext(ProceedingJoinPoint jp) throws Throwable {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated() || "anonymousUser".equals(
auth.getPrincipal())) {
throw new UnauthorizedException("User not authenticated");
}

String username = auth.getName();
UserContext.setUserInfo(UserInfo.builder().username(username).build());

try {
return jp.proceed();
} finally {
UserContext.clear();
log.debug("User context cleared for {}", username);
}
}

@Eugenekulik Eugenekulik Apr 10, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно было сделать на основе scope websocket и заполнять в фильтре или интерсепторе

refill-duration-seconds: 60

jwt:
secret: FITSy9dGK9BlOOrOqOi3xRaWjMPgR9KQtT0GaPiBaKQ7LcYniHsdsSA78iEy8BmOGAXpkVi7Imp9dZeHfJPptA==

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в .env лучше выносить, даже если это учебный проект (взять за практику)

jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create-drop

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше использую миграции

Comment on lines +75 to +87
void withUserContext_authenticated_setsContextAndProceeds() throws Throwable {
Authentication auth = mock(Authentication.class);
when(auth.isAuthenticated()).thenReturn(true);
when(auth.getPrincipal()).thenReturn(TEST_USER);
when(auth.getName()).thenReturn(TEST_USER);
SecurityContextHolder.getContext().setAuthentication(auth);

when(joinPoint.proceed()).thenReturn(EXPECTED_METHOD_RESULT);

Object result = aspect.withUserContext(joinPoint);

verify(joinPoint).proceed();
softly.assertThat(result).isEqualTo(EXPECTED_METHOD_RESULT);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты разделяй на блоки, самому потом проще будет читать их (например //GIVEN//WHEN//THEN)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants