Skip to content

Wired behaviours between Cors and Pac4jModule #3509

@jonaskahn

Description

@jonaskahn

In my project, I setup both CORS and Pac4J like below:

    install(
        Pac4jModule().client(
            "/api/secure/*",
            CheckHttpMethodAuthorizer(
                HttpConstants.HTTP_METHOD.GET,
                HttpConstants.HTTP_METHOD.PUT,
                HttpConstants.HTTP_METHOD.POST,
                HttpConstants.HTTP_METHOD.DELETE,
                HttpConstants.HTTP_METHOD.PATCH
            )
        ) {
            HeaderClient(
                "Authorization",
                "Bearer ",
                AdvancedJwtAuthenticator(
                    require(JedisPooled::class.java),
                    SecretSignatureConfiguration(it.getString("jwt.salt"))
                )
            )
        }
    )

    val corsOption = Cors()
    corsOption.setOrigin("*")
    corsOption.setUseCredentials(true)
    corsOption.setHeaders("X-Requested-With", "Content-Type", "Accept", "Origin", "Authorization")
    corsOption.setMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD")
    corsOption.setMaxAge(Duration.ofMinutes(60))
    use(CorsHandler(corsOption))

AdvancedJwtAuthenticator: Just an extended class to store JWT ID to redis before create user profile.

  • If I enabled CORS, when I send method OPTIONS on protected API it will throw 401, the code will be execute
                    if (startAuthentication(context, sessionStore, currentClients)) {
                        LOGGER.debug("Starting authentication");
                        saveRequestedUrl(context, sessionStore, currentClients, config.getClients().getAjaxRequestResolver());
                        action = redirectToIdentityProvider(context, sessionStore, currentClients);
                    } else {
                       // Line 152: DefaultSecurityLogic.java
                        LOGGER.debug("unauthorized");
                        action = unauthorized(context, sessionStore, currentClients);
                    }
  • If I turn off Cors, this line of code will be ignored.

It will lead to browser can never send Options method to complete API calling. Could you explain this ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions