In progress
This commit is contained in:
parent
9b808a521c
commit
32193fb080
4 changed files with 70 additions and 3 deletions
4
http/getToken.http
Normal file
4
http/getToken.http
Normal 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
|
|
@ -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<ObjectMapper> getObjectMapper() {
|
||||
|
@ -33,8 +37,15 @@ public class ApiController implements DefaultApi {
|
|||
|
||||
@Override
|
||||
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();
|
||||
hello.setMsg("Hello World");
|
||||
hello.setMsg(data);
|
||||
return ResponseEntity.ok(hello);
|
||||
}
|
||||
}
|
||||
|
|
12
src/main/java/de/hmmh/pmt/auth/JwtToken.java
Normal file
12
src/main/java/de/hmmh/pmt/auth/JwtToken.java
Normal 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;
|
||||
}
|
40
src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java
Normal file
40
src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue