Spring Boot + Google OAuth2:如何定义用户详细信息服务?

时间:2017-10-13 09:25:35

标签: java spring spring-security-oauth2 google-oauth2

我有简单的春季启动应用程序,我想在其中使用谷歌oauth2身份验证。它工作正常,但我不明白如何设置我自己的userDetailsS​​ervice来为用户设置角色。

我的配置:

@SpringBootApplication
@EnableWebMvc
@EnableOAuth2Sso
public class Application extends WebMvcAutoConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

...和application.properties:

security.oauth2.client.client-id: <client-d>
security.oauth2.client.client-secret: <secret>
security.oauth2.client.access-token-uri: https://www.googleapis.com/oauth2/v3/token
security.oauth2.client.user-authorization-uri: https://accounts.google.com/o/oauth2/auth
security.oauth2.client.client-authentication-scheme: form
security.oauth2.client.scope: profile,email
security.oauth2.resource.user-info-uri: https://www.googleapis.com/userinfo/v2/me
security.oauth2.resource.prefer-token-info: false

那么,我应该在哪里取悦UserDetailsS​​ervice的实现?只是将bean添加到上下文不起作用,它可能应该设置在某个地方,但在哪里? 感谢

1 个答案:

答案 0 :(得分:0)

我不知道这是否是解决此问题的最佳方式,但它确实有效。 我添加了新的安全过滤器,从数据库中检索用户权限。 申请类:

#nav {  
 z-index: 9; 
}

当局过滤:

@Autowired
private UserRepository userRepository;

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    AuthoritiesFilter filter = new AuthoritiesFilter();
    filter.setUserRepository(userRepository);
    registration.setFilter(filter);
    registration.addUrlPatterns("/*");
    registration.setName("authoritiesFilter");
    registration.setOrder(Ordered.LOWEST_PRECEDENCE);
    return registration;
}