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

View File

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

View File

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