登录Spring安全后保留相同的URL

时间:2016-09-30 10:27:35

标签: java spring spring-security

我想在登录后留在同一页面,但春天要求你重定向到defaultSuccessUrl 我的代码是这样的

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/css/**","/fonts/**","/js/**","/app/**","/images/**","/partials/**").permitAll()
                .anyRequest()
                    .authenticated()
        .and()
            .formLogin()
                .loginPage("/login")
                    .defaultSuccessUrl("/index.html")
                    .failureUrl("/login?error=true")
                    .permitAll()
                    .and().logout().logoutSuccessUrl("/login?logout")
                    .and().exceptionHandling().accessDeniedPage("/pages/303.html");

    }

2 个答案:

答案 0 :(得分:0)

设置

always-use-default-target =" true"

这会强制spring-security进入/index.html

答案 1 :(得分:0)

我认为你应该添加一个身份验证成功处理程序

    @Override
protected void configure(HttpSecurity http) throws Exception {
   SavedRequestAwareAuthenticationSuccessHandler successHandler = new    SavedRequestAwareAuthenticationSuccessHandler();
   successHandler.setUseReferer(true);//redirect to the previous page
    http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/css/**","/fonts/**","/js/**","/app/**","/images/**","/partials/**").permitAll()
            .anyRequest()
                .authenticated()
    .and()
        .formLogin()
            .loginPage("/login")
                .defaultSuccessUrl("/index.html")
                .failureUrl("/login?error=true")
                .permitAll()
                .successHandler(successHandler)//enable success handler
                .and().logout().logoutSuccessUrl("/login?logout")
                .and().exceptionHandling().accessDeniedPage("/pages/303.html");

}