首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java Spring安全-未定义构造函数安全属性用户

Java Spring安全-未定义构造函数安全属性用户
EN

Stack Overflow用户
提问于 2018-10-13 05:13:01
回答 1查看 850关注 0票数 -1

我收到这个错误消息:

错误:构造函数SecurityProperties.User(String,String,boolean,List)未定义

这是我的代码:

代码语言:javascript
复制
package org.launchcode.shopcartsbh.service;

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

import org.launchcode.shopcartsbh.dao.AccountDAO;
import org.launchcode.shopcartsbh.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
private AccountDAO accountDAO;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Account account = accountDAO.findAccount(username);
    System.out.println("Account= " + account);

    if (account == null) {
        throw new UsernameNotFoundException("User " //
                + username + " was not found in the database");
    }

    // EMPLOYEE,MANAGER,..
    String role = account.getUserRole();

    List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();

    // ROLE_EMPLOYEE, ROLE_MANAGER
    GrantedAuthority authority = new SimpleGrantedAuthority(role);

    grantList.add(authority);

    String user = account.getUserName();
    String password = account.getEncrytedPassword();

    boolean enabled = account.isActive();
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;

****   UserDetails userDetails = (UserDetails) new User(user, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantList); 
**** this is where the error is

    return userDetails;
}

}

我在想,我是否可以重写这段代码,使其更具功能性?在过去的几天里,我一直在研究这个问题,但仍然没有解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-13 05:19:44

您导入了错误的User类,应该是

代码语言:javascript
复制
import org.springframework.security.core.userdetails.User

而不是

代码语言:javascript
复制
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52787068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档