`
Wind_ZhongGang
  • 浏览: 260027 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Security 3多用户登录实现之四 用户凭证

 
阅读更多

 

      前讲讲到AuthenticationFilter会拦截我们的登录表单提交信息并根据相应的信息构造出对应的用户凭证,这里的用户凭证将会贯穿整个Spring Security安全验证过程,如果验证用户凭证成功,我们可以为相应的用户凭证分配相应的权限,并将用户导向登录成功的界面。来看看这里的多种类型用户登录凭证的实现吧,这里主要实现了两种用户凭证,一种是前台用户登录凭证,一种是后台用户登录凭证。

 

 

package com.template.security.authentication.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 12-11-4
 * Time: 下午11:23
 */
public class ForendAuthenticationToken extends AbstractAuthenticationToken {
    private String email;
    private String phone;

    public ForendAuthenticationToken(String email, String phone, List<GrantedAuthority> grantedAuthorities) {
        super(grantedAuthorities);
        this.email = email;
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }
}

 

 

package com.template.security.authentication.token;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 12-11-4
 * Time: 下午11:24
 */
public class BackendAuthenticationToken extends UsernamePasswordAuthenticationToken {
    private String captcha;

    public BackendAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, String captcha) {
        super(principal, credentials, authorities);
        this.captcha = captcha;
    }

    public String getCaptcha() {
        return captcha;
    }
}

 

    为了标志我们创建的类为一个用户凭证并且能够被Spring Security识别,我们需要实现Authentication接口,因为Spring Security为我们提供了一个抽象的凭证,所以这里我的前台凭证就继承了这样一个抽象的凭证类,名为AbstractAuthenticationToken, 根据前台表单的提交信息有邮件地址和电话号码,所以实现的ForendAuthenticationToken包含这样两个属性,而Spring Security为我们提供了常见的用户名和密码的登录凭证实现,我的后台登录表单信息只是多了一个验证码,所以我直接继承了UsernamePasswordAuthenticationToken这样一个类。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics