我已经使用 Spring Security 使用登录名和密码实现了注册和身份验证。我使用本教程做到了这一点。但现在我想使用 Facebook Id 提供注册和身份验证。我将在移动应用程序上获取 Facebook 用户的访问令牌,然后将此访问令牌发送到我的 Spring 服务器(因此 OAuth 不适合),使用 Facebook API 对其进行验证,并获取 Facebook 用户 ID。请注意,Users我的数据库中已经有包含user_id, login, password,fb_id列的表,因此注册有两种变体:要么用户有登录名和密码,要么用户只有 Facebook 帐户,我将使用他的 Facebook id。
最后,我只需要对该用户进行身份验证并返回给他 JWT 令牌。
已经存在的代码。
安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter();
}
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder());
}
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
繁星coding
相关分类