Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java com.*.*.**。安全CustomUserDetail无法强制转换为com。***。安全CustomUserDetail

当类在获取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时返回语句出错

CustomUserDetail类

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) 个答案