import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public JwtTokenProvider jwtTokenProvider() { return new JwtTokenProvider(); } @Autowired private JwtTokenProvider jwtTokenProvider; @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .addFilterBefore(new JwtTokenFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/api/").authenticated() .anyRequest().permitAll(); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(HttpMethod.OPTIONS, "/"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(12); } }报错信息:Cannot resolve symbol 'JwtTokenFilter',如何创建一个JwtTokenFilter类,让代码不报错
时间: 2023-11-18 10:04:13 浏览: 126
你需要创建一个名为 JwtTokenFilter 的类,代码如下:
```
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class JwtTokenFilter extends OncePerRequestFilter {
private final JwtTokenProvider jwtTokenProvider;
public JwtTokenFilter(JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
String token = jwtTokenProvider.resolveToken(httpServletRequest);
try {
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication auth = jwtTokenProvider.getAuthentication(token);
if (auth != null) {
SecurityContextHolder.getContext().setAuthentication(auth);
}
}
} catch (ExpiredJwtException e) {
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token已过期");
return;
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
```
在上面的代码中,我们创建了一个 JwtTokenFilter 类,它继承了 OncePerRequestFilter 类。JwtTokenFilter 类中包含了我们在之前创建的 JwtTokenProvider 对象,以及 doFilterInternal() 方法,该方法是实际执行 JWT 验证的地方。
在 doFilterInternal() 方法中,我们首先从请求中获取 JWT Token,并调用 JwtTokenProvider 对象中的方法来验证 Token 是否有效。如果 Token 有效,我们使用 JwtTokenProvider 对象的 getAuthentication() 方法来获取用户的身份验证对象,然后将其设置为 SecurityContext 中的认证对象。最后,我们调用 FilterChain 对象的 doFilter() 方法,以便请求可以继续进行处理。
请注意,在上面的代码中,我们使用了 @Component 注解将 JwtTokenFilter 类声明为 Spring 组件。这样,Spring 将负责创建 JwtTokenFilter 对象,并将其注入到 SecurityConfig 类中。
阅读全文