Commit 2d7d83f013f60b51cfd0b24b06c89cc0b7f98761

Authored by Igor Kulikov
2 parents b124f86a feba4b04

Merge branch 'develop/2.5.1'

@@ -32,6 +32,8 @@ import org.thingsboard.server.utils.MiscUtils; @@ -32,6 +32,8 @@ import org.thingsboard.server.utils.MiscUtils;
32 import javax.servlet.http.HttpServletRequest; 32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse; 33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException; 34 import java.io.IOException;
  35 +import java.net.URLEncoder;
  36 +import java.nio.charset.StandardCharsets;
35 37
36 @Component(value = "oauth2AuthenticationSuccessHandler") 38 @Component(value = "oauth2AuthenticationSuccessHandler")
37 @ConditionalOnProperty(prefix = "security.oauth2", value = "enabled", havingValue = "true") 39 @ConditionalOnProperty(prefix = "security.oauth2", value = "enabled", havingValue = "true")
@@ -57,16 +59,22 @@ public class Oauth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS @@ -57,16 +59,22 @@ public class Oauth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
57 public void onAuthenticationSuccess(HttpServletRequest request, 59 public void onAuthenticationSuccess(HttpServletRequest request,
58 HttpServletResponse response, 60 HttpServletResponse response,
59 Authentication authentication) throws IOException { 61 Authentication authentication) throws IOException {
60 - OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) authentication;  
61 62
62 - OAuth2Client oauth2Client = oauth2Configuration.getClientByRegistrationId(token.getAuthorizedClientRegistrationId());  
63 - OAuth2ClientMapper mapper = oauth2ClientMapperProvider.getOAuth2ClientMapperByType(oauth2Client.getMapperConfig().getType());  
64 - SecurityUser securityUser = mapper.getOrCreateUserByClientPrincipal(token, oauth2Client.getMapperConfig()); 63 + String baseUrl = MiscUtils.constructBaseUrl(request);
  64 + try {
  65 + OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) authentication;
65 66
66 - JwtToken accessToken = tokenFactory.createAccessJwtToken(securityUser);  
67 - JwtToken refreshToken = refreshTokenRepository.requestRefreshToken(securityUser); 67 + OAuth2Client oauth2Client = oauth2Configuration.getClientByRegistrationId(token.getAuthorizedClientRegistrationId());
  68 + OAuth2ClientMapper mapper = oauth2ClientMapperProvider.getOAuth2ClientMapperByType(oauth2Client.getMapperConfig().getType());
  69 + SecurityUser securityUser = mapper.getOrCreateUserByClientPrincipal(token, oauth2Client.getMapperConfig());
68 70
69 - String baseUrl = MiscUtils.constructBaseUrl(request);  
70 - getRedirectStrategy().sendRedirect(request, response, baseUrl + "/?accessToken=" + accessToken.getToken() + "&refreshToken=" + refreshToken.getToken()); 71 + JwtToken accessToken = tokenFactory.createAccessJwtToken(securityUser);
  72 + JwtToken refreshToken = refreshTokenRepository.requestRefreshToken(securityUser);
  73 +
  74 + getRedirectStrategy().sendRedirect(request, response, baseUrl + "/?accessToken=" + accessToken.getToken() + "&refreshToken=" + refreshToken.getToken());
  75 + } catch (Exception e) {
  76 + getRedirectStrategy().sendRedirect(request, response, baseUrl + "/login?loginError=" +
  77 + URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8.toString()));
  78 + }
71 } 79 }
72 } 80 }