Hallo,
neuer Tag, neues Pech 😬 Beim Start meiner Spring-App erhalte ich folgende Meldung:
Hierzu habe ich eine Klasse UserServiceImpl implementiert
Das zugehörige Interface UserService sieht wie folgt aus:
Ich habe doch die Klasse AuthenticationManager importiert und die Annotation @Autowired angegeben. Was mache ich falsch?
neuer Tag, neues Pech 😬 Beim Start meiner Spring-App erhalte ich folgende Meldung:
Code:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field authenaticationManager in com.inn.cafe.service.impl.UserServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
Hierzu habe ich eine Klasse UserServiceImpl implementiert
Java:
package com.inn.cafe.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import com.inn.cafe.JWT.CustomerUserDetailsService;
import com.inn.cafe.JWT.JwtFilter;
import com.inn.cafe.JWT.JwtUtil;
import com.inn.cafe.POJO.User;
import com.inn.cafe.constants.CafeConstants;
import com.inn.cafe.dao.UserDao;
import com.inn.cafe.service.UserService;
import com.inn.cafe.util.CafeUtils;
import com.inn.cafe.wrapper.UserWrapper;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Autowired(required = true)
AuthenticationManager authenaticationManager;
@Autowired
CustomerUserDetailsService customerUserDetailsService;
@Autowired
JwtUtil jwtUtil;
@Autowired
JwtFilter jwtFilter;
@Override
public ResponseEntity<String> signup(Map<String, String> requestMap) {
try {
if (validateSignupMap(requestMap)) {
User user = userDao.findByEmailId(requestMap.get("email"));
if (Objects.isNull(user)) {
userDao.save(getUserFromMap(requestMap));
return CafeUtils.getResponseEntity(CafeConstants.SUCCESSFULLY_REGISTERED, HttpStatus.OK);
} else
return CafeUtils.getResponseEntity(CafeConstants.EMAIL_ALREADY_EXISTS, HttpStatus.BAD_REQUEST);
} else
return CafeUtils.getResponseEntity(CafeConstants.INVALID_DATA, HttpStatus.BAD_REQUEST);
} catch (Exception e) {
e.printStackTrace();
}
return CafeUtils.getResponseEntity(CafeConstants.SOMETHING_WENT_WRONG, HttpStatus.INTERNAL_SERVER_ERROR);
}
@Override
public ResponseEntity<String> login(Map<String, String> requestMap) {
try {
Authentication auth = authenaticationManager.authenticate(
new UsernamePasswordAuthenticationToken(requestMap.get("email"), requestMap.get("password")));
if (auth.isAuthenticated()) {
if (customerUserDetailsService.getUserDetail().getStatus().equalsIgnoreCase("true")) {
return new ResponseEntity<String>(
"{\token\":\""
+ jwtUtil.generateToken(customerUserDetailsService.getUserDetail().getEmail(),
customerUserDetailsService.getUserDetail().getRole())
+ "\"}",
HttpStatus.OK);
} else {
/** TODO Auf {@link CafeConstants} umstellen. */
return new ResponseEntity<String>("{\"message\":\"" + CafeConstants.WAIT_FOR_ADMIN_APPROVAL + "\"}",
HttpStatus.BAD_REQUEST);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<String>("{\"message\":\"" + CafeConstants.BAD_CREDENTIALS + "\"}",
HttpStatus.BAD_REQUEST);
}
@Override
public ResponseEntity<String> update(Map<String, String> requestMap) {
try {
if (jwtFilter.isUser()) {
Optional<User> optional = userDao.findById(Integer.parseInt(requestMap.get("id")));
if (!optional.isPresent()) {
userDao.updateStatus(requestMap.get("status"), Integer.parseInt(requestMap.get("id")));
return CafeUtils.getResponseEntity("User Status Updated Successfully", HttpStatus.OK);
} else
return CafeUtils.getResponseEntity("User Id Doesn't Exist", HttpStatus.OK);
} else
return CafeUtils.getResponseEntity(CafeConstants.UNAUTHORIZED_ACCESS, HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
e.printStackTrace();
}
return CafeUtils.getResponseEntity(CafeConstants.SOMETHING_WENT_WRONG, HttpStatus.INTERNAL_SERVER_ERROR);
}
private boolean validateSignupMap(Map<String, String> requestMap) {
if (requestMap.containsKey("name") && requestMap.containsKey("contactNumber") && requestMap.containsKey("email")
&& requestMap.containsKey("password"))
return true;
else
return false;
}
private User getUserFromMap(Map<String, String> requestMap) {
User user = new User();
user.setName(requestMap.get("name"));
user.setContactNumber(requestMap.get("contactNumber"));
user.setEmail(requestMap.get("email"));
user.setPassword(requestMap.get("password"));
user.setStatus(requestMap.get("status"));
user.setRole(requestMap.get("role"));
return user;
}
@Override
public ResponseEntity<List<UserWrapper>> getAllUser() {
try {
if (jwtFilter.isAdmin())
return new ResponseEntity<List<UserWrapper>>(userDao.getAllUser(), HttpStatus.OK);
else
return new ResponseEntity<List<UserWrapper>>(new ArrayList<>(), HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Das zugehörige Interface UserService sieht wie folgt aus:
Java:
package com.inn.cafe.service;
import java.util.List;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import com.inn.cafe.wrapper.UserWrapper;
public interface UserService {
ResponseEntity<String> signup(Map<String, String> requestMap);
ResponseEntity<String> login(Map<String, String> requestMap);
ResponseEntity<List<UserWrapper>> getAllUser();
ResponseEntity<String> update(Map<String, String> requestMap);
}
Ich habe doch die Klasse AuthenticationManager importiert und die Annotation @Autowired angegeben. Was mache ich falsch?