Newer
Older
springboot-auth201 / src / main / java / com / heaerie / server / auth201 / Auth201Server / controller / UserController.java
@agalyaramadoss agalyaramadoss on 29 Nov 1 KB first commit
package com.heaerie.server.auth201.Auth201Server.controller;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserController {

    /**
     * Endpoint to get user information (protected resource)
     * Requires a valid access token
     */
    @GetMapping("/userinfo")
    public Map<String, Object> userInfo(@AuthenticationPrincipal Jwt jwt) {
        return Map.of(
            "sub", jwt.getSubject(),
            "username", jwt.getClaimAsString("sub"),
            "scopes", jwt.getClaimAsStringList("scope"),
            "exp", jwt.getExpiresAt(),
            "iat", jwt.getIssuedAt()
        );
    }

    /**
     * Public endpoint to check server status
     */
    @GetMapping("/")
    public Map<String, String> home() {
        return Map.of(
            "message", "OAuth 2.1 Authorization Server is running",
            "wellKnown", "http://localhost:9000/.well-known/oauth-authorization-server"
        );
    }
}