Spring 当';在bean中';使用的登录方法

Spring 当';在bean中';使用的登录方法,spring,spring-security,spring-3,spring-social,Spring,Spring Security,Spring 3,Spring Social,几天来,我一直致力于将Facebook与我们的应用程序集成。我已经成功地建立了连接,现在在Facebook登录后,我将用户复制到我们的数据库中,然后我想在上下文中使用我们的内部主体 对于Spring安全登录,我们的类实现了UserDetailsService,从而重载了身份验证管理器 当有人使用facebook登录时,他有一些他不知道的抽象凭证。 我在我的Facebook登录控制器中使用此方法让他登录: Authentication auth = new UsernamePasswordAut

几天来,我一直致力于将Facebook与我们的应用程序集成。我已经成功地建立了连接,现在在Facebook登录后,我将用户复制到我们的数据库中,然后我想在上下文中使用我们的内部主体


对于Spring安全登录,我们的类实现了
UserDetailsService
,从而重载了身份验证管理器

当有人使用facebook登录时,他有一些他不知道的抽象凭证。 我在我的Facebook登录控制器中使用此方法让他登录:

Authentication auth = new UsernamePasswordAuthenticationToken(
    profile.getId(), new Md5PasswordEncoder().encodePassword(
    profile.getEmail() + profile.getId(), null),
    (Collection<? extends GrantedAuthority>) getAuthorities(profile.getId()));
Authentication auth=new UsernamePasswordAuthenticationToken(
profile.getId(),新的Md5PasswordEncoder().encodePassword(
profile.getEmail()+profile.getId(),null),

(收集您需要按照的末尾所述设置SignInAdapter。在
SignInAdapter.signIn(…)
方法中,您可以从DB加载您的用户对象,然后准备身份验证对象并将其注入到安全上下文持有者中。

我起初并没有真正获得“准备身份验证对象”,但这非常简单; 我发布这个例子是为了将来的澄清,也许有人会需要它:)

公共类FacebookAuthenticationToken扩展了AbstractAuthenticationToken
{   
私有最终org.springframework.security.core.userdetails.userdetails主体;
私有字符串凭证;
公共FacebookAuthenticationToken(org.springframework.security.core.userdetails.userdetails详细信息,FacebookProfile配置文件,集合
public class FacebookAuthenticationToken extends AbstractAuthenticationToken 
{   
private final org.springframework.security.core.userdetails.UserDetails principal;
private String credentials;

public FacebookAuthenticationToken(org.springframework.security.core.userdetails.UserDetails details,FacebookProfile profile, Collection<? extends GrantedAuthority> authorities){
    super(authorities);
    this.principal = details;
    this.credentials = new Md5PasswordEncoder().encodePassword(profile.getEmail()+profile.getId(), null);
    super.setAuthenticated(true); // must use super, as we override
}

private static final long serialVersionUID = -7545290433068513777L;

public Object getCredentials() {
    return this.credentials;
}

public Object getPrincipal() {
    return this.principal;
}

}