Search in sources :

Example 1 with StudentRepository

use of ba.isss.repositories.StudentRepository 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)

Aggregations

Student (ba.isss.models.Student)1 StudentRepository (ba.isss.repositories.StudentRepository)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1