我收到错误信息 "Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root causejava.lang.StackOverflowError: null"
我试着调试我的代码,但不明白这个错误的原因。
我在我的AuthService类中的一行得到这个错误。
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
这是我的AuthService类。
@Service
public class AuthService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtProvider jwtProvider;
.........................................
public String login(LoginRequest loginRequest){
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(),
loginRequest.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authenticate);
return jwtProvider.generateToken(authenticate);
}
}
SecurityConfig文件。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable().authorizeRequests()
.antMatchers("/api/auth/**")
.permitAll()
.anyRequest()
.authenticated();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
AuthController类 :
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@Autowired
private AuthService authService;
@PostMapping("/login")
public String login(@RequestBody LoginRequest loginRequest){
return authService.login(loginRequest);
}
}
LoginRequest dto:
public class LoginRequest {
private String username;
private String password;
...getters and setters...
}
用户模型。
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String userName;
@Column
private String password;
@Column
private String email;
...getters and setters...
}
UserRepository Interface:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUserName(String username);
}
JwtProvider:
@Service
public class JwtProvider {
private Key key;
@PostConstruct
public void init(){
key = Keys.secretKeyFor(SignatureAlgorithm.HS512);
}
public String generateToken(Authentication authentication){
User principal = (User) authentication.getPrincipal();
return Jwts.builder()
.setSubject(principal.getUsername())
.signWith(key)
.compact();
}
}
UserDetailsServiceImpl类:
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUserName(username).orElseThrow(()->
new UsernameNotFoundException("No user name found named " + username));
return new org.springframework.security.core.userdetails.User(user.getUserName(),
user.getPassword(),
true,true,true,true,
getAuthorities("ROLE_USER"));
}
private Collection<? extends GrantedAuthority> getAuthorities(String role_user) {
return Collections.singletonList(new SimpleGrantedAuthority(role_user));
}
}
和我的应用程序.属性。
spring.datasource.url=jdbc:mysql://localhost/photoblog?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=...username...
spring.datasource.password=...password...
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
在你的 SecurityConfig
类的方法,你正在重写 authenticationManager()
负责获取AuthenticationManager,但在这个方法中,你是在调用bean的super.authenticationManagerBean()
其中创建了AuthenticationManager Bean。
改变 authenticationManager()
到 authenticationManagerBean()
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}