java - Java spring boot AuthenticationManager 注入失败
问题描述
我正在尝试实现此处找到的 JWT 身份验证。在这里 运行时出现以下错误:
controllers.SecurityController 中的字段 authenticationManager 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。
controllers.SecurityController 中的字段 authenticationManager 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。
行动:
考虑在你的配置中定义一个 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。
这是我的控制器:
包控制器;
import common.ServiceObjectResponse;
import entity.UserEntity;
import enums.Role;
import exception.CustomException;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.AuthenticationException;
import request.LoginRequest;
import request.RegisterRequest;
import security.JwtTokenProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import services.IUserService;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
@RestController
@Api(tags = "diak", value = "SecurityService")
public class SecurityController
{
@Autowired
private JwtTokenProvider jwtTokenProvider;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private IUserService _userService;
@ApiOperation(value = "login", nickname = "login", tags = "login")
@PostMapping("/api/login")
@ResponseBody
public String Login(@RequestBody @Valid LoginRequest data) throws Exception
{
ServiceObjectResponse<UserEntity> request = _userService.findByCredentials(data.Email, data.Jelszo);
if(!request.getIsSuccess())
{
throw new Exception(request.getMessage());
}
else if(request.getIsSuccess() && request.getObject() == null)
{
throw new Exception("No user found with given credantials!");
}
UserEntity user = request.getObject();
try
{
UsernamePasswordAuthenticationToken authenticationData = new UsernamePasswordAuthenticationToken(data.Email, data.Jelszo);
authenticationManager.authenticate(authenticationData);
List<Role> roles = new ArrayList<>();
roles.add(enums.Role.valueOf(user.Role));
return jwtTokenProvider.createToken(user.UniqID, roles);
}
catch (AuthenticationException e)
{
throw new CustomException("Invalid username/password supplied", HttpStatus.UNPROCESSABLE_ENTITY);
}
}
return jwtTokenProvider.createToken(user.UniqID, roles);
}
}
对java不好。如果有人可以提供帮助。
谢谢
解决方案
推荐阅读
- visual-studio - Visual Studio 2017 Excludefoldersfromdeployment 但包含一些子文件夹
- reactjs - 达到特定值时如何隐藏反应元素?
- perl - 如何在 Mojolicious::Plugin::SslAuth 插件中查看客户端证书?
- c# - 如何在 MVC 的 DropDownList 中将默认值“0”设置为文本“选择”?
- r - 在定义的间隔内绘制函数
- kubernetes - kubernetes:如何从多个文件创建和使用 configmap
- node.js - 为什么 LoopBack 会缩小 JSON 配置文件的内容?
- elasticsearch - 如何在聚合中获取特定的 _source 字段
- python - .obj 到 python 中带有法线的原始顶点转换器
- python - Python:如何将标记列表添加到数据框的新列