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;
}
Aggregations