第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Spring boot:注冊(cè)成功后嘗試自動(dòng)登錄時(shí) java.lang.

Spring boot:注冊(cè)成功后嘗試自動(dòng)登錄時(shí) java.lang.

慕容3067478 2023-07-19 16:03:04
我需要在注冊(cè)成功后自動(dòng)登錄。java.lang.StackOverflowError : null,但在通過 Postman 測(cè)試我的代碼時(shí)得到了??刂破黝悾篅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并且我猜測(cè)AuthenticationManagerBuilder或者authenticationManagerBean應(yīng)該導(dǎo)致這個(gè)問題。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的猜測(cè)是正確的:) 您應(yīng)該AuthenticationManager正確配置。您引用的鏈接沒有明確表明這一點(diǎn)。


配置它的方法有很多種:顯式提供 的實(shí)現(xiàn)AuthenticationManager,或者配置將創(chuàng)建 的構(gòu)建器AuthenticationManager,或者AuthenticationManager通過 XML 進(jìn)行配置等。下面是配置它的多種可能方法中的 2 種。




1.提供自己的AuthenticationManager


對(duì)于某些真正的身份驗(yàn)證,您可以實(shí)現(xiàn)AuthenticationManager基于 LDAP 或 JDBC 的身份驗(yàn)證。為了演示這個(gè)想法,這里有一個(gè)虛擬實(shí)現(xiàn),足以使您的代碼運(yùn)行。


public class DummyAuthenticationManager implements AuthenticationManager {


    @Override

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

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

        return authentication;

    }


}

在您SecurityConfiguration創(chuàng)建它的實(shí)例,如下所示:


@Configuration

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Bean

    @Override

    public AuthenticationManager authenticationManagerBean() throws Exception {

        return new DummyAuthenticationManager();

    }


    ...


}


通過這些更改,您的代碼將運(yùn)行,并且您可以繼續(xù)逐步擴(kuò)展它。




2.使用AuthenticationManagerBuilder


AuthenticationManager您可以配置AuthenticationManagerBuilder將為您構(gòu)建AuthenticationManager所需的內(nèi)容,而不是實(shí)現(xiàn)。


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

    }


    ...

}


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


查看完整回答
反對(duì) 回復(fù) 2023-07-19
  • 1 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)