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

Posted

技术标签:

【中文标题】Java Spring Security - 构造函数安全属性用户未定义【英文标题】:Java Spring Security - The constructor Security Properties User is not defined 【发布时间】:2019-03-18 03:07:35 【问题描述】:

我收到此错误消息:

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

这是我的代码:

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;



我想知道他们是否可以通过某种方式重写它以使其更实用?过去几天我一直在研究这个问题,但仍然没有找到解决方案。

【问题讨论】:

【参考方案1】:

你输入了错误的Userclass,应该是

import org.springframework.security.core.userdetails.User

而不是

import org.springframework.boot.autoconfigure.security.SecurityProperties.User;

【讨论】:

以上是关于Java Spring Security - 构造函数安全属性用户未定义的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Security Oauth2集成

串理spring security认证流程源码

Spring Security Java Config Preview--官方

如何从 base64 编码的字符串构造 java.security.PublicKey 对象?

spring security基本知识

java.security.Principal - 在 HttpServletRequest 和 Spring Security 中创建