春季启动和谷歌oauth

时间:2014-12-01 17:59:36

标签: google-oauth spring-boot

我已经查看了Google的文档中的Oauth2工作流程,看起来非常直观。使用基本的servlet我理解我是如何处理所有回调的,但是如果我使用oauth和Spring安全模块,那么使用Spring Boot我会对我需要做的事情感到困惑。

我找到了一个教程here,描述了如何使用用户名和密码添加身份验证,但我不确定这是否是满足我需求的最佳选择,因为我主要想限制页面中的内容而不是页面本身。无论如何,假设我每页需要身份验证,我如何使用Oauth设置用户?看一下教程,看来安全模块正在做一些魔术,以便根据内存数据库检查用户名/密码。

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

在上面的示例中,我看到configureGlobal设置要检查的用户名/密码存储区。 Spring boot是否有用户名/密码的硬编码逻辑,这会阻止我将其用于Oauth2?我应该忽略这个安全模块并使用我自己的处理程序直接设置用户吗?

1 个答案:

答案 0 :(得分:0)

如果您只需知道Google认为您的用户是谁,那么您很乐意自己处理一切,而不一定需要Spring Security。还有一个Spring Social Google库here可用于实现回调。