![](/img/trans.png)
[英]Java Spring Security login success at first and fail at second attempt
[英]spring security first login fail,and second login success
我有一個問題,當我第一次登錄時,我會得到
{"timestamp":1481719982036,"status":999,"error":"None","message":"No message available"} 但第二個沒問題。如果我清除緩存。我也失敗了。 這是主要代碼:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password,TRUE from authority where username = ?")
.authoritiesByUsernameQuery("select username,role from authority where username = ?")
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.loginProcessingUrl("/login/check").permitAll()
.defaultSuccessUrl("/index").failureUrl("/login?failed=true")
.permitAll()
.and()
.logout().logoutSuccessUrl("/login").logoutUrl("/login?logout")
.and().csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/static/**", "/js/**", "/css/**", "/img/**", "/json/**");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
我不知道發生了什么。
檢查您是否擁有以下所有文件夾
"/static/**", "/js/**", "/css/**", "/img/**", "/json/**"
例外
{"timestamp":1481719982036,"status":999,"error":"None","message":"No message available"}
當文件夾不存在但被排除時拋出。
有同樣的問題。 將此添加到 application.properties 解決了問題
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
類似的場景“第一次登錄失敗,第二次登錄成功,但有一個例外”與 spring security 發生在我身上。 不知道是不是和你的情況一樣。 就我而言,我發現原則對象為空。 我的情況也使用 Zuul
我在我的自定義失敗登錄處理程序中發現了這一點。 我重定向到我的 localhost 。
.failureHandler(customAuthenticationFailureHandler())
我已經從
response.sendRedirect("localhost"+ "/login/fail/" );
到
response.sendRedirect("Zuul-localhost"+ "/login/fail/" );
我的問題解決了。
我有同樣的情況。
僅解決了一處更改。
.defaultSuccessUrl("/index", true);
另請參閱: Spring security login 總是登陸錯誤頁面,沒有消息123 的回答
還要檢查是否有在您的項目中找不到但在登錄頁面上聲明的資源。 您應該刪除它們並修復/error
頁面,如此處所述Spring security redirects to page with status code 999
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.