扩展SPRINGSECURITY的User类 提供更丰富的UserDetail信息

1

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springside.examples.showcase.common.entity.Role;

/**
 * 扩展SpringSecurity的WebAuthenticationDetails类, 增加登录时间属性和角色属性.
 *
 * @author calvin
 */
public class OperatorDetails extends User {
    private static final long serialVersionUID = 1919464185097508773L;

    private Date loginTime;

    private List roleList;

    public OperatorDetails(String username, String password, boolean enabled, boolean accountNonExpired,
            boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities)
            throws IllegalArgumentException {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public List getRoleList() {
        return roleList;
    }

    public void setRoleList(List roleList) {
        this.roleList = roleList;
    }
}

 

2

 

import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.showcase.common.entity.Role;
import org.springside.examples.showcase.common.entity.User;
import org.springside.examples.showcase.common.service.AccountManager;

import com.google.common.collect.Sets;

/**
 * 实现SpringSecurity的UserDetailsService接口,实现获取用户Detail信息的回调函数.
 *
 * 演示扩展SpringSecurity的User类加入loginTime信息.
 *
 * @author calvin
 */
@Transactional(readOnly = true)
public class UserDetailsServiceImpl implements UserDetailsService {

    private AccountManager accountManager;

    /**
     * 获取用户Detail信息的回调函数.
     */
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {

        User user = accountManager.findUserByLoginName(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户" + username + " 不存在");
        }

        Set grantedAuths = obtainGrantedAuthorities(user);

        //showcase的User类中无以下属性,暂时全部设为true.
        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        OperatorDetails userDetails = new OperatorDetails(user.getLoginName(), user.getShaPassword(), enabled,
                accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
        //加入登录时间信息和用户角色
        userDetails.setLoginTime(new Date());
        userDetails.setRoleList(user.getRoleList());
        return userDetails;
    }

    /**
     * 获得用户所有角色的权限.
     */
    private Set obtainGrantedAuthorities(User user) {
        Set authSet = Sets.newHashSet();
        for (Role role : user.getRoleList()) {
            authSet.add(new GrantedAuthorityImpl("ROLE_" + role.getName()));
        }
        return authSet;
    }

    @Autowired
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }
}

你可能感兴趣的:(扩展SPRINGSECURITY的User类 提供更丰富的UserDetail信息)