spring security first login fail,and second login success

孤街浪徒 提交于 2020-04-15 15:54:10

问题


i have a problem, when i first login i will get

{"timestamp":1481719982036,"status":999,"error":"None","message":"No message available"} but second is ok.And if i clear the cache.I fail,too. this is primary code:

@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();
    }

}

and i don't know what happened.


回答1:


Check whether you have all the following folders

"/static/**", "/js/**", "/css/**", "/img/**", "/json/**"

The Exception

{"timestamp":1481719982036,"status":999,"error":"None","message":"No message available"}

Is Thrown when a Folder doesn't exist but excluded.




回答2:


Had the same issue. Adding this to application.properties solved the problem

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration



回答3:


A similar scenario "first login fail,and second login success but there is an exception" with spring security happens to me. Not sure if it is the same as your case. In my case I found that principle object is null. My case also use Zuul to

I have found that in my custom fail login handler. I redirect to my localhost .

.failureHandler(customAuthenticationFailureHandler())

I have changed from

response.sendRedirect("localhost"+ "/login/fail/" );

to

response.sendRedirect("Zuul-localhost"+ "/login/fail/" );

and my problem is solved.




回答4:


I had the same case.

Only one change was resolved.

.defaultSuccessUrl("/index", true);

see also : Spring security login always lands in an error page with no message 123's answer




回答5:


Also check if there are any resources that are not found in your project but declared on login page. You should remove them and fix /error page like described here Spring security redirects to page with status code 999



来源:https://stackoverflow.com/questions/41144028/spring-security-first-login-fail-and-second-login-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!