Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package hr.foi.sis.controllers; import hr.foi.sis.model.Credentials; import hr.foi.sis.model.Person; import hr.foi.sis.model.Role; import hr.foi.sis.repositories.PersonRepository; import hr.foi.sis.utility.PBKDF2; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.List; import org.jboss.logging.Logger.Level; import org.jboss.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * * @author paz * * */ @RestController @RequestMapping(value = "/person") public class PersonController { PersonRepository personRepository; @Autowired public PersonController(PersonRepository personRepository) { this.personRepository = personRepository; } /** * gets all users from database * @return all users in json format with HTTP 200 */ @RequestMapping(value = "/", method = RequestMethod.GET) @PreAuthorize("hasRole('ROLE_ADMIN')") public ResponseEntity<List<Person>> retrieveAll() { Logger.getLogger("PersonController.java").log(Level.INFO, "GET on /person -- retrieving full list of users"); return new ResponseEntity(this.personRepository.findAll(), HttpStatus.OK); } @RequestMapping(value = "/id", method = RequestMethod.GET) @PreAuthorize("isAuthenticated()") public ResponseEntity<Long> retriveId() { Logger.getLogger("PersonController.java").log(Level.INFO, "GET on /person/id -- retrieving userId"); Person creator = (Person) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return new ResponseEntity(creator.getIdPerson(), HttpStatus.OK); } /** * inserts new user to database * @param person user to insert * @return person info and HTTP 200 on success or HTTP BAD REQUEST on fail */ @RequestMapping(value = "/signup", method = RequestMethod.POST) public ResponseEntity<Person> signup(@RequestBody Person person) throws NoSuchAlgorithmException, InvalidKeySpecException { Logger.getLogger("PersonController.java").log(Level.INFO, "POST on /person/signup -- " + person.toString()); Role role = new Role(); role.setId(2); role.setName("ROLE_USER"); person.setRole(role); String salt = PBKDF2.generateSalt(); person.setSalt(salt); String passwordHash = PBKDF2.getEncryptedPassword(person.getCredentials().getPassword(), salt); person.getCredentials().setPassword(passwordHash); Logger.getLogger("PersonController.java").log(Level.INFO, "Password -- " + passwordHash); Person signed = this.personRepository.save(person); if (signed != null) { Logger.getLogger("PersonController.java").log(Level.INFO, "Registration success for " + signed.toString()); return new ResponseEntity(signed, HttpStatus.OK); } else { Logger.getLogger("PersonController.java").log(Level.WARN, "Registration failed for " + person.toString()); return new ResponseEntity(HttpStatus.BAD_REQUEST); } } /** * gets user with specified id * @param id id of user * @return person info with HTTP 200 on success or HTTP 404 on fail */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) @PreAuthorize("isAuthenticated() and principal.idPerson == #id") public ResponseEntity<Person> retrieveById(@PathVariable("id") long id) { Logger.getLogger("PersonController.java").log(Level.INFO, "GET on /person/" + id + " -- "); Person found = this.personRepository.findByIdPerson(id); if (found != null) { Logger.getLogger("PersonController.java").log(Level.INFO, "User found for id " + id + ", returning " + found.toString()); return new ResponseEntity(found, HttpStatus.OK); } else { Logger.getLogger("PersonController.java").log(Level.WARN, "No user found for id " + id); return new ResponseEntity(HttpStatus.NOT_FOUND); } } }