我需要在注册成功后自动登录。我关注了这些stackoverflow 帖子和文章java.lang.StackOverflowError : null,但在通过 Postman 测试我的代码时得到了。
控制器类:
@RestController
public 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);
}
}
Run Code Online (Sandbox Code Playgroud)
安全配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { …Run Code Online (Sandbox Code Playgroud) 我有两个表(模型)被调用Users,Books并且需要为我的表创建自定义 ID(主键)值,比如u25894r用户和b419k书籍。我在互联网和 stackoverflow 上搜索了很多,但我不明白我应该在哪里定义我的自定义 ID 生成器函数,例如,AppServiceProvider boot function或者在Model constructor函数中。如果你用一个小例子来说明方法,我会非常感激。
编辑:我需要编写为表生成唯一自定义 ID 的函数。