Non utilizzo spring security.
Il progetto è il seguente:
Model:
User
package com.security.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "utenti")
public class User {
@Id
@Column(name = "id_utente")
private Integer id_utente;
@Column(name = "username")
private String username;
@Column(name = "cognome")
private String cognome;
@Column(name = "nome")
private String nome;
@Column(name = "email")
private String email;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "user")
private Password password;
}
Password:
package com.security.model;
import java.security.MessageDigest;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "password")
public class Password {
@Id
@Column(name = "id_utente")
private Integer id_utente;
@Column(name = "password")
private String password;
@Column(name = "fl_valida")
private boolean fl_valida;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "id_utente", nullable = false)
private User user;
public String encryptSHA1(String plainText) {
String encrypted = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] passBytes = plainText.getBytes();
md.reset();
byte[] digested = md.digest(passBytes);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digested.length; i++) {
sb.append(Integer.toHexString(0xff & digested[i]));
}
encrypted = sb.toString();
} catch (Throwable th) {
System.out.println(th.getMessage());
}
return encrypted;
}
}
DTO:
package com.security.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginDto {
private String username;
private String password;
}
Repository:
UserRepository
package com.security.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.security.model.User;
public interface UserRepository extends JpaRepository<User,Integer>{
User findByUsername(String username);
}
PasswordRepository
package com.security.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.security.model.Password;
public interface PasswordRepository extends JpaRepository<Password,Integer>{
Password findByPassword(String password);
}
Service:
ApiResponse --> qui mi deve restituire nome, congome,ecc.. dell'untente
package com.security.service;
import lombok.Data;
@Data
public class ApiResponse {
private int status;
private String message;
private Object result;
public ApiResponse(int status, String message, Object result){
this.status = status;
this.message = message;
this.result = result;
}
}
UserService:
package com.security.service;
import java.util.List;
import com.security.dto.LoginDto;
import com.security.model.User;
public interface UserService {
public ApiResponse login(LoginDto loginDto);
public List<User> userAll();
}
UserServiceImpl:
package com.security.service;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.security.dto.LoginDto;
import com.security.model.Password;
import com.security.model.User;
import com.security.repository.PasswordRepository;
import com.security.repository.UserRepository;
@Transactional
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordRepository passwordRepository;
public ApiResponse login(LoginDto loginDto) {
Password password = new Password();
User user = new User();
if ((user.getId_utente() == password.getId_utente()) && password.isFl_valida() == true) {
// search Username
user = userRepository.findByUsername(loginDto.getUsername());
// search Password
password = passwordRepository.findByPassword(loginDto.getPassword());
// Decript HASH
String passwordHash = password.encryptSHA1(password.getPassword());
password.setPassword(passwordHash);
user.setPassword(password);
return new ApiResponse(200, "Login success", user);
} else {
if (!(user.getId_utente() == password.getId_utente())) {
System.out.println("User not exsist!");
}
}
return null;
}
public List<User> userAll() {
return userRepository.findAll();
}
}
Controller:
UserController
package com.security.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.security.dto.LoginDto;
import com.security.model.User;
import com.security.service.ApiResponse;
import com.security.service.UserService;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
//Login Username and Password
@PostMapping("/login")
public ApiResponse login(@RequestBody LoginDto loginDto) {
return userService.login(loginDto);
}
//Return Utenti
@GetMapping("/all")
public List<User> userAll() {
return userService.userAll();
}
}
I miei problemi:
1) Adesso l'errore che mi restituisce è il seguente:
org.hibernate.HibernateException: More than one row with the given identifier was found: 1093, for class: com.security.model.Password
Come posso risolvere? visto che è un problema di mappatura 1 to 1.
2) Ho un dubbio nel metodo login di UserServiceImpl, in pratica la password ricevuta viene decriptata in Hash, è stata impostata bene?