Created
April 1, 2017 17:09
-
-
Save andreybleme/df031ab9c0fdc6034a03bb75b9553e34 to your computer and use it in GitHub Desktop.
andreybleme.com | JWT com Springboot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jwtme.security; | |
import java.io.IOException; | |
import java.util.Collections; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.security.authentication.AuthenticationManager; | |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.AuthenticationException; | |
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; | |
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter { | |
protected JWTLoginFilter(String url, AuthenticationManager authManager) { | |
super(new AntPathRequestMatcher(url)); | |
setAuthenticationManager(authManager); | |
} | |
@Override | |
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) | |
throws AuthenticationException, IOException, ServletException { | |
AccountCredentials credentials = new ObjectMapper() | |
.readValue(request.getInputStream(), AccountCredentials.class); | |
return getAuthenticationManager().authenticate( | |
new UsernamePasswordAuthenticationToken( | |
credentials.getUsername(), | |
credentials.getPassword(), | |
Collections.emptyList() | |
) | |
); | |
} | |
@Override | |
protected void successfulAuthentication( | |
HttpServletRequest request, | |
HttpServletResponse response, | |
FilterChain filterChain, | |
Authentication auth) throws IOException, ServletException { | |
TokenAuthenticationService.addAuthentication(response, auth.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment