perbaikan handling static token dan respon
This commit is contained in:
parent
176f63d7a6
commit
2b8de7a320
@ -84,7 +84,7 @@ public class JwtAuthFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
map.put("message", "Authorization header is missing");
|
map.put("message", "Tidak ada authorization header");
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
response.setContentType("application/json");
|
response.setContentType("application/json");
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package id.co.anaheim.gateway.span.controllers;
|
|||||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||||
import id.co.anaheim.gateway.span.models.AuthDto;
|
import id.co.anaheim.gateway.span.models.AuthDto;
|
||||||
import id.co.anaheim.gateway.span.models.AuthResponse;
|
import id.co.anaheim.gateway.span.models.AuthResponse;
|
||||||
|
import id.co.anaheim.gateway.span.models.JwtExpiration;
|
||||||
import id.co.anaheim.gateway.span.models.User;
|
import id.co.anaheim.gateway.span.models.User;
|
||||||
import id.co.anaheim.gateway.span.repositories.UserRepository;
|
import id.co.anaheim.gateway.span.repositories.UserRepository;
|
||||||
import id.co.anaheim.gateway.span.repositories.jdbc.JdbcUserRepository;
|
import id.co.anaheim.gateway.span.repositories.jdbc.JdbcUserRepository;
|
||||||
@ -14,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/auth")
|
@RequestMapping("/auth")
|
||||||
public class AuthController {
|
public class AuthController {
|
||||||
@ -30,19 +33,20 @@ public class AuthController {
|
|||||||
AuthResponse response = new AuthResponse();
|
AuthResponse response = new AuthResponse();
|
||||||
User user = repository.findByUsername(authDto.getUsername());
|
User user = repository.findByUsername(authDto.getUsername());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
response.setMessage("User not found");
|
response.setMessage("Pengguna tidak ditemukan");
|
||||||
response.setStatus(false);
|
|
||||||
} else {
|
} else {
|
||||||
BCrypt.Result result = BCrypt.verifyer().verify(authDto.getPassword().toCharArray(), user.getPassword());
|
BCrypt.Result result = BCrypt.verifyer().verify(authDto.getPassword().toCharArray(), user.getPassword());
|
||||||
if (result.verified) {
|
if (result.verified) {
|
||||||
response.setMessage("Success");
|
response.setMessage("Berhasil masuk");
|
||||||
response.setStatus(true);
|
response.setId(user.getId());
|
||||||
response.setUser(user);
|
|
||||||
String token = jwtService.generateToken(user);
|
String token = jwtService.generateToken(user);
|
||||||
response.setToken(token);
|
response.setToken(token);
|
||||||
|
JwtExpiration jwtExpiration = jwtService.extractExpiration(token);
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
response.setTokenExpiration(dateFormat.format(jwtExpiration.getValue()));
|
||||||
} else {
|
} else {
|
||||||
response.setMessage("Invalid passwod");
|
response.setMessage("Password tidak sesuai");
|
||||||
response.setStatus(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
@ -62,14 +66,12 @@ public class AuthController {
|
|||||||
user.setRole("ADMIN");
|
user.setRole("ADMIN");
|
||||||
repository.create(user);
|
repository.create(user);
|
||||||
|
|
||||||
response.setMessage("Success");
|
response.setMessage("Pendaftaran berhasil");
|
||||||
response.setStatus(true);
|
response.setId(user.getId());
|
||||||
response.setUser(user);
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
response.setStatus(false);
|
response.setMessage("Pengguna dengan username admin sudah ada");
|
||||||
response.setMessage("User already exist");
|
response.setId(user.getId());
|
||||||
response.setUser(user);
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class AuthResponse {
|
public class AuthResponse {
|
||||||
private User user;
|
private String id;
|
||||||
private String token;
|
private String token;
|
||||||
private String message;
|
private String message;
|
||||||
private boolean status;
|
private String tokenExpiration;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package id.co.anaheim.gateway.span.services;
|
package id.co.anaheim.gateway.span.services;
|
||||||
|
|
||||||
|
import id.co.anaheim.gateway.span.models.JwtUsername;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.asynchttpclient.AsyncHttpClient;
|
import org.asynchttpclient.AsyncHttpClient;
|
||||||
import org.asynchttpclient.BoundRequestBuilder;
|
import org.asynchttpclient.BoundRequestBuilder;
|
||||||
@ -50,10 +51,14 @@ public class HttpClientService {
|
|||||||
if (!request.headers().header("Authorization").isEmpty()) {
|
if (!request.headers().header("Authorization").isEmpty()) {
|
||||||
String authorizationHeader = request.headers().header("Authorization").get(0);
|
String authorizationHeader = request.headers().header("Authorization").get(0);
|
||||||
String token = authorizationHeader.substring(7);
|
String token = authorizationHeader.substring(7);
|
||||||
String staticToken = jwtService.extractStaticToken(token);
|
JwtUsername username = jwtService.extractUsername(token);
|
||||||
if (!staticToken.equals("admin")) {
|
if (username.isValid() && !username.getValue().equals("admin")) {
|
||||||
boundRequestBuilder.addHeader("token", staticToken);
|
String staticToken = jwtService.extractStaticToken(token);
|
||||||
|
if (staticToken != null) {
|
||||||
|
boundRequestBuilder.addHeader("token", staticToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Future<Response> whenResponse = boundRequestBuilder
|
Future<Response> whenResponse = boundRequestBuilder
|
||||||
.execute();
|
.execute();
|
||||||
|
|||||||
@ -52,11 +52,8 @@ public class JwtService {
|
|||||||
StaticToken staticToken = gson.fromJson(responseBody, StaticToken.class);
|
StaticToken staticToken = gson.fromJson(responseBody, StaticToken.class);
|
||||||
if (staticToken != null) {
|
if (staticToken != null) {
|
||||||
claims.put("token", staticToken.getToken());
|
claims.put("token", staticToken.getToken());
|
||||||
} else {
|
|
||||||
claims.put("token", "admin");
|
|
||||||
}
|
}
|
||||||
} catch (IOException | ExecutionException | InterruptedException e) {
|
} catch (IOException | ExecutionException | InterruptedException e) {
|
||||||
claims.put("token", "admin");
|
|
||||||
log.error("error get token", e);
|
log.error("error get token", e);
|
||||||
}
|
}
|
||||||
log.info("here");
|
log.info("here");
|
||||||
@ -109,19 +106,19 @@ public class JwtService {
|
|||||||
return JwtValidationResult.builder().valid(true).claims(claims).message(message).build();
|
return JwtValidationResult.builder().valid(true).claims(claims).message(message).build();
|
||||||
} catch (MalformedJwtException e) {
|
} catch (MalformedJwtException e) {
|
||||||
log.error("Invalid JWT token: {}", e.getMessage());
|
log.error("Invalid JWT token: {}", e.getMessage());
|
||||||
message = "Invalid JWT token";
|
message = "JWT token tidak sesuai";
|
||||||
} catch (ExpiredJwtException e) {
|
} catch (ExpiredJwtException e) {
|
||||||
log.error("JWT token is expired: {}", e.getMessage());
|
log.error("JWT token is expired: {}", e.getMessage());
|
||||||
message = "JWT token is expired";
|
message = "JWT token telah kadaluarsa";
|
||||||
} catch (UnsupportedJwtException e) {
|
} catch (UnsupportedJwtException e) {
|
||||||
log.error("JWT token is unsupported: {}", e.getMessage());
|
log.error("JWT token is unsupported: {}", e.getMessage());
|
||||||
message = "JWT token is unsupported";
|
message = "JWT token tidak didukung";
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
log.error("JWT claims string is empty: {}", e.getMessage());
|
log.error("JWT claims string is empty: {}", e.getMessage());
|
||||||
message = "JWT claims string is empty";
|
message = "String JWT claims kosong";
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
log.error("Invalid JWT token");
|
log.error("Invalid JWT token");
|
||||||
message = "Invalid JWT token";
|
message = "JWT token tidak sesuai";
|
||||||
}
|
}
|
||||||
return JwtValidationResult.builder().valid(false).message(message).build();
|
return JwtValidationResult.builder().valid(false).message(message).build();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user