I’m trying to configure an Nginx ingress in Kubernetes to use an authentication service (auth-api) for validating access tokens before routing requests to other services (user-api and other-api). However, the setup isn’t working as expected, and I’m not sure what I’m missing. Here’s what I have so far:
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@CrossOrigin("*")
public class AuthController {
private final AuthService authService;
@PostMapping("/generateAccessToken")
public ResponseEntity<String> generateAccessToken(@RequestBody UserSharable userSharable) {
//My logic here
}
@IsAuthenticated
@GetMapping("/validate")
public void validate(HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/auth-url: "http://auth-api-service.default.svc.cluster.local:8083/auth/validate"
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80
- path: /other
pathType: Prefix
backend:
service:
name: other-api-service
port:
number: 8081
- path: /user
pathType: Prefix
backend:
service:
name: user-api-service
port:
number: 8082
- path: /auth //I added this one just for test purpose. After make auth work i will not add this path to ingress
pathType: Prefix
backend:
service:
name: auth-api-service
port:
number: 8083
Problem:
When I attempt to access the endpoints through the ingress, it seems that the validation process either always fails or doesn’t get triggered at all. Requests are not being properly authenticated, and When i used endpoints without any auth annotation , login, validate, token creation works properly
Questions:
How should the auth-url be properly configured to ensure it only allows access to authenticated requests?
Are there additional annotations or ingress configurations that need to be set to handle JWTs or bearer tokens more effectively?
I have my login and register logic inside user-api. Is it a good practice. Can i add an authantication exemption for these 2 endpoints
Any advice or guidance would be greatly appreciated!