在Spring安全性中如何初始化User - (org.springframework.security.core.userdetails.User)

时间:2014-03-11 03:53:58

标签: java spring

我只是在一个服务类中初始化User(org.springframework.security.core.userdetails.User),它实现了UserDetailsS​​ervice(org.springframework.security.core.userdetails),

User user=new User(null, null, false, false, false, false, null);

但显示错误

java.lang.IllegalArgumentException:无法将null值或空值传递给构造函数

然后如何初始化此User对象?

方式

public UserDetails loadUserByUsernameAndUsertype(String username, String type) throws
        UsernameNotFoundException {

    User user = new User(null, null, false, false, false, false, null);

    try {

        if (type.equals("normalUser")) {
            com.booktheservice.entity.NormalUser normalUser = normalUserService.readSpecific("name", username);

            boolean enabled = true;
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;
            user = new User(
                    normalUser.getName(),
                    normalUser.getPassword().toLowerCase(),
                    enabled,
                    accountNonExpired,
                    credentialsNonExpired,
                    accountNonLocked,
                    getAuthorities("normalUser", false));


        }
        if (type.equals("adminUser")) {
            com.booktheservice.entity.AdminUser adminUser = adminUserService.readSpecific("name", username);

            boolean enabled = true;
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;

            user = new User(
                    adminUser.getName(),
                    adminUser.getPassword().toLowerCase(),
                    enabled,
                    accountNonExpired,
                    credentialsNonExpired,
                    accountNonLocked,
                    getAuthorities("adminUser", false));

        }

        if (type.equals("serviceLister")) {
            com.booktheservice.entity.ServiceLister serviceLister = serviceListerService.readSpecific("name", username);

            boolean enabled = true;
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;

            user = new User(
                    serviceLister.getName(),
                    serviceLister.getPassword().toLowerCase(),
                    enabled,
                    accountNonExpired,
                    credentialsNonExpired,
                    accountNonLocked,
                    getAuthorities("serviceLister", false));

        }
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

2 个答案:

答案 0 :(得分:0)

这是你试图调用的构造函数

public User(String username, String password, boolean enabled, boolean accountNonExpired,
            boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {

        if (((username == null) || "".equals(username)) || (password == null)) {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }

        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }

您无法将null传递给某些字段。

答案 1 :(得分:0)

我通过初始化User(org.springframework.security.core.userdetails.User)解决了这个问题:

用户user = null;