|
| 1 | +package com.fd.tryout.security.jwt; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.jsonwebtoken.Jwts; |
| 5 | +import io.jsonwebtoken.SignatureAlgorithm; |
| 6 | +import lombok.var; |
| 7 | +import org.springframework.security.authentication.AuthenticationManager; |
| 8 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 9 | +import org.springframework.security.core.Authentication; |
| 10 | +import org.springframework.security.core.AuthenticationException; |
| 11 | +import org.springframework.security.core.GrantedAuthority; |
| 12 | +import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
| 13 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 14 | + |
| 15 | +import javax.servlet.FilterChain; |
| 16 | +import javax.servlet.http.HttpServletRequest; |
| 17 | +import javax.servlet.http.HttpServletResponse; |
| 18 | +import java.io.IOException; |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Date; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author fdanismaz |
| 27 | + * date: 10/20/18 4:33 PM |
| 28 | + */ |
| 29 | +public class JwtUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { |
| 30 | + |
| 31 | + private final JwtAuthenticationConfig config; |
| 32 | + private final ObjectMapper mapper; |
| 33 | + |
| 34 | + public JwtUsernamePasswordAuthenticationFilter(JwtAuthenticationConfig config, |
| 35 | + AuthenticationManager authenticationManager) { |
| 36 | + super(new AntPathRequestMatcher(config.getUrl(), "POST")); |
| 37 | + this.setAuthenticationManager(authenticationManager); |
| 38 | + this.config = config; |
| 39 | + this.mapper = new ObjectMapper(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) |
| 44 | + throws AuthenticationException, IOException { |
| 45 | + |
| 46 | + User user = this.mapper.readValue(request.getInputStream(), User.class); |
| 47 | + var authenticationToken = |
| 48 | + new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), Collections.emptyList()); |
| 49 | + return this.getAuthenticationManager().authenticate(authenticationToken); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, |
| 54 | + FilterChain chain, Authentication auth) { |
| 55 | + Instant now = Instant.now(); |
| 56 | + List<String> authorities = auth.getAuthorities().stream().map(GrantedAuthority::getAuthority) |
| 57 | + .collect(Collectors.toList()); |
| 58 | + String token = Jwts.builder() |
| 59 | + .setSubject(auth.getName()) |
| 60 | + .claim("authorities", authorities) |
| 61 | + .setIssuedAt(Date.from(now)) |
| 62 | + .setExpiration(Date.from(now.plusSeconds(config.getExpiration()))) |
| 63 | + .signWith(SignatureAlgorithm.HS256, config.getSecret().getBytes()) |
| 64 | + .compact(); |
| 65 | + response.addHeader(config.getHeader(), config.getPrefix() + " " + token); |
| 66 | + } |
| 67 | +} |
0 commit comments