在尝试验证我的登录时,我收到错误“处理程序调度失败;嵌套异常是 java.lang.***Error”
Posted
技术标签:
【中文标题】在尝试验证我的登录时,我收到错误“处理程序调度失败;嵌套异常是 java.lang.***Error”【英文标题】:While try to authenticate my login I am getting error "Handler dispatch failed; nested exception is java.lang.***Error " 【发布时间】:2020-08-19 00:20:24 【问题描述】:我收到错误 "Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [处理程序调度失败;嵌套异常是 java.lang.***Error],根本原因 java.lang.***Error: 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);
安全配置文件:
@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);
登录请求 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 接口:
@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));
还有我的 application.properties :
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
【问题讨论】:
【参考方案1】:在您的 SecurityConfig
类中,您将覆盖负责获取 AuthenticationManager 的方法 authenticationManager()
,但在此方法中,您正在调用创建 AuthenticationManager bean 的 beansuper.authenticationManagerBean()
。
将authenticationManager()
更改为authenticationManagerBean()
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
return super.authenticationManagerBean();
https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html#authenticationManager--
https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html#authenticationManagerBean--
【讨论】:
你真是个天才兄弟!谢谢!以上是关于在尝试验证我的登录时,我收到错误“处理程序调度失败;嵌套异常是 java.lang.***Error”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在我的网络应用上使用 Google 和登录时,不断收到“超出未经身份验证使用的每日限制。继续使用需要注册”