Это мой код, который называется UserController
package co.edu.unal.software_engineering.labs.controller;
import co.edu.unal.software_engineering.labs.model.Role;
import co.edu.unal.software_engineering.labs.model.User;
import co.edu.unal.software_engineering.labs.pojo.LoginUserPOJO;
import co.edu.unal.software_engineering.labs.pojo.RegisterUserPOJO;
import co.edu.unal.software_engineering.labs.service.RoleService;
import co.edu.unal.software_engineering.labs.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.authentication.AuthenticationProvider;
import java.util.Collections;
@CrossOrigin
@RestController
public class UserController{
private UserService userService;
private RoleService roleService;
private PasswordEncoder passwordEncoder;
public UserController( UserService userService, RoleService roleService, PasswordEncoder passwordEncoder ){
this.userService = userService;
this.roleService = roleService;
this.passwordEncoder = passwordEncoder;
}
@PostMapping( value = { "/registro/nuevo-usuario/rol/{roleId}" } )
public ResponseEntity registerNewUser( @PathVariable Integer roleId, @RequestBody RegisterUserPOJO userPOJO ){
Role role = roleService.findById( roleId );
User existingUser = userService.findByUsername( userPOJO.getUsername( ) );
if( role == null || existingUser != null || !userService.isRightUser( userPOJO ) ){
return new ResponseEntity( HttpStatus.BAD_REQUEST );
}
User newUser = new User( );
newUser.setNames( userPOJO.getNames( ).toUpperCase( ) );
newUser.setSurnames( userPOJO.getSurnames( ).toUpperCase( ) );
newUser.setUsername( userPOJO.getUsername( ).toLowerCase( ) );
newUser.setPassword( passwordEncoder.encode( userPOJO.getPassword( ) ) );
newUser.setRoles( Collections.singletonList( role ) );
userService.save( newUser );
return new ResponseEntity( HttpStatus.CREATED );
}
@PostMapping( value = { "/registro/usuario/rol/{roleId}" } )
public ResponseEntity registerRoleToUser( @PathVariable Integer roleId, @RequestBody LoginUserPOJO userPOJO ){
Role role = roleService.findById( roleId );
User existingUser = userService.findByUsername( userPOJO.getUsername( ) );
if( role == null || existingUser == null || existingUser.hasRole( role ) ){
return new ResponseEntity( HttpStatus.BAD_REQUEST );
}else if( !passwordEncoder.matches( userPOJO.getPassword( ), existingUser.getPassword( ) ) ){
return new ResponseEntity( HttpStatus.UNAUTHORIZED );
}
existingUser.addRole( role );
userService.save( existingUser );
return new ResponseEntity( HttpStatus.CREATED );
}
@PostMapping( value = { "/login" } )
public ResponseEntity loginUser( @RequestBody LoginUserPOJO userPOJO ){
User existingUser = userService.findByUsername( userPOJO.getUsername( ) );
if(existingUser == null){
return new ResponseEntity( HttpStatus.BAD_REQUEST );
}else if( !passwordEncoder.matches( userPOJO.getPassword( ), existingUser.getPassword( ) ) ){
return new ResponseEntity( HttpStatus.UNAUTHORIZED );
}
return new ResponseEntity( HttpStatus.ACCEPTED );
}
}
, но я не знаю, как аутентифицировать пользователя в функции входа в систему с помощью OAuth2.
Не могли бы вы мне помочь?
Это другие файлы, вызываемые из контроллера. UserPOJO для входа в систему.
package co.edu.unal.software_engineering.labs.pojo;
/**
* Plain Old Java Object for Login
*/
public class LoginUserPOJO{
private String username;
private String password;
public String getUsername( ){
return username;
}
public void setUsername( String username ){
this.username = username;
}
public String getPassword( ){
return password;
}
public void setPassword( String password ){
this.password = password;
}
}
UserRepository
package co.edu.unal.software_engineering.labs.repository;
import co.edu.unal.software_engineering.labs.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Integer>{
User findByUsername( String username );
}
UserService
package co.edu.unal.software_engineering.labs.service;
import co.edu.unal.software_engineering.labs.model.User;
import co.edu.unal.software_engineering.labs.pojo.RegisterUserPOJO;
import co.edu.unal.software_engineering.labs.repository.UserRepository;
import org.springframework.stereotype.Service;
@Service
public class UserService{
private final UserRepository userRepository;
public UserService( UserRepository userRepository ){
this.userRepository = userRepository;
}
public User findByUsername( String username ){
return userRepository.findByUsername( username );
}
public void save( User user ){
userRepository.save( user );
}
public boolean isRightUser( RegisterUserPOJO user ){
boolean correctness = user.getNames( ) != null && user.getPassword( ) != null && user.getUsername( ) != null &&
user.getSurnames( ) != null;
if( correctness ){
correctness = !user.getNames( ).trim( ).isEmpty( )
&& !user.getPassword( ).trim( ).isEmpty( )
&& !user.getUsername( ).trim( ).isEmpty( )
&& !user.getSurnames( ).trim( ).isEmpty( );
}
return correctness;
}
}
Я уже внедрил полный каталог с использованием протокола аутентификации OAuth2.