【发布时间】:2020-10-24 14:11:03
【问题描述】:
我为每个请求创建了两个过滤器来运行 JwtUsernameAndPasswordFilter 和 JwtTokenVerifier。我以前使用过这两个过滤器,它们已经奏效了。我认为主要问题在于我的 Spring Security Config。当我调试这两个过滤器时,只有 JwtTokenVerified 被识别并且 JwtUsernameAndPasswordFilter 根本不会被调用。当我使用 application/json 内容类型从 PostMan 发出请求时,服务器会给我一个错误:
class path resource [templates/logIn.html] cannot be opened because it does not exist
/登录控制器
@RequestMapping(value="/logIn",method = {RequestMethod.POST,RequestMethod.GET})
public void login(){
}
Spring 安全配置
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@Configuration
@Builder
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private SecureUserDaoService secureUserDaoService;
private JwtConfig jwtConfig;
private SecretKey secretKey;
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilter(new JwtUsernameAndPasswordFilter(authenticationManager(), jwtConfig, secretKey))
.addFilterAfter(new JwtTokenVerifier(secretKey, jwtConfig), JwtUsernameAndPasswordFilter.class)
.authorizeRequests()
.antMatchers("/accountPage", "/accountSettings").authenticated()
.antMatchers("/", "/signUp", "/logIn").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.failureUrl("/")
.successForwardUrl("/accountPage");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(){
DaoAuthenticationProvider provider =
new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(secureUserDaoService);
return provider;
}
githubrepo
更新:
通过在客户端调用“/login”而不是“/logIn”使其工作,因为显然即使我添加了 .loginPage("/logIn") AND .logInProcessingUrl("/logIn")。看起来像春天通过过滤器链时仍然无法识别我的自定义登录控制器。如果您知道更好的解决方案,请随时在下面发表评论
【问题讨论】:
-
好的,所以我在 formLogIn 设置中添加了一个 .loginPage("/logIn") 但是当我使用请求标头 Content-Type: application/json 发出请求并将用户名和密码参数存储在正文为 JSON。它给了我一个找不到用户的错误,因为 DaoAuthenticationProvider.class 中的 retreiveUser() 方法没有给出用户名参数。当我再次检查错误堆栈时,我发现未调用 UsernameAndPassword 过滤器
-
@KavithakaranKanapathippillai 我编辑了我的帖子并找到了某种解决方案。虽然它不是我想要找到的那种。
标签: java spring spring-mvc spring-security jwt