From b0d89589ae42ba9c099ef934c39bebc6b0a75f7f Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 26 Nov 2016 04:34:01 +0700 Subject: logging on provider --- .../security/JuickAuthenticationProvider.java | 22 ++++++++++++++-------- .../juick/server/security/entities/JuickUser.java | 6 +++--- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'juick-server/src/main/java/com/juick/server') diff --git a/juick-server/src/main/java/com/juick/server/security/JuickAuthenticationProvider.java b/juick-server/src/main/java/com/juick/server/security/JuickAuthenticationProvider.java index bf0ed4d7..8d0bfddb 100644 --- a/juick-server/src/main/java/com/juick/server/security/JuickAuthenticationProvider.java +++ b/juick-server/src/main/java/com/juick/server/security/JuickAuthenticationProvider.java @@ -1,11 +1,13 @@ 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 org.springframework.security.core.authority.SimpleGrantedAuthority; import javax.inject.Inject; import java.util.Collections; @@ -14,18 +16,22 @@ import java.util.Collections; * Created by vitalyster on 25.11.2016. */ public class JuickAuthenticationProvider implements AuthenticationProvider { + private final Logger logger = LoggerFactory.getLogger(getClass()); + @Inject - UserService userService; + private UserService userService; + @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String name = authentication.getName(); String password = authentication.getCredentials().toString(); - if (userService.checkPassword(name, password) > 0) { - return new UsernamePasswordAuthenticationToken(name, password, Collections.singletonList( - new SimpleGrantedAuthority("ROLE_USER") - )); - } - return null; + + boolean isAuthenticated = userService.checkPassword(name, password) > 0; + + logger.info("user {} authenticated: {}", name, isAuthenticated); + + return isAuthenticated ? + new UsernamePasswordAuthenticationToken(name, password, JuickUser.USER_AUTHORITY) : null; } @Override diff --git a/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java b/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java index 6cc002ae..fd06b65b 100644 --- a/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java +++ b/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java @@ -12,18 +12,18 @@ import java.util.Collections; * Created by aalexeev on 11/21/16. */ public class JuickUser implements UserDetails { - private static final GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER"); + public static final GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER"); + public static final Collection USER_AUTHORITY = Collections.singletonList(ROLE_USER); private final com.juick.User user; - public JuickUser(com.juick.User user) { this.user = user; } @Override public Collection getAuthorities() { - return Collections.singletonList(ROLE_USER); + return USER_AUTHORITY; } @Override -- cgit v1.2.3