Search in sources :

Example 1 with Student

use of ba.isss.models.Student in project SI2016_TIM6 by SoftverInzenjeringETFSA.

the class TokenAuthenticationService method getAuthentication.

public static Authentication getAuthentication(HttpServletRequest request) {
    ServletContext servletContext = request.getServletContext();
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    studentRepository = webApplicationContext.getBean(StudentRepository.class);
    String token = request.getHeader(HEADER_STRING);
    if (token != null) {
        // parse the token.
        String user = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody().getSubject();
        Student student = studentRepository.findStudentByUsername(user);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (student != null) {
            authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT"));
        }
        return user != null ? new UsernamePasswordAuthenticationToken(user, null, authorities) : null;
    }
    return null;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) StudentRepository(ba.isss.repositories.StudentRepository) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Student(ba.isss.models.Student) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 2 with Student

use of ba.isss.models.Student in project SI2016_TIM6 by SoftverInzenjeringETFSA.

the class PrijavaController method prijaviIspit.

@PreAuthorize("hasAnyRole('ROLE_STUDENT')")
@PostMapping(path = "/prijavi")
public void prijaviIspit(@ModelAttribute("prijava") Prijava p, Principal principal) throws Exception {
    Student s = studentService.findByUsername(principal.getName());
    prijavaService.SavePrijava(p, s);
}
Also used : Student(ba.isss.models.Student) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with Student

use of ba.isss.models.Student in project SI2016_TIM6 by SoftverInzenjeringETFSA.

the class PrijavaController method odjaviIspit.

@PreAuthorize("hasAnyRole('ROLE_STUDENT')")
@RequestMapping(path = "/odjavi", method = RequestMethod.POST)
@ResponseBody
public void odjaviIspit(@ModelAttribute("odjava") Prijava p, Principal principal) throws Exception {
    Student s = studentService.findByUsername(principal.getName());
    prijavaService.DeletePrijava(p, s);
}
Also used : Student(ba.isss.models.Student) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with Student

use of ba.isss.models.Student in project SI2016_TIM6 by SoftverInzenjeringETFSA.

the class StudentController method updatePassword.

@PreAuthorize("hasAnyRole('ROLE_STUDENT')")
@RequestMapping(value = "/update_password", method = RequestMethod.POST)
@ResponseBody
public String updatePassword(Principal principal, @RequestParam("password1") String pass1, @RequestParam("password2") String pass2, @RequestParam("password") String pass) throws NoSuchAlgorithmException {
    Student s = studentService.findOne(studentService.findByUsername(principal.getName()).getId());
    ShaPasswordEncoder encoder = new ShaPasswordEncoder(256);
    String oldPassHash = encoder.encodePassword(pass, null);
    String newPassHash = encoder.encodePassword(pass1, null);
    if (pass1.length() < 5)
        return "Prekratak password";
    if (!s.getPassword().equals(oldPassHash))
        return "Pogresan stari password";
    if (!pass1.equals(pass2))
        return "Passwordi razliciti";
    if (studentService.updatePassword(studentService.findByUsername(principal.getName()).getId(), newPassHash) == 0)
        return "ERROR";
    return "Password promijenjen";
}
Also used : ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) Student(ba.isss.models.Student) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Student (ba.isss.models.Student)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 StudentRepository (ba.isss.repositories.StudentRepository)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 ShaPasswordEncoder (org.springframework.security.authentication.encoding.ShaPasswordEncoder)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1