further cleanup and added get by token string in usertokenservice

This commit is contained in:
Rohan Sircar 2019-11-19 12:33:56 +05:30
parent deaefaf866
commit 0ca1c47011
3 changed files with 12 additions and 12 deletions

View File

@ -1,8 +1,6 @@
package org.ros.chatto.security; package org.ros.chatto.security;
import org.ros.chatto.model.UserToken; import org.ros.chatto.model.UserToken;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.service.UserService;
import org.ros.chatto.service.UserTokenService; import org.ros.chatto.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
@ -15,14 +13,10 @@ import org.springframework.stereotype.Component;
@Component @Component
public class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter { public class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter {
@Autowired
private UserService userService;
@Autowired @Autowired
private TokenService tokenService; private TokenService tokenService;
@Autowired @Autowired
private UserTokenService userTokenService; private UserTokenService userTokenService;
@Autowired
private UserRepository userRepository;
@Autowired @Autowired
public CustomBasicAuthenticationFilter(final AuthenticationManager authenticationManager) { public CustomBasicAuthenticationFilter(final AuthenticationManager authenticationManager) {
@ -35,7 +29,7 @@ public class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter {
// Generate Token // Generate Token
// Save the token for the logged in user // Save the token for the logged in user
// send token in the response // send token in the response
UserToken userToken = userTokenService.getToken(authResult.getName()); UserToken userToken = userTokenService.getTokenByUserName(authResult.getName());
Token token; Token token;
if (userToken == null) { if (userToken == null) {
token = tokenService.allocateToken(""); token = tokenService.allocateToken("");

View File

@ -13,6 +13,7 @@ import javax.servlet.http.HttpServletRequest;
import org.ros.chatto.model.UserToken; import org.ros.chatto.model.UserToken;
import org.ros.chatto.repository.TokenRepository; import org.ros.chatto.repository.TokenRepository;
import org.ros.chatto.repository.UserRoleRepository; import org.ros.chatto.repository.UserRoleRepository;
import org.ros.chatto.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
@ -28,10 +29,10 @@ import org.springframework.web.filter.GenericFilterBean;
public class TokenAuthenticationFilter extends GenericFilterBean { public class TokenAuthenticationFilter extends GenericFilterBean {
@Autowired @Autowired
UserRoleRepository userRoleRepository; private TokenRepository tokenRepository;
@Autowired @Autowired
TokenRepository tokenRepository; private UserTokenService userTokenService;
@Autowired @Autowired
TokenService tokenService; TokenService tokenService;
@ -51,7 +52,7 @@ public class TokenAuthenticationFilter extends GenericFilterBean {
if (token == null) { if (token == null) {
throw new UsernameNotFoundException("Token not issued by us"); throw new UsernameNotFoundException("Token not issued by us");
} }
UserToken userToken = tokenRepository.findByToken(accessToken); UserToken userToken = userTokenService.getTokenByTokenString(accessToken);
if (userToken == null) { if (userToken == null) {
throw new UsernameNotFoundException("Token not associated with any user"); throw new UsernameNotFoundException("Token not associated with any user");

View File

@ -15,11 +15,16 @@ public class UserTokenService {
// @Cacheable // @Cacheable
public UserToken getToken(String userName) public UserToken getTokenByUserName(String userName)
{ {
return tokenRepository.findByUserName(userName); return tokenRepository.findByUserName(userName);
} }
public UserToken getTokenByTokenString(String tokenString)
{
return tokenRepository.findByToken(tokenString);
}
@Transactional @Transactional
public void saveToken(UserToken userToken) public void saveToken(UserToken userToken)
{ {