简体   繁体   English

spring security 第一次登录失败,第二次登录成功

[英]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. {"timestamp":1481719982036,"status":999,"error":"None","message":"No message available"} 但第二个没问题。如果我清除缓存。我也失败了。 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将此添加到 application.properties 解决了问题

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.类似的场景“第一次登录失败,第二次登录成功,但有一个例外”与 spring security 发生在我身上。 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我的情况也使用 Zuul

I have found that in my custom fail login handler.我在我的自定义失败登录处理程序中发现了这一点。 I redirect to my localhost .我重定向到我的 localhost 。

.failureHandler(customAuthenticationFailureHandler()) .failureHandler(customAuthenticationFailureHandler())

I have changed from我已经从

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

to

response.sendRedirect("Zuul-localhost"+ "/login/fail/" ); 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另请参阅: Spring security login 总是登陆错误页面,没有消息123 的回答

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您应该删除它们并修复/error页面,如此处所述Spring security redirects to page with status code 999

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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