【发布时间】:2017-05-03 10:18:10
【问题描述】:
自定义 Spring 安全 OAuth2 工作正常,现在想添加 Spring Social 集成(facebook 登录、google 登录等),当用户点击 Facebook 登录时(用户不会提供任何用户名/密码),Facebook 将返回一个access_token,但是这个 access_token 我们不能用来查询我的应用程序 web 服务,要获取我的应用程序 access_token,我们需要传递用户名和密码,并使用 grant_type 作为密码。以下是我的配置文件
AuthorizationServerConfiguration.java
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(
AuthorizationServerSecurityConfigurer oauthServer)
throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.jdbc(dataSource);
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setSupportRefreshToken(true);
tokenServices.setTokenStore(tokenStore());
tokenServices.setAccessTokenValiditySeconds(86400000);
tokenServices.setRefreshTokenValiditySeconds(86400000);
return tokenServices;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints
.tokenServices(tokenServices())
.authenticationManager(authenticationManager);
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
}
ResourceServerConfiguration.java
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private String resourceId = "rest_api";
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// @formatter:off
resources.resourceId(resourceId);
// @formatter:on
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
.antMatchers(HttpMethod.GET, "/**/login").permitAll()
.antMatchers(HttpMethod.GET, "/**/callback").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll();
}
}
最后是 WebSecurityConfigurerAdapter.java
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean()
throws Exception {
return super.authenticationManagerBean();
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
.antMatchers(HttpMethod.GET, "/**/login").permitAll()
.antMatchers(HttpMethod.GET, "/**/callback").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll();
}
}
已阅读 SO 中的不同帖子,但找不到任何可行的示例,请指导我。提前致谢!
【问题讨论】:
-
最近我在我的一个项目中做了类似的实现。我以某种合乎逻辑的方式处理了它。使用从 Facebook/google 收到的 access_token,我检索了用户的 (google/Facebook) 个人资料,然后使用,然后从个人资料信息中检索了用户名,然后使用此用户名从我的应用程序中检索了 access_token。
-
你是怎么配置的?
-
任何帮助将不胜感激
-
您想通过 Facebook 对您的用户进行身份验证,然后为经过身份验证的用户创建您自己的访问令牌以访问您的后端,这对吗?如果是,Spring Boot OAuth tutorial 中有一个名为“托管授权服务器”的部分。看看吧。
-
@yanys 感谢您的评论,是的,但我没有使用弹簧靴。
标签: spring spring-security oauth-2.0 spring-security-oauth2