diff --git a/http/getToken.http b/http/getToken.http new file mode 100644 index 0000000..d198851 --- /dev/null +++ b/http/getToken.http @@ -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 \ No newline at end of file diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index b173e85..0c79842 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -1,16 +1,16 @@ package de.hmmh.pmt; import com.fasterxml.jackson.databind.ObjectMapper; +import de.hmmh.pmt.auth.JwtToken; import de.hmmh.pmt.employee.ApiClient; +import de.hmmh.pmt.employee.api.EmployeeControllerApi; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.dtos.HelloOut; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.client.RestTemplate; import java.util.Optional; @@ -20,6 +20,10 @@ public class ApiController implements DefaultApi { @Autowired private ApiClient apiClient; + @Autowired + private JwtToken apiToken; + @Autowired + private EmployeeControllerApi employeeApi; @Override public Optional getObjectMapper() { @@ -33,8 +37,15 @@ public class ApiController implements DefaultApi { @Override public ResponseEntity 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(); - hello.setMsg("Hello World"); + hello.setMsg(data); return ResponseEntity.ok(hello); } } diff --git a/src/main/java/de/hmmh/pmt/auth/JwtToken.java b/src/main/java/de/hmmh/pmt/auth/JwtToken.java new file mode 100644 index 0000000..67c5897 --- /dev/null +++ b/src/main/java/de/hmmh/pmt/auth/JwtToken.java @@ -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; +} diff --git a/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java b/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java new file mode 100644 index 0000000..f589e1a --- /dev/null +++ b/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java @@ -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); + } +}