Marko
Marko

Reputation: 405

Spring Security + Firebase

I have rest backend wrote on Spring Boot and oauth2 (provided by Google) auto redirect on "/login". I want to make Firebase auth on the backend for mobile beside with oauth for web, like on the following algorithm:

User authorizes on mobile -> User sends request -> Backend gets request -> Backend checks if user openid exists in local database -> Backend returns response or exception page

The following code is my current WebSecurityConfiguration:

@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().mvcMatchers("/","/static/**","/public/**","/assets/**","/api/sensors/**", "/emulator/**").permitAll()
                .anyRequest().authenticated()
                .and().logout().logoutSuccessUrl("/").permitAll()
                .and()
                .csrf().disable();
    }

    @Bean
    public PrincipalExtractor principalExtractor(PersonRepository personRepository) {
        return map -> {
            String id = (String) map.get("sub");
            Person person1 = personRepository.findById(id).orElseGet(() -> {
                Person person = new Person();
                person.setPersonId(id);
                person.getDetails().setFirstName((String) map.get("given_name"));
                person.getDetails().setLastName((String) map.get("family_name"));
                person.getDetails().setEmail((String) map.get("email"));
                person.getDetails().setPictureUrl((String) map.get("picture"));
                person.getSettings().setLocale(new Locale((String) map.get("locale")));

                person.setPersonRole(PersonRole.USER);
                person.setStatus(PersonStatus.NORMAL);
                person.newToken();
                return person;
            });
            return personRepository.save(person1);
        };
    }
}

Upvotes: 3

Views: 1275

Answers (1)

Ayman Arif
Ayman Arif

Reputation: 1699

Add Firebase Configuration Bean of the form:

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.firebase.*;

@Configuration
public class FirebaseConfig {

    @Bean
    public DatabaseReference firebaseDatabse() {
        DatabaseReference firebase = FirebaseDatabase.getInstance().getReference();
        return firebase;
    }

    @Value("${firebase.database.url}")
    private String databaseUrl;

    @Value("${firebase.config.path}")
    private String configPath;

    @PostConstruct
    public void init() {

        /**
         * https://firebase.google.com/docs/server/setup
         * 
         * Create service account , download json
         */
        InputStream inputStream = FirebaseConfig.class.getClassLoader().getResourceAsStream(configPath);

        FirebaseOptions options = new FirebaseOptions.Builder().setServiceAccount(inputStream)
                .setDatabaseUrl(databaseUrl).build();
        FirebaseApp.initializeApp(options);

    }
}

In your application.properties, add

firebase.config.path=Configuration.json
firebase.database.url=<firebase-database-path>

You can download your Configuration.json for your Firebase project by referring to this page

Upvotes: 1

Related Questions