Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @PostMapping("/login")
- public ResponseEntity<String> login(@RequestBody User requestUser) {
- String username = requestUser.getUsername();
- String password = requestUser.getPassword();
- try {
- if (userRepository.findByUsername(username) == null) {
- User user = new User(username, passwordEncoder.encode(password));
- userRepository.save(user);
- }
- UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken(username, password);
- Authentication auth = authenticationManager.authenticate(authReq);
- SecurityContext sc = SecurityContextHolder.getContext();
- sc.setAuthentication(auth);
- HttpHeaders headers = new HttpHeaders();
- headers.add("Location", "/main");
- return new ResponseEntity<>(headers, HttpStatus.FOUND);
- }
- catch (AuthenticationException e) {
- return new ResponseEntity<>(("Invalid."), HttpStatus.UNAUTHORIZED);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement