Spring MVC应用程序中的重定向错误

时间:2018-03-28 17:30:58

标签: spring spring-security

我会很快。我有一个Spring MVC项目,我正在使用Spring Security,因此在我成功登录服务器后,将我重定向到应用程序上下文而不是索引页面。不知道为什么会发生这种情况,我怀疑这可能是一个安全问题,但到目前为止我还没弄明白,所以请我帮忙解决这个问题。

我的登录表单操作是:$ {loginUrl} 并且重定向问题仅在我第一次尝试登录时发生,如果我退出并再次登录,则服务器会重定向我。

这是我的代码:

网络安全配置类:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    ServicioInicioSesion inicioSesion;

    @Autowired
    MessageSource messageSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/addUsuarios").permitAll()
                .antMatchers("/roles/**", "/usuarios/**").hasAuthority("Administrador")
                .antMatchers("/editarPerfil").authenticated()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login").defaultSuccessUrl("/index")
                .permitAll()
                .and()
                .logout()
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .logoutSuccessUrl("/login")
                .and()
                .exceptionHandling().accessDeniedPage("/403");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
           .ignoring()
           .antMatchers("/static/**");
    }

    @Bean(name = "authenticationManager")
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
        auth.setUserDetailsService(inicioSesion);
        auth.setMessageSource(messageSource);
        auth.setPasswordEncoder(passwordEncoder());
        return auth;
    }

    @Bean
    public PasswordEncoder p`enter code here`asswordEncoder() {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder;
    }
}

索引控制器类

@Controller
public class IndexController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String showIndex() {
        return "index";
    }
}

1 个答案:

答案 0 :(得分:0)

阿尔贝托。试试这个: 1 - 将value = "/index"替换为value = {"/","/index"} 2 - 删除method参数 @RequestMapping(value = {"/","/index"})

当您在请求中提交身份验证表单时,您有POST数据,但在您的情况下,您有RequestMethod.GET