修改实体类名称,并且使用链式编码
This commit is contained in:
parent
9afb93a4fc
commit
854c747911
|
@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.ossez.spring.security.models.entity.User;
|
||||
import com.ossez.spring.security.models.entity.Person;
|
||||
import com.ossez.spring.security.repository.RoleRepository;
|
||||
import com.ossez.spring.security.repository.UserRepository;
|
||||
|
||||
|
@ -87,9 +87,11 @@ public class AuthController {
|
|||
}
|
||||
|
||||
// Create new user's account
|
||||
User user = new User(signUpRequest.getUsername(),
|
||||
signUpRequest.getEmail(),
|
||||
encoder.encode(signUpRequest.getPassword()));
|
||||
Person person = new Person()
|
||||
.setUserEmail(signUpRequest.getUsername())
|
||||
.setUserPassword(encoder.encode(signUpRequest.getPassword()))
|
||||
.setUserEmail(signUpRequest.getEmail());
|
||||
|
||||
|
||||
Set<String> strRoles = signUpRequest.getRole();
|
||||
Set<Role> roles = new HashSet<>();
|
||||
|
@ -121,8 +123,8 @@ public class AuthController {
|
|||
});
|
||||
}
|
||||
|
||||
user.setRoles(roles);
|
||||
userRepository.save(user);
|
||||
person.setRoles(roles);
|
||||
userRepository.save(person);
|
||||
|
||||
return ResponseEntity.ok(new MessageResponse("User registered successfully!"));
|
||||
}
|
||||
|
|
|
@ -1,47 +1,43 @@
|
|||
package com.ossez.spring.security.models.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Person Entity
|
||||
* @author YuCheng Hu
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "username"), @UniqueConstraint(columnNames = "email")})
|
||||
@Data
|
||||
public class User {
|
||||
@Data()
|
||||
@Accessors(chain = true)
|
||||
@Table(name = "Person", uniqueConstraints = {@UniqueConstraint(columnNames = "username"), @UniqueConstraint(columnNames = "email")})
|
||||
public class Person {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 20)
|
||||
private String username;
|
||||
private String userName;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 120)
|
||||
private String userPassword;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 50)
|
||||
@Email
|
||||
private String email;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 120)
|
||||
private String password;
|
||||
private String userEmail;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
|
||||
private Set<Role> roles = new HashSet<>();
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String username, String email, String password) {
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
}
|
|
@ -5,11 +5,11 @@ import java.util.Optional;
|
|||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.ossez.spring.security.models.entity.User;
|
||||
import com.ossez.spring.security.models.entity.Person;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
Optional<User> findByUsername(String username);
|
||||
public interface UserRepository extends JpaRepository<Person, Long> {
|
||||
Optional<Person> findByUsername(String username);
|
||||
|
||||
Boolean existsByUsername(String username);
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.springframework.security.core.GrantedAuthority;
|
|||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import com.ossez.spring.security.models.entity.User;
|
||||
import com.ossez.spring.security.models.entity.Person;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
public class UserDetailsImpl implements UserDetails {
|
||||
|
@ -35,16 +35,16 @@ public class UserDetailsImpl implements UserDetails {
|
|||
this.authorities = authorities;
|
||||
}
|
||||
|
||||
public static UserDetailsImpl build(User user) {
|
||||
public static UserDetailsImpl build(Person user) {
|
||||
List<GrantedAuthority> authorities = user.getRoles().stream()
|
||||
.map(role -> new SimpleGrantedAuthority(role.getName().name()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new UserDetailsImpl(
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
user.getEmail(),
|
||||
user.getPassword(),
|
||||
user.getUserName(),
|
||||
user.getUserEmail(),
|
||||
user.getUserPassword(),
|
||||
authorities);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ossez.spring.security.models.entity.User;
|
||||
import com.ossez.spring.security.models.entity.Person;
|
||||
import com.ossez.spring.security.repository.UserRepository;
|
||||
|
||||
@Service
|
||||
|
@ -18,7 +18,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||
@Override
|
||||
@Transactional
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
User user = userRepository.findByUsername(username)
|
||||
Person user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username));
|
||||
|
||||
return UserDetailsImpl.build(user);
|
||||
|
|
Loading…
Reference in New Issue