cleaned up the code a bit
This commit is contained in:
parent
264bc9efde
commit
deaefaf866
@ -35,30 +35,18 @@ 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
|
||||||
// String tokenString = UUID.randomUUID().toString();
|
|
||||||
|
|
||||||
// System.out.println("Role = " + authResult.getAuthorities().iterator().next().getAuthority());
|
|
||||||
|
|
||||||
UserToken userToken = userTokenService.getToken(authResult.getName());
|
UserToken userToken = userTokenService.getToken(authResult.getName());
|
||||||
Token token;
|
Token token;
|
||||||
if (userToken == null) {
|
if (userToken == null) {
|
||||||
token = tokenService.allocateToken("");
|
token = tokenService.allocateToken("");
|
||||||
userToken = new UserToken();
|
userToken = new UserToken();
|
||||||
System.out.println("srwrrrrrrrrrrrr = " + authResult.getName());
|
|
||||||
// ChatUser user = userService.findByUserName(authResult.getName());
|
|
||||||
// ChatUser user = userRepository.findByUserName("hmm");
|
|
||||||
userToken.setTokenContent(token.getKey());
|
userToken.setTokenContent(token.getKey());
|
||||||
// userToken.setTokenContent(tokenString);
|
|
||||||
userToken.setUserName(authResult.getName());
|
userToken.setUserName(authResult.getName());
|
||||||
userToken.setRole(authResult.getAuthorities().iterator().next().getAuthority());
|
userToken.setRole(authResult.getAuthorities().iterator().next().getAuthority());
|
||||||
userTokenService.saveToken(userToken);
|
userTokenService.saveToken(userToken);
|
||||||
response.setHeader("X-AUTH-TOKEN", token.getKey());
|
response.setHeader("X-AUTH-TOKEN", token.getKey());
|
||||||
}
|
} else {
|
||||||
else {
|
response.setHeader("X-AUTH-TOKEN", userToken.getTokenContent());
|
||||||
token = tokenService.verifyToken(userToken.getTokenContent());
|
|
||||||
if(token!=null) {
|
|
||||||
response.setHeader("X-AUTH-TOKEN", token.getKey());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -46,31 +46,22 @@ public class TokenAuthenticationFilter extends GenericFilterBean {
|
|||||||
if (null != accessToken) {
|
if (null != accessToken) {
|
||||||
// get and check whether token is valid ( from DB or file wherever you are
|
// get and check whether token is valid ( from DB or file wherever you are
|
||||||
// storing the token)
|
// storing the token)
|
||||||
|
Token token = tokenService.verifyToken(accessToken);
|
||||||
|
|
||||||
|
if (token == null) {
|
||||||
|
throw new UsernameNotFoundException("Token not issued by us");
|
||||||
|
}
|
||||||
UserToken userToken = tokenRepository.findByToken(accessToken);
|
UserToken userToken = tokenRepository.findByToken(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");
|
||||||
}
|
}
|
||||||
Token token = tokenService.verifyToken(userToken.getTokenContent());
|
|
||||||
|
|
||||||
if (token == null) {
|
|
||||||
throw new UsernameNotFoundException("Token not issued by us");
|
|
||||||
}
|
|
||||||
|
|
||||||
String userName = userToken.getUserName();
|
String userName = userToken.getUserName();
|
||||||
if (userName == null) {
|
if (userName == null) {
|
||||||
throw new UsernameNotFoundException("User not found");
|
throw new UsernameNotFoundException("User not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// List<UserRole> userRoles = userRoleRepository.findByUser(chatUser.getUserName());
|
|
||||||
// // Populate SecurityContextHolder by fetching relevant information using token
|
|
||||||
// final UserDetails userPrincipal = User.withUsername(chatUser.getUserName()).password(chatUser.getPassword())
|
|
||||||
// .roles(userRoles.stream().map(userRole -> {
|
|
||||||
//// System.out.println("role = " + userRole.getRole().getName());
|
|
||||||
// return userRole.getRole().getName();
|
|
||||||
// }).toArray(size -> new String[size])).build();
|
|
||||||
// final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
|
||||||
// userPrincipal, null, userPrincipal.getAuthorities());
|
|
||||||
SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(userToken.getRole());
|
SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(userToken.getRole());
|
||||||
List<SimpleGrantedAuthority> updatedAuthorities = new ArrayList<SimpleGrantedAuthority>();
|
List<SimpleGrantedAuthority> updatedAuthorities = new ArrayList<SimpleGrantedAuthority>();
|
||||||
updatedAuthorities.add(simpleGrantedAuthority);
|
updatedAuthorities.add(simpleGrantedAuthority);
|
||||||
|
Loading…
Reference in New Issue
Block a user