package com.juick.server.security; import com.juick.server.security.entities.JuickUser; import com.juick.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import javax.inject.Inject; import java.util.Collections; /** * Created by vitalyster on 25.11.2016. */ public class JuickAuthenticationProvider implements AuthenticationProvider { private final Logger logger = LoggerFactory.getLogger(getClass()); @Inject private UserService userService; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String name = authentication.getName(); String password = authentication.getCredentials().toString(); boolean isAuthenticated = userService.checkPassword(name, password) > 0; logger.info("user {} authenticated: {}", name, isAuthenticated); return isAuthenticated ? new UsernamePasswordAuthenticationToken(name, password, JuickUser.USER_AUTHORITY) : null; } @Override public boolean supports(Class authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } }