Spring Boot Post重定向返回302状态

时间:2019-06-27 17:58:40

标签: spring-boot redirect spring-security

成功注册后,我想重定向到主页。注册成功,但重定向过程不成功,再次返回登录页面,post方法返回302状态。

@RequestMapping(value="/bireysel/kaydet" , method = RequestMethod.POST)
public String saveBireyselKullanici(@Valid
        @ModelAttribute("kullaniciKayitModel") KullaniciKayitModel kullaniciKayitModel,
        Model model,
        BindingResult bindingResult,
        HttpServletRequest request, 
        HttpServletResponse response) {
    ErrorDetail errorDetail = null;

    if (bindingResult.hasErrors()) {
        return "yeniKayit";
    }

    kullaniciKayitModel.setBireyselKurumsal(BireyselKurumsalTypeEnum.BIREYSEL);

    errorDetail = registerService.hesapOlustur(kullaniciKayitModel);

    if(errorDetail == null) {
        registerService.authenticateUserAndSetSession(kullaniciKayitModel, request);
        model.addAttribute("welcomeMessage", "Hoş geldiniz");
        return "redirect:/anasayfa";
    }else {
        model.addAttribute("hataMesaj", "Kayıt esnasında hata meydana geldi!");
        return "yeniKayit";
    }

}

我的安全配置如下:

@Autowired
private DataSource dataSource;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
            .usersByUsernameQuery("SELECT username,password,enabled from toptansepetim.users WHERE username=? ")
            .authoritiesByUsernameQuery("SELECT username,role from toptansepetim.user_roles where username=?");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.csrf().requireCsrfProtectionMatcher(new RequestMatcher() {
        private Pattern allowedMethods = Pattern.compile("^(GET|POST)$");
        private RegexRequestMatcher apiMatcher = new RegexRequestMatcher("", null);

        @Override
        public boolean matches(HttpServletRequest request) {
            if (allowedMethods.matcher(request.getMethod()).matches())
                return false;

            if (apiMatcher.matches(request))
                return false;

            return true;
        }
    });

    http.authorizeRequests()
            .antMatchers("/css/**","/js/**","/fonts/**","/images/**", "/yeniKayit","/anasayfa","/bireysel/kaydet","/rest/kullanici/mevcut","/api/ililce/il/liste","/api/ililce/ilce/liste","/kullanici/bireysel/kaydet","/exceptions/**", "/home", "/anasayfa")
            .permitAll().antMatchers()
            .access("hasRole('" + RolTypeEnum.NORMAUL_USER.getValue() + "')").anyRequest().authenticated().and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/anasayfa").permitAll().usernameParameter("username")
            .passwordParameter("password").and().logout().logoutSuccessUrl("/login?logout").permitAll().and()
            .exceptionHandling().accessDeniedPage("/exceptions/403").and().csrf().csrfTokenRepository(new HttpSessionCsrfTokenRepository());

}

@Override
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)    
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

下面是我成功注册的登录方法

  public void authenticateUserAndSetSession(KullaniciKayitModel 
  model,HttpServletRequest request) {
    String username = model.getUsername();
    String password = model.getPassword();

     UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, password);
     authToken.setDetails(new WebAuthenticationDetails(request));
     Authentication authentication = authenticationManager.authenticate(authToken);
     SecurityContextHolder.getContext().setAuthentication(authentication);
}

Spring引导中302状态重定向的原因是什么

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题的原因是PassworEncoder。您必须发送带有解码的密码才能进行身份验证。