Last active
August 7, 2023 04:40
-
-
Save A-pZ/b6bee6437f8c5e00be8bcf7aac0573dc to your computer and use it in GitHub Desktop.
Salesforceの認証を行う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.model; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | |
import com.fasterxml.jackson.databind.annotation.JsonNaming; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.Setter; | |
import lombok.ToString; | |
@NoArgsConstructor @Getter @Setter @ToString | |
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) // 認証結果のレスポンスはSnakeCase | |
public class AuthenticationResult { | |
int statusCode; | |
String accessToken; | |
String signature; | |
String instanceUrl; | |
String id; | |
String tokenType; | |
String issuedAt; | |
public String bearerToken() { | |
return "Bearer " + accessToken; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.repository; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.github.apz.salesforcesample.config.SalesforceProperties; | |
import com.github.apz.salesforcesample.model.AuthenticationResult; | |
import lombok.AllArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.http.MediaType; | |
import org.springframework.stereotype.Component; | |
import org.springframework.util.LinkedMultiValueMap; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.web.reactive.function.BodyInserters; | |
import org.springframework.web.reactive.function.client.WebClient; | |
@Component | |
@AllArgsConstructor | |
@Slf4j | |
public class SalesforceAuthentication { | |
SalesforceProperties salesforceProperties; | |
WebClient salesforceAuthWebClient; | |
public AuthenticationResult authentication() { | |
AuthenticationResult authenticationResult = salesforceAuthWebClient.post() | |
.contentType(MediaType.APPLICATION_FORM_URLENCODED) | |
.body(BodyInserters.fromFormData(authenticationCode())) | |
.retrieve() | |
.bodyToMono(AuthenticationResult.class) | |
.block(); | |
log.info("result: {}", authenticationResult.toString()); | |
return authenticationResult; | |
} | |
MultiValueMap<String, String> authenticationCode() { | |
return new LinkedMultiValueMap<>() {{ | |
add("grant_type", salesforceProperties.getGrantType()); | |
add("client_id", salesforceProperties.getConsumerKey()); | |
add("client_secret", salesforceProperties.getConsumerSecret()); | |
add("username", salesforceProperties.getMailAddress()); | |
add("password", salesforceProperties.getPassword() + salesforceProperties.getSecurityToken()); | |
}}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment