Spring Security + Firebase

3

我有一个基于Spring Boot和由Google提供的oauth2编写的rest后端,可以自动重定向到"/login"。我想在后端为移动设备进行Firebase身份验证,并为Web使用oauth,就像以下算法一样:

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

以下代码是我当前的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);
        };
    }
}

1
请查看 https://github.com/savicprvoslav/Spring-Boot-starter - Ayman Arif
1个回答

1
添加Firebase配置的Bean,形式如下:

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);

    }
}

在你的 application.properties 文件中,添加。
firebase.config.path=Configuration.json
firebase.database.url=<firebase-database-path>

您可以通过参考此页面下载与您的Firebase项目相关联的Configuration.json文件。请注意,不要删除HTML标签。


好的,我有自己的Postgresql数据库,为什么需要连接Firebase数据库? - Marko
我以为你是指认证和数据库都使用Firebase。 - Ayman Arif
抱歉,这是我的错,我在问题中没有清楚地定义它。 - Marko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接