У меня есть конфигурация
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final JwtTokenProvider jwtTokenProvider;
private static final String ADMIN_ENDPOINT = "/api/v1/admin/**";
private static final String LOGIN_ENDPOINT = "/api/v1/auth/login";
@Autowired
public SecurityConfig(JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(LOGIN_ENDPOINT).permitAll()
.antMatchers(ADMIN_ENDPOINT).hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.apply(new JwtConfigurer(jwtTokenProvider));
}
}
И контроллер остальных
@RestController
@RequestMapping(value = "/api/v1/auth/")
public class AuthenticationRestControllerV1 {
private final AuthenticationManager authenticationManager;
private final JwtTokenProvider jwtTokenProvider;
private final UserService userService;
@Autowired
public AuthenticationRestControllerV1(AuthenticationManager authenticationManager, JwtTokenProvider jwtTokenProvider, UserService userService) {
this.authenticationManager = authenticationManager;
this.jwtTokenProvider = jwtTokenProvider;
this.userService = userService;
}
@PostMapping("login")
public ResponseEntity login(@RequestBody AuthenticationRequestDto requestDto) {
try {
String username = requestDto.getUsername();
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, requestDto.getPassword()));
User user = userService.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User with username: " + username + " not found");
}
String token = jwtTokenProvider.createToken(username, user.getRoles());
Map<Object, Object> response = new HashMap<>();
response.put("username", username);
response.put("token", token);
return ResponseEntity.ok(response);
} catch (AuthenticationException e) {
throw new BadCredentialsException("Invalid username or password");
}
}
}
Если я отправляю запрос POST через POSTMAN, возникает ошибка
java .lang.StackOverflowError: null at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection (AopUtils. java: 344) ~ [spring-aop-5.2.5.RELEASE.jar: 5.2. 5.RELEASE] в org.springframework.aop.framework.JdkDynamicAopProxy.invoke (JdkDynamicAopProxy. java: 205) ~ [spring-aop-5.2.5.RELEASE.jar: 5.2.5.RELEASE] в com.sun. proxy. $ Proxy134.authenticate (неизвестный источник) ~ [na: na] в org.springframework.security.authentication.ProviderManager.authenticate (ProviderManager. java: 195) ~ [spring-security-core-5.2.2.RELEASE .jar: 5.2.2.RELEASE] в org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter $ AuthenticationManagerDelegator.authenticate (WebSecurityConfigurerAdapter. java: 501) ~ [spring-security-security RELEASE.jar: 5.2.2.RELEASE] в jdk.internal.reflect.GeneratedMethodAccessor52. invoke (Неизвестный источник) ~ [na: na] в java .base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl. java: 43) ~ [na: na] в java .base / java .lang.reflect.Method.invoke (Метод. java: 567) ~ [na: na]
Проблема находится в контроллере, в этой строке:
authenticationManager.authenticate(new
UsernamePasswordAuthenticationToken(username,requestDto.getPassword()));
Как решить эту проблему?