Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final AuthenticationManager authenticationManager;
- private final PropertyFileText properties;
- public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
- this.authenticationManager = authenticationManager;
- properties = new PropertyFileText(this.getClass().getResource("/security.properties").getPath());
- setUsernameParameter("email");
- }
- @Override
- public Authentication attemptAuthentication(HttpServletRequest httpServletRequest,
- HttpServletResponse httpServletResponse)
- throws AuthenticationException {
- try {
- ObjectMapper objectMapper = new ObjectMapper();
- AuthenticationCredentials credentials = objectMapper.readValue(httpServletRequest.getInputStream(),
- AuthenticationCredentials.class);
- return authenticationManager.authenticate(
- new UsernamePasswordAuthenticationToken(
- credentials.getEmail(),
- credentials.getPassword(), emptyList()
- )
- );
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- @Override
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
- long expirationTime = Long.valueOf(properties.read("EXPIRATION_TIME"));
- String secretKey = properties.read("SECRET");
- String headerString = properties.read("HEADER_STRING");
- String tokenPrefix = properties.read("TOKEN_PREFIX");
- String token = Jwts.builder()
- .setSubject(((User) authResult.getPrincipal()).getUsername())
- .setExpiration(new Date(System.currentTimeMillis() + expirationTime))
- .signWith(SignatureAlgorithm.HS512, secretKey)
- .compact();
- response.addHeader(headerString, tokenPrefix + token);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement