首页 > 解决方案 > 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不好。如果有人可以提供帮助。

谢谢

标签: javaspring-bootjwt

解决方案


推荐阅读