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

时间:2016-12-14 13:37:48

标签: java spring spring-security

我有一个问题,当我第一次登录时,我会得到

{"时间戳":1481719982036,"状态":999,"错误":"无""消息&# 34;:"没有可用消息"}但是第二个是好的。如果我清除缓存。我也失败了。 这是主要代码:

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

}

我不知道发生了什么。

5 个答案:

答案 0 :(得分:1)

检查您是否拥有以下所有文件夹

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

例外

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

当文件夹不存在但被排除时抛出。

答案 1 :(得分:0)

有同样的问题。将此添加到application.properties中即可解决问题

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

答案 2 :(得分:0)

我遇到了类似的情况,即“第一次登录失败,第二次登录成功,但是有一个例外”,这与spring安全有关。不确定是否与您的情况相同。以我为例,我发现主对象为空。我的案例也使用Zuul进行

我发现在我的自定义失败登录处理程序中。我重定向到本地主机。

  

.failureHandler(customAuthenticationFailureHandler())

我已从

更改为
  

response.sendRedirect(“ localhost” +“ / login / fail /”);

  

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

我的问题解决了。

答案 3 :(得分:0)

我有同样的情况。

仅解决了一项更改。

.defaultSuccessUrl("/index", true);

另请参阅:Spring security login always lands in an error page with no message 123的答案

答案 4 :(得分:0)

还要检查是否有项目中未找到但在登录页面上声明过的资源。您应该删除它们并按照Spring security redirects to page with status code 999

中的说明修复/error