简体   繁体   中英

spring security first login fail,and second login success

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.

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.

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM