0

Given a hypothetical application which uses Spring Session to store session information and:

  • There are more than one way of initiating a session and authenticating, i.e. different endpoints that can be hit depending on how the user is "logging in".
  • All of the endpoints a user can use for authentication result in a session attribute called "authenticated" being set to true.

Is it possible to configure Spring Security to determine whether a request is authenticated based on the presence and truthiness of that session variable?

The security filter chain might look something like this

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http.cors()
                .authorizeHttpRequests(
                        requests -> {
                            requests.antMatchers("/auth/login").permitAll();
                            requests.antMatchers("/auth/sso-login").permitAll();
                            requests.antMatchers("/auth/developer-login").permitAll();
                            requests.anyRequest().authenticated();
                        })
                .build();
    }

The idea would be that so long as a user has hit any of the login endpoints correctly, the application flags the session as authenticated, allowing the user to access other endpoints as an authenticated user.

Or is there a more integrated solution that allows the application to designate a particular session as authenticated, not using traditional mechanisms like BasicAuth? Specifically in the case of a developer utility being able to mock a login as a mocked user without providing credentials. Simply hitting the endpoint (in the environments where it is available) triggering a fully authenticated session as far as Spring Security is concerned.

BrandonLenz
  • 103
  • 9
  • The following answer gave me a good way to implement a forced/manual developer login feature: https://stackoverflow.com/a/11314388/7700346. For the other login methods, they can use actual spring security authentication mechanisms – BrandonLenz Oct 07 '22 at 01:25

0 Answers0