Я использую Spring Boot 2.2.6
Итак, когда я пытаюсь автоматически подключить пользовательский фильтр (который записан в Java) внутри моего SecurityConfig.kt
класса, он выдает ошибку java.lang.NoClassDefFoundError
.
вот мой SecurityConfig.kt
:
class SecurityConfig : WebSecurityConfigurerAdapter() {
@Autowired
private val userDetailsService: UserDetailsServiceImpl? = null
@Autowired
private val unauthorizedHandler: AuthEntryPointJwt? = null
@Throws(Exception::class)
override fun configure(auth: AuthenticationManagerBuilder) {
auth.userDetailsService<UserDetailsService?>(userDetailsService).passwordEncoder(passwordEncoder())
}
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
.antMatchers("/api/users/**").permitAll()
.anyRequest().authenticated()
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter::class.java)
}
@Bean
fun authenticationJwtTokenFilter(): AuthTokenFilter? {
return AuthTokenFilter()
}
@Bean
@Throws(Exception::class)
override fun authenticationManagerBean(): AuthenticationManager? {
return super.authenticationManagerBean()
}
@Bean
fun passwordEncoder(): PasswordEncoder? {
return BCryptPasswordEncoder()
}
}
@Slf4j
public class AuthTokenFilter extends OncePerRequestFilter {
@Autowired
private JwtUtils jwtUtils;
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
FilterChain filterChain) throws ServletException, IOException {
try {
String jwt = parseJwt(httpServletRequest);
if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
String email = jwtUtils.getEmailFromJwtToken(jwt);
UserDetails userDetails = userDetailsService.loadUserByUsername(email);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
} catch (Exception e) {
logger.error("Cannot set user authentication: {}", e);
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
private String parseJwt(HttpServletRequest request) {
String headerAuth = request.getHeader("Authorization");
if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
return headerAuth.substring(7, headerAuth.length());
}
return null;
}
}