Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. public class AuthenticationFilter extends GenericFilterBean {
  2.  
  3. @Override
  4. public void doFilter(ServletRequest request, ServletResponse response,
  5. FilterChain filterChain) throws IOException, ServletException{
  6.  
  7. Authentication authentication = AuthenticationService
  8. .getAuthentication((HttpServletRequest)request);
  9.  
  10. SecurityContextHolder.getContext()
  11. .setAuthentication(authentication);
  12. filterChain.doFilter(request, response);
  13. }
  14. }
  15.  
  16. public class LoginFilter extends AbstractAuthenticationProcessingFilter {
  17.  
  18. public LoginFilter(String url, AuthenticationManager authManager) {
  19. super(new AntPathRequestMatcher(url));
  20. setAuthenticationManager(authManager);
  21. }
  22.  
  23. @Override
  24. public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
  25. throws AuthenticationException, IOException, ServletException {
  26.  
  27. AccountCredentials creds = new ObjectMapper()
  28. .readValue(request.getInputStream(), AccountCredentials.class);
  29.  
  30. return getAuthenticationManager().authenticate(
  31. new UsernamePasswordAuthenticationToken(
  32. creds.getUsername(),
  33. creds.getPassword(),
  34. Collections.emptyList()
  35. )
  36. );
  37. }
  38.  
  39. @Override
  40. protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
  41. Authentication auth) throws IOException, ServletException {
  42.  
  43. AuthenticationService.addToken(response, auth.getName());
  44. }
  45. }
  46.  
  47. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  48.  
  49. @Autowired
  50. private UserDetailsServiceImpl userDetailsService;
  51.  
  52. @Autowired
  53. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
  54.  
  55. auth.userDetailsService(userDetailsService)
  56. .passwordEncoder(new BCryptPasswordEncoder());
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement