0

In spring security, log in is done through the specified login page in the securityconfig class. Following is the code..

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

    http.formLogin().loginPage("/login").defaultSuccessUrl("/")
}

But I also want my users to get logged in right after they sign up. I tried something like:

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

    http.formLogin().loginPage("/login", "/signup").defaultSuccessUrl("/")
}

But this does not work. How do I do this in spring security?

Jeet Roy
  • 91
  • 1
  • 1
  • 4

1 Answers1

0

Auto login is not related with Spring Security Configuration file.... it's depend on your developing programming logic ...

after complete successful signup process you can call a login method, it will be give valid login session of that user. you can try this code ...

@Override
public void autoLogin(String username, String password) {
    UserDetails userDetails = userDetailsService.loadUserByUsername(username);
    UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());

    authenticationManager.authenticate(usernamePasswordAuthenticationToken);

    if (usernamePasswordAuthenticationToken.isAuthenticated()) {
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
        logger.debug(String.format("Auto login %s successfully!", username));
    }
}

for more details link1 link2 link3

Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28