亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring boot:注冊成功后嘗試自動登錄時 java.lang.

Spring boot:注冊成功后嘗試自動登錄時 java.lang.

慕容3067478 2023-07-19 16:03:04
我需要在注冊成功后自動登錄。java.lang.StackOverflowError : null,但在通過 Postman 測試我的代碼時得到了??刂破黝悾篅RestControllerpublic class RegistrationController {? ? @Autowired? ? private UserService userService;? ? @Autowired? ? private AuthenticationManager authenticationManager;? ? @PostMapping("/api/user/registration")? ? public ResponseEntity registerNewUserAccount(? ? ? ? ? ? @RequestBody @Valid RegistrationDto userDto, HttpServletRequest request){? ? ? ? userService.save(userDto);? ? ? ? authenticateUser(userDto, request);? ? ? ? return ResponseEntity.ok().build();? ? }? ? private void authenticateUser(RegistrationDto userDto, HttpServletRequest request){? ? ? ? String username = userDto.getEmailAddress();? ? ? ? String password = userDto.getPassword();? ? ? ? UsernamePasswordAuthenticationToken token =? ? ? ? ? ? ? ? new UsernamePasswordAuthenticationToken(username, password);? ? ? ? request.getSession();? ? ? ? token.setDetails(new WebAuthenticationDetails(request));? ? ? ? Authentication authenticatedUser =? ? ? ? ? ? ? ? authenticationManager.authenticate(token);? ? ? ? SecurityContextHolder.getContext().setAuthentication(authenticatedUser);? ? }}安全配置類:@Configuration@EnableWebSecuritypublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {? ? @Override? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception {? ? ? ? super.configure(auth);? ? }? ? @Override? ? protected void configure(HttpSecurity http) throws Exception {? ? ? ? http? ? ? ? ? ? ? ? .authorizeRequests()? ? ? ? ? ? ? ? .anyRequest().permitAll()? ? ? ? ? ? ? ? .and()? ? ? ? ? ? ? ? .csrf().disable();? ? }? ? @Bean? ? @Override? ? public AuthenticationManager authenticationManagerBean() throws Exception {? ? ? ? return super.authenticationManagerBean();? ? }? ? @Bean? ? public BCryptPasswordEncoder cryptPasswordEncoder(){? ? ? ? return new BCryptPasswordEncoder();? ? }}我知道StackOverflowError并且我猜測AuthenticationManagerBuilder或者authenticationManagerBean應該導致這個問題。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

您的猜測是正確的:) 您應該AuthenticationManager正確配置。您引用的鏈接沒有明確表明這一點。


配置它的方法有很多種:顯式提供 的實現AuthenticationManager,或者配置將創建 的構建器AuthenticationManager,或者AuthenticationManager通過 XML 進行配置等。下面是配置它的多種可能方法中的 2 種。




1.提供自己的AuthenticationManager


對于某些真正的身份驗證,您可以實現AuthenticationManager基于 LDAP 或 JDBC 的身份驗證。為了演示這個想法,這里有一個虛擬實現,足以使您的代碼運行。


public class DummyAuthenticationManager implements AuthenticationManager {


    @Override

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        // Dummy implementation. We don't check anything here.

        return authentication;

    }


}

在您SecurityConfiguration創建它的實例,如下所示:


@Configuration

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Bean

    @Override

    public AuthenticationManager authenticationManagerBean() throws Exception {

        return new DummyAuthenticationManager();

    }


    ...


}


通過這些更改,您的代碼將運行,并且您可以繼續逐步擴展它。




2.使用AuthenticationManagerBuilder


AuthenticationManager您可以配置AuthenticationManagerBuilder將為您構建AuthenticationManager所需的內容,而不是實現。


@Configuration

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication()

            .withUser("user1").password("password1").roles("USER").and()

            .withUser("user2").password("password2").roles("USER").and()

            .withUser("admin").password("password3").roles("USER", "ADMIN");

    }


    @Bean

    @Override

    public AuthenticationManager authenticationManagerBean() throws Exception {

        return super.authenticationManager();

    }


    ...

}


通過這些更改,您的代碼將運行,并且您可以繼續逐步擴展它。例如,對于實際的東西,inMemoryAuthentication()您可以使用ldapAuthentication()或jdbcAuthentication()或其他一些配置器。


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號