diff options
author | Alexander Alexeev | 2017-04-05 17:36:38 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2017-04-05 14:02:56 +0300 |
commit | a9a2c587a4de11ce04aaae7a0c1a5dab1430794a (patch) | |
tree | bd86b40c1fefc10b3fa30d370ce0cdee5ba4ee06 /juick-server/src/main | |
parent | 4c99585f95cda1839f364524b6f68a16d063ed61 (diff) |
login by hash, remember-me
Diffstat (limited to 'juick-server/src/main')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java | 66 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/service/UserServiceImpl.java | 5 |
2 files changed, 69 insertions, 2 deletions
diff --git a/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java b/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java new file mode 100644 index 00000000..df1ae38c --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java @@ -0,0 +1,66 @@ +package com.juick.server.security; + +import com.juick.User; +import com.juick.server.security.entities.JuickUser; +import com.juick.service.UserService; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.authentication.RememberMeAuthenticationToken; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by aalexeev on 4/5/17. + */ +public class HashParamAuthenticationFilter extends OncePerRequestFilter { + public static final String PARAM_NAME = "hash"; + + private final UserService userService; + + + public HashParamAuthenticationFilter(UserService userService) { + this.userService = userService; + } + + @Override + protected void doFilterInternal( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain) throws ServletException, IOException { + + String hash = request.getHeader(PARAM_NAME); + + if (hash == null) + hash = request.getParameter(PARAM_NAME); + + if (hash != null && authenticationIsRequired()) { + User user = userService.getUserByHash(hash); + + if (!user.isAnonymous()) + SecurityContextHolder.getContext().setAuthentication( + new RememberMeAuthenticationToken(hash, new JuickUser(user), JuickUser.USER_AUTHORITY)); + } + + filterChain.doFilter(request, response); + } + + private boolean authenticationIsRequired() { + Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); + + if (existingAuth == null || !existingAuth.isAuthenticated()) + return true; + + if (existingAuth instanceof AnonymousAuthenticationToken) + return true; + + return false; + } +} diff --git a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java index 81a1fea7..1b33a6d3 100644 --- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java @@ -4,6 +4,7 @@ import com.juick.User; import com.juick.server.helpers.Auth; import com.juick.server.helpers.EmailOpts; import com.juick.server.helpers.UserInfo; +import com.juick.server.security.entities.AnonymousUser; import com.juick.util.UserUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -126,7 +127,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { return list.get(0); } // TODO: @NonNullable ? - return new User(); + return AnonymousUser.INSTANCE; } @Override @@ -284,7 +285,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { return user; } } - return new User(); + return AnonymousUser.INSTANCE; } @Transactional |