Skip to content

Commit

Permalink
update: refactor for BEARER and service,
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladik-gif committed Oct 17, 2024
1 parent afe5721 commit 4eaa683
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Component
@ConfigurationProperties(prefix = "security.jwt")
public class SecurityJwtProperties {
private String tokenType;
private String secretKey;
private Duration accessExpiration;
private Duration refreshExpiration;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/chat/yourway/security/JwtAuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class JwtAuthFilter extends OncePerRequestFilter {
protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) {

if (isNotAuthorizationHeader(request) && isNotTokenParameter(request)) {
log.warn("Request without authorization. Header or parameter does not contain {}", AUTHORIZATION);
filterChain.doFilter(request, response);
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/com/chat/yourway/security/JwtService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chat.yourway.security;

import static com.chat.yourway.utils.Constants.BEARER;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.chat.yourway.config.security.SecurityJwtProperties;
Expand Down Expand Up @@ -40,20 +41,17 @@ public String generateRefreshToken(UserDetails userDetails) {
}

public String extractToken(HttpServletRequest request) {
String token = request.getHeader(AUTHORIZATION);
var token = request.getHeader(AUTHORIZATION);

if (token == null) {
token = request.getParameter(AUTHORIZATION);
}

final String tokenType = jwtProperties.getTokenType();
final String tokenTypePrefix = tokenType + " ";

if (isNotValidTokenType(token, tokenTypePrefix)) {
log.warn("Invalid token type, token type should be [{}]", tokenType);
throw new InvalidTokenException("Invalid token type, token type should be [" + tokenType + "]");
if (isNotValidTokenType(token)) {
log.warn("Invalid token type, token type should be [{}]", BEARER);
throw new InvalidTokenException("Invalid token type, token type should be [" + BEARER + "]");
}
return token.substring(tokenTypePrefix.length());
return token.substring(BEARER.length());
}

public boolean isTokenValid(String token, UserDetails userDetails) {
Expand All @@ -72,8 +70,8 @@ private String generateRefreshTokenBuild(Map<String, Object> extraClaims, UserDe
return buildToken(extraClaims, userDetails, jwtProperties.getRefreshExpiration().toMillis());
}

private boolean isNotValidTokenType(String token, String tokenTypePrefix) {
return token == null || !token.startsWith(tokenTypePrefix);
private boolean isNotValidTokenType(String token) {
return token == null || !token.startsWith(BEARER);
}

private String buildToken(Map<String, Object> extraClaims, UserDetails userDetails, long expiration) {
Expand All @@ -82,7 +80,7 @@ private String buildToken(Map<String, Object> extraClaims, UserDetails userDetai
.setSubject(userDetails.getUsername())
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + expiration))
.signWith(getSigningKey(), SignatureAlgorithm.HS256)
.signWith(getSigningKey())
.compact();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/chat/yourway/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public class Constants {
public static final String UUID_REGEX_PATTERN =
"\\b[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\\b";
public static final String TOKEN_PARAMETER = "?token=";
public static final String BEARER = "BEARER ";
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ socket.error-prefix=/specific/error
socket.time-to-first-message=999999

#Security:
security.jwt.token-type=Bearer
security.jwt.secret-key=${SECRET_KEY}
security.jwt.access-expiration=10m
# 7 days
Expand Down

0 comments on commit 4eaa683

Please sign in to comment.