In progress
Some checks failed
Quality Check / Validate OAS (push) Failing after 33s
Quality Check / Linting (push) Failing after 1m1s
Quality Check / Testing (push) Successful in 1m3s
Quality Check / Static Analysis (push) Failing after 1m6s

This commit is contained in:
Snoweuph 2024-09-30 06:11:53 +02:00
parent 9b808a521c
commit 32193fb080
Signed by: Snoweuph
GPG key ID: 0021ADF278B2F49B
4 changed files with 70 additions and 3 deletions

4
http/getToken.http Normal file
View file

@ -0,0 +1,4 @@
POST https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded
grant_type=password&client_id=employee-management-service&username=user&password=test

View file

@ -1,16 +1,16 @@
package de.hmmh.pmt; package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.auth.JwtToken;
import de.hmmh.pmt.employee.ApiClient; import de.hmmh.pmt.employee.ApiClient;
import de.hmmh.pmt.employee.api.EmployeeControllerApi;
import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.oas.DefaultApi;
import de.hmmh.pmt.dtos.HelloOut; import de.hmmh.pmt.dtos.HelloOut;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.util.Optional; import java.util.Optional;
@ -20,6 +20,10 @@ public class ApiController implements DefaultApi {
@Autowired @Autowired
private ApiClient apiClient; private ApiClient apiClient;
@Autowired
private JwtToken apiToken;
@Autowired
private EmployeeControllerApi employeeApi;
@Override @Override
public Optional<ObjectMapper> getObjectMapper() { public Optional<ObjectMapper> getObjectMapper() {
@ -33,8 +37,15 @@ public class ApiController implements DefaultApi {
@Override @Override
public ResponseEntity<HelloOut> getHello() { public ResponseEntity<HelloOut> getHello() {
apiClient.setAccessToken(apiToken.getToken());
apiClient.setBasePath("https://employee.szut.dev");
//TODO: Get Authentication working
employeeApi.setApiClient(apiClient);
String data = employeeApi.findAll1().toString();
HelloOut hello = new HelloOut(); HelloOut hello = new HelloOut();
hello.setMsg("Hello World"); hello.setMsg(data);
return ResponseEntity.ok(hello); return ResponseEntity.ok(hello);
} }
} }

View file

@ -0,0 +1,12 @@
package de.hmmh.pmt.auth;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;
@Getter
@Setter
@Component
public class JwtToken{
private String token;
}

View file

@ -0,0 +1,40 @@
package de.hmmh.pmt.auth;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Component
public class JwtTokenFilter extends OncePerRequestFilter {
private final JwtToken token;
public JwtTokenFilter(JwtToken token) {
this.token = token;
}
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {
token.setToken(null);
String authHeader = request.getHeader("Authorization");
if (authHeader == null) {
filterChain.doFilter(request, response);
return;
}
if (!authHeader.startsWith("Bearer ")) {
filterChain.doFilter(request, response);
return;
}
token.setToken(authHeader.substring("Bearer ".length()));
filterChain.doFilter(request, response);
}
}