Search in sources :

Example 1 with JwtAuthenticationResponse

use of no.arkivlab.hioa.nikita.webapp.security.service.JwtAuthenticationResponse in project nikita-noark5-core by HiOA-ABI.

the class AuthenticationRestController method refreshAndGetAuthenticationToken.

@RequestMapping(value = "${jwt.route.authentication.refresh}", method = RequestMethod.GET)
public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest request) {
    String token = request.getHeader(tokenHeader);
    String username = jwtTokenUtil.getUsernameFromToken(token);
    JwtUser user = (JwtUser) userDetailsService.loadUserByUsername(username);
    if (jwtTokenUtil.canTokenBeRefreshed(token, user.getLastPasswordResetDate())) {
        String refreshedToken = jwtTokenUtil.refreshToken(token);
        return ResponseEntity.ok(new JwtAuthenticationResponse(refreshedToken));
    } else {
        return ResponseEntity.badRequest().body(null);
    }
}
Also used : JwtUser(no.arkivlab.hioa.nikita.webapp.security.JwtUser) JwtAuthenticationResponse(no.arkivlab.hioa.nikita.webapp.security.service.JwtAuthenticationResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JwtAuthenticationResponse

use of no.arkivlab.hioa.nikita.webapp.security.service.JwtAuthenticationResponse in project nikita-noark5-core by HiOA-ABI.

the class AuthenticationRestController method createAuthenticationToken.

@RequestMapping(value = "${jwt.route.authentication.path}", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {
    // Perform the security
    final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword()));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    // Reload password post-security so we can generate token
    final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
    final String token = jwtTokenUtil.generateToken(userDetails, device);
    // Return the token
    return ResponseEntity.ok(new JwtAuthenticationResponse(token));
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) JwtAuthenticationResponse(no.arkivlab.hioa.nikita.webapp.security.service.JwtAuthenticationResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JwtAuthenticationResponse (no.arkivlab.hioa.nikita.webapp.security.service.JwtAuthenticationResponse)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 JwtUser (no.arkivlab.hioa.nikita.webapp.security.JwtUser)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1