com。***。**。**。security.CustomUserDetail不能转换为com。***。**。**。security.CustomUserDetail

时间:2017-12-20 09:25:44

标签: java spring security spring-security

在获取AuthenticatedUser的同时将org.springframework.security.core.userdetails.User转换为com.***.**.config.security.CustomUserDetail时,我将异常作为"java.lang.ClassCastException"

错误讯息为com.***.**.config.security.CustomUserDetail cannot be cast to com.***.**.config.security.CustomUserDetail

源代码:

public static CustomUserDetail getAuthenticatedUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null) {
            throw new IllegalArgumentException("Authentication Required.");
        }
        Object principal = auth.getPrincipal();
        User user = (User) principal;
        return (CustomUserDetail) user;
    }

user映射到CustomUserDetail

时,return语句出错

CustomUserDetail Class

import java.util.Collection;
import org.springframework.security.core.GrantedAuthority;
import com.***.**.globaluser.domain.GlobalUser;

public class CustomUserDetail extends org.springframework.security.core.userdetails.User {

    private static final long serialVersionUID = 1L;

    private GlobalUser globalUser;

    public CustomUserDetail(GlobalUser user,
            Collection<? extends GrantedAuthority> authorities) {
        super(user.getUsername(), user.getPassword(), authorities);
        this.globalUser = user;
    }

    public CustomUserDetail(GlobalUser user, boolean enabled,
            boolean accountNonExpired, boolean credentialsNonExpired,
            boolean accountNonLocked,
            Collection<? extends GrantedAuthority> authorities) {
        super(user.getUsername(), user.getPassword(), enabled, accountNonExpired,
                credentialsNonExpired, accountNonLocked, authorities);
        this.globalUser = user;
    }

    public GlobalUser getUserLogin() {
        return globalUser;
    }

}

0 个答案:

没有答案