无法定义 Bean 类型并且 Autowired 为 Null [重复]
Posted
技术标签:
【中文标题】无法定义 Bean 类型并且 Autowired 为 Null [重复]【英文标题】:Could Not Defining Bean Type and Autowired is Null [duplicate] 【发布时间】:2020-11-27 14:45:03 【问题描述】:我想在 java 中的身份验证过滤器上使用我的服务时遇到一些问题。这是我的代码
AuthenticationFilter.java
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter
@Autowired
private AuthenticationManager authManager;
@Autowired
private UserService userService;
public AuthenticationFilter(AuthenticationManager authManager)
this.authManager = authManager;
super.setFilterProcessesUrl("/api/login");
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException
User user = new User();
try
Object req = request.getInputStream();
user = new ObjectMapper().readValue(request.getInputStream(), User.class);
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
return authManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUname(), user.getPass()));
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException
String secretKey = "$2y$18$1d12QDK72RFixzHcDqKYjODHA36NAsKm1RIu5V1DsgbPJAxvH0R22";
SecretKey key = Keys.hmacShaKeyFor(secretKey.getBytes());
long expiredDate = 900000000;
String tokenStr = Jwts.builder().signWith(key).setSubject(authResult.getName())
.setExpiration(new Date(new Date().getTime() + expiredDate)).compact();
UserHelper usr = new UserHelper();
User u = new User();
System.out.println(authResult.getName());
String uname = authResult.getName();
System.out.println(uname);
try
u = userService.findByUname(uname, uname);
usr.setUser(u);
usr.setToken(tokenStr);
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
response.setContentType("application/json");
response.getWriter().append("\"token\":\"" + tokenStr + "\"");
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException
response.setStatus(HttpStatus.UNAUTHORIZED.value());
错误信息是
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.lawencon.security.AuthenticationFilter required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
我尝试在构造函数之前使用@Autowired 和@Bean,但注释的位置不允许。 对于这种情况,我想发送响应 jwt 和角色名称
如果我在此代码上使用 bean 注释
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationFilter(AuthenticationManager authManager)
this.authManager = authManager;
super.setFilterProcessesUrl("/api/login");
注释是不允许的位置
【问题讨论】:
AuthManager是接口类,我在AuthFilter的构造函数中调用 你需要为那个类创建一个bean..***.com/questions/49473634/… @ArghyaSadhu 我试过线程中的方法,但是bean注解错误不允许定位 在您尝试创建一个给出disallowed
错误的bean 的位置添加代码
我刚刚在添加 bean 注释时更新了 bean 不允许的位置错误
【参考方案1】:
据说,你的类中应该有默认构造函数
公共 AuthenticationFilter()
【讨论】:
【参考方案2】:@Bean
注释不能用于constructor
,这就是您收到disallowed
错误的原因。
您需要创建一个新类,如下所示
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
return super.authenticationManagerBean();
【讨论】:
以上是关于无法定义 Bean 类型并且 Autowired 为 Null [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Spring @Autowired 是按名称还是按类型注入 bean?