[英]JWT Token not generated after successful Authentication in Spring Boot
我創建了兩個過濾器來為每個請求運行 JwtUsernameAndPasswordFilter 和 JwtTokenVerifier。 我以前使用過這兩個過濾器,它們已經奏效了。 我認為主要問題在於我的 Spring 安全配置。 當我調試這兩個過濾器時,只有 JwtTokenVerified 被識別並且 JwtUsernameAndPasswordFilter 根本不會被調用。 當我使用 application/json 內容類型從 PostMan 發出請求時,服務器會給我一個錯誤:
class path resource [templates/logIn.html] cannot be opened because it does not exist
/登錄 Controller
@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;
}
github 回購
更新:
通過在客戶端調用“/login”而不是“/logIn”使其工作,因為顯然即使我 add.loginPage("/logIn") AND.logInProcessingUrl("/logIn")。看起來 spring 仍然沒有通過過濾器鏈時識別我的自定義登錄 controller。 如果您知道更好的解決方案,請隨時在下面發表評論
當我調試這兩個過濾器時,只有 JwtTokenVerified 被識別並且 JwtUsernameAndPasswordFilter 根本不會被調用。
正在調用JwtUsernameAndPasswordFilter
。 為了證明這一點,要么在您的過濾器中放置一個調試語句,或者更好,在您的SecurityConfig.java
中,啟用Spring Security debugger
,如下所示:
@EnableWebSecurity(debug=true)
你會看到這個:
Security filter chain: [
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
HeaderWriterFilter
LogoutFilter
JwtUsernameAndPasswordFilter // This is your filter
UsernamePasswordAuthenticationFilter
JwtTokenVerifier // This is your filter
DefaultLoginPageGeneratingFilter
DefaultLogoutPageGeneratingFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
]
當我使用 application/json 內容類型從 PostMan 發出請求時,服務器會給我一個錯誤:
class 路徑資源 [templates/logIn.html] 無法打開,因為它不存在
這是因為在您的/templates
文件夾中,沒有logIn.html
。
通過在客戶端調用“/login”而不是“/logIn”使其工作,因為顯然即使我 add.loginPage("/logIn") AND.logInProcessingUrl("/logIn")。看起來 spring 仍然沒有通過過濾器鏈時識別我的自定義登錄 controller。 如果您知道更好的解決方案,請隨時在下面發表評論
/login
之所以有效,是因為您沒有在 SecurityConfig.java、Spring 中定義自定義登錄頁面,因此安全性會將您重定向到其默認登錄頁面。
如果您不想要該功能,可以按如下方式自定義loginPage
:
.and()
.formLogin().loginPage("/customizeMe") // Your custom login page here
.failureUrl("/")
.successForwardUrl("/accountPage");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.