google日历-API with java spring Boot boot

jexiocij  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(124)

我正在开发一个Java Springboot Web应用程序。在其中我有多个用户登录与地址邮件和密码。登录是OK的。

@Override
protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/error.xhtml").permitAll().antMatchers("/error-404.xhtml").permitAll()
            .anyRequest().authenticated().accessDeniedPage("/accessDenied.xhtml");
    ;
    // login
    http.formLogin().loginPage("/login").permitAll().failureUrl("/login?error=1");
    // logout
    http.logout().logoutSuccessUrl("/login").invalidateHttpSession(true).deleteCookies("JSESSIONID");

    http.formLogin().defaultSuccessUrl("/dashboard", true);

    http.csrf().disable();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

我不想用谷歌登录。我需要将他们的谷歌日历应用程序帐户,所以他们不能创建事件和会议使用他们的Gmail ID。
有没有什么方法,我可以通过必须得到用户的同意,只有一次在设置中集成他们的谷歌帐户到应用程序.

wfsdck30

wfsdck301#

你应该看看official sample

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
    .setAccessType("offline")
    .build();

如果FileDataStoreFactory配置正确,它将存储每个用户的凭据,那么您只需请求访问一次。
更新:
评论中的问题
1.how正确配置FileDataStoreFactory存储每个用户的凭据,并使用连接的用户的凭据?
检查下面的示例.authorize("user");将表示用户并存储该用户的信用。
1.如何切断一个帐户?
通过调用撤销端点或删除它们上面存储的文件。它将强制您的应用再次请求授权。

private static Credential authorize() throws Exception
    {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        // load client secrets
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(BigQueryConsumer.class.getResourceAsStream("/secret.json")));

        // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
        FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));

        // set up authorization code flow
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, JSON_FACTORY, clientSecrets,
                SCOPES).setDataStoreFactory(fileDataStoreFactory)
                .build();
        // authorize
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

相关问题