Spring authorization server + angular app + oidc-client.ts NoResourceFoundException: No static...

Discussão em 'Angular' iniciado por San Jaisy, Novembro 30, 2024.

  1. San Jaisy

    San Jaisy Guest

    I have the below client and security configuration using Spring authorization server

    build.gradle

    dependencies {
    implementation("org.springframework.boot:spring-boot-starter-oauth2-authorization-server")
    implementation("org.springframework.boot:spring-boot-starter-web")
    }


    Client Registration

    @Configuration
    public class Clients {

    @Bean
    public RegisteredClientRepository registeredClientRepository() {
    RegisteredClient oidcClient = RegisteredClient.withId(UUID.randomUUID().toString())
    .clientId("school-staff")
    .clientSecret("school-staff")
    .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
    .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
    .redirectUris( redirectUri-> {
    redirectUri.add("http://localhost:4200/");
    redirectUri.add("https://oauth.pstmn.io/v1/callback");
    redirectUri.add("http://localhost:4200/assets/silent-renew.html");
    redirectUri.add("http://localhost:4200/auth-callback");
    })
    .postLogoutRedirectUri("http://localhost:4200/")
    .scope(OidcScopes.OPENID)
    .scope(OidcScopes.PROFILE)
    .scope(OidcScopes.EMAIL)
    .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
    .build();

    return new InMemoryRegisteredClientRepository(oidcClient);
    }
    }


    Security configuration

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig {

    @Bean
    @Order(1)
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
    throws Exception {
    OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
    OAuth2AuthorizationServerConfigurer.authorizationServer();

    http
    .securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
    .with(authorizationServerConfigurer, (authorizationServer) ->
    authorizationServer
    .oidc(Customizer.withDefaults()) // Enable OpenID Connect 1.0
    )
    // Redirect to the login page when not authenticated from the
    // authorization endpoint
    .exceptionHandling((exceptions) -> exceptions
    .defaultAuthenticationEntryPointFor(
    new LoginUrlAuthenticationEntryPoint("/login"),
    new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
    )
    );

    return http.build();
    }

    @Bean
    @Order(2)
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
    throws Exception {
    http
    .authorizeHttpRequests((authorize) -> authorize
    .requestMatchers("/login", "/error", "/main.css", "/oauth2/authorize").permitAll()
    .anyRequest().authenticated()
    )
    // Form login handles the redirect to the login page from the
    // authorization server filter chain
    .formLogin((login) -> login.loginPage("/login"));

    return http.build();
    }

    @Bean
    public JWKSource<SecurityContext> jwkSource() {
    KeyPair keyPair = generateRsaKey();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAKey rsaKey = new RSAKey.Builder(publicKey)
    .privateKey(privateKey)
    .keyID(UUID.randomUUID().toString())
    .build();
    JWKSet jwkSet = new JWKSet(rsaKey);
    return new ImmutableJWKSet<>(jwkSet);
    }

    private static KeyPair generateRsaKey() {
    KeyPair keyPair;
    try {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(2048);
    keyPair = keyPairGenerator.generateKeyPair();
    }
    catch (Exception ex) {
    throw new IllegalStateException(ex);
    }
    return keyPair;
    }


    @Bean
    public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
    return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
    }

    @Bean
    public AuthorizationServerSettings authorizationServerSettings() {
    return AuthorizationServerSettings.builder().build();
    }

    }


    Using the OIDC client ts the angular application is using the below configuration

    https://github.com/authts/oidc-client-ts

    openID = {
    authority: 'http://127.0.0.1:5100',
    client_id: 'school-staff',
    redirect_uri: 'http://localhost:4200/auth-callback',
    post_logout_redirect_uri: 'http://localhost:4200',
    response_type: 'code',
    scope: 'openid profile email',
    automaticSilentRenew: true,
    silentRenewUrl: window.location.origin + '/assets/silent-renew.html',
    renewTimeBeforeTokenExpiresInSeconds: 10,
    autoUserInfo: false,
    };


    200 OK network request

    http://127.0.0.1:5100/.well-known/openid-configuration
    Request Method:
    GET
    Status Code:
    200 OK


    The network request 404 not found

    fetch("http://127.0.0.1:5100/oauth2/authorize?client_id=school-staff&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fauth-callback&response_type=code&scope=openid+profile+email&state=2fdb6a7c30694bd595d0eb24998063dc&code_challenge=HUd0-sH1tKkijeXFoJBMEzURFZL37kK1-Ag8oJR50U8&code_challenge_method=S256", {
    "headers": {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "en-US,en;q=0.9",
    "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"macOS\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "cross-site",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
    },
    "referrer": "http://localhost:4200/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "include"
    });


    ERROR on browser

    This application has no explicit mapping for /error, so you are seeing this as a fallback.

    Sat Nov 30 20:29:10 AEDT 2024
    There was an unexpected error (type=Not Found, status=404).
    No static resource oauth2/authorize.
    org.springframework.web.servlet.resource.NoResourceFoundException: No static resource oauth2/authorize.

    Continue reading...

Compartilhe esta Página