Azure App Service에 대한 배포 인증
여러분은 이 모듈의 연습을 진행하기 위해 샌드박스 환경에 로그인했습니다. 이 환경은 대화형 방식이므로, 모든 배포가 샌드박스를 초기화할 때 사용한 자격 증명을 사용하여 인증되었습니다. 그러나 빌드 프로세스를 자동화할 때는 배포가 이 대화형 환경을 사용하지 않습니다. 자동화 시나리오에서는 프로젝트가 지원되는 인증 방법 중 하나를 사용하도록 구성해야 합니다.
이 단원에서는 회사에서 Azure 인증을 사용하도록 Maven을 구성하는 방법을 알아봅니다.
웹앱 인증
Azure는 유연한 앱 인증 방식을 지원합니다. 사용자가 선택하는 옵션은 회사의 빌드 환경에 따라 달라집니다. 아래에 Maven에 대해 애플리케이션 코드를 인증하는 옵션이 가장 간단한 방법부터 나와 있습니다.
Azure CLI를 사용하여 인증, 또는 Azure Portal에서 Cloud Shell을 사용합니다.
Azure 서비스 주체를 만들고, 서비스 주체 자격 증명으로 JSON 파일을 만들고, 프로젝트의
pom.xml
파일을 수정합니다.Azure 서비스 주체를 만들고, Maven
settings.xml
파일에 서비스 주체 자격 증명을 추가한 다음, Maven 설정을 사용하도록 프로젝트의pom.xml
파일을 수정합니다.
Microsoft는 가장 안정적이고 유연하며 일관성 있는 인증 방식을 제공하는 세 번째 옵션을 권장합니다. 실제 환경에서는 회사의 기존 Java 웹앱이 Azure CLI 도구가 설치되지 않은 로컬 서버에서 실행되고 있을 수 있습니다. 이 사실을 고려하면 서비스 주체와 Maven settings.xml
파일을 사용하여 인증을 추가하는 권장 사항을 구현하게 될 것입니다. 그러나 이 연습에서는 샌드박스에 서비스 사용자를 만드는 데 필요한 권한이 없습니다.
Azure CLI를 사용하여 인증
Maven을 인증하는 가장 쉬운 방법은 Azure CLI로 로그인하는 것입니다. 이렇게 하면 Azure App Service용 Maven 플러그인이 추가 구성 없이 자격 증명을 사용하여 앱을 배포할 수 있습니다.
이 모듈에서 Microsoft Learn Sandbox를 사용하여 연습을 진행할 때 그랬듯이 Azure Cloud Shell을 사용하는 경우, 기본적으로 Azure에 로그인되어 있으므로 더 많은 명령을 실행할 필요가 없습니다. 그러나 별도의 컴퓨터에서 Azure CLI를 사용하는 경우에는 az login
명령을 사용하여 로그인해야 합니다.
서비스 주체를 통한 인증
웹앱을 인증하는 두 번째 방법은 Azure 서비스 주체를 만든 후에 서비스 주체 자격 증명을 프로젝트 설정에서 참조할 파일에 저장하는 것입니다.
Azure CLI를 사용하여 Azure 서비스 주체를 만들려면 다음 단계를 수행합니다.
Azure CLI에서 다음 명령을 실행하여 Azure 서비스 주체를 만듭니다.
az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
여기서
https://mywebapp-1234567890.azurewebsites.net/
은 웹앱의 URL입니다.이 명령은 다음 예와 유사한 JSON 개체가 포함된 응답을 반환합니다.
Creating 'Contributor' role assignment under scope '/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss' The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli { "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", "displayName": "mywebapp-1234567890.azurewebsites.net/", "name": "https://mywebapp-1234567890.azurewebsites.net/", "password": "...", "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt" }
JSON 출력의 정보를 사용하도록 웹앱의
pom.xml
파일을 수정합니다.코드 편집기를 사용하여
pom.xml
파일을 엽니다.cd ~/MyWebApp code pom.xml
azure-webapp-maven-plugin
의<configuration>
섹션을 찾습니다.<region>
요소가 포함된 줄 뒤에 다음 XML을 추가하고 JSON 출력의 정보를 사용합니다.<auth> <type>service_principal</type> <client>value-of-appId</client> <tenant>value-of-tenant</tenant> <key>value-of-password</key> <environment>azure</environment> </auth>
azure-webapp-maven-plugin
섹션은 이제 다음 예제와 비슷해야 합니다.<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-webapp-maven-plugin</artifactId> <version>2.13.0</version> <configuration> <schemaVersion>v2</schemaVersion> <resourceGroup>MyWebApp-1714654093047-rg</resourceGroup> <appName>MyWebApp-1714654093047</appName> <pricingTier>S1</pricingTier> <region>centralus</region> <auth> <type>service_principal</type> <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client> <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant> <key>abcdefghijklmnopqrstuvwxyz1234567890</key> <environment>azure</environment> </auth> <runtime> <os>Linux</os> <javaVersion>Java 17</javaVersion> <webContainer>Tomcat 10.0</webContainer> </runtime> <deployment> <resources> <resource> <directory>${project.basedir}/target</directory> <includes> <include>*.war</include> </includes> </resource> </resources> </deployment> </configuration> </plugin>
Ctrl+S를 입력하여 변경 내용을 저장합니다.
Ctrl+Q를 입력하여 코드 편집기를 종료합니다.
Maven을 사용하여 웹앱을 빌드하고 Azure App Service에 배포합니다.
mvn azure-webapp:deploy
Maven에 일련의 빌드 메시지가 표시됩니다. 마지막 메시지는 Azure에 성공적으로 배포되었음을 나타냅니다.
[INFO] Scanning for projects... [INFO] [INFO] -------------------< com.microsoft.example:MyWebApp >------------------- [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- azure-webapp-maven-plugin:2.13.0:deploy (default-cli) @ MyWebApp --- [INFO] Auth type: SERVICE_PRINCIPAL [INFO] Username: 74d82376-184f-400e-a08e-27cd522d7559 [INFO] There is only one subscription '...' in your account, will use it automatically. [INFO] Subscription: ... [INFO] Failed to get version of your artifact, skip artifact compatibility test [INFO] Trying to deploy external resources to MyWebApp-1714654093047... [INFO] Successfully deployed the resources to MyWebApp-1714654093047 [INFO] Trying to deploy artifact to MyWebApp-1714654093047... [INFO] Deploying (/home/cephas/MyWebApp/target/MyWebApp.war)[war] ... [INFO] Application url: https://mywebapp-1714654093047.azurewebsites.net [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 47.052 s [INFO] Finished at: 2024-05-02T13:10:54Z [INFO] ------------------------------------------------------------------------
응답의
Auth type: SERVICE_PRINCIPAL
줄은 웹앱을 Azure에 게시하는 데 서비스 주체가 사용되었음을 나타냅니다.
Maven settings.xml
파일을 사용하여 인증
웹앱을 인증하는 세 번째 방법은 Azure 서비스 주체를 만든 후에 서비스 주체 자격 증명을 포함하는 Maven settings.xml
파일을 만들고, Maven 설정을 사용하도록 프로젝트의 pom.xml
파일을 수정하는 것입니다.
Azure CLI를 사용하여 Azure 서비스 주체를 만드는 방법은 이 단원의 앞 섹션에 나온 방법과 동일합니다.
Azure CLI에서 다음 명령을 실행하여 Azure 서비스 주체를 만듭니다.
az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
여기서
https://mywebapp-1234567890.azurewebsites.net/
은 웹앱의 URL입니다.이 명령은 다음 예와 유사한 JSON 개체가 포함된 응답을 반환합니다.
Creating 'Contributor' role assignment under scope '/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss' The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli { "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", "displayName": "mywebapp-1234567890.azurewebsites.net/", "name": "https://mywebapp-1234567890.azurewebsites.net/", "password": "...", "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt" }
Maven이 사용할
settings.xml
파일의 사용자 버전을 만듭니다.코드 편집기를 사용하여 Maven 설정을 위한 새 XML 파일을 만듭니다.
code ~/.m2/settings.xml
파일에 다음 XML을 붙여넣습니다.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>azure-auth</id> <configuration> <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client> <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant> <key>pppppppp-pppp-pppp-pppp-pppppppppppp</key> </configuration> </server> </servers> </settings>
여기서,
매개 변수 설명 client
서비스 주체의 appId
값을 지정합니다.key
서비스 주체의 password
값을 지정합니다.tenant
서비스 주체의 tenant
값을 지정합니다.Ctrl+S를 입력하여 변경 내용을 저장합니다.
Ctrl+Q를 입력하여 코드 편집기를 종료합니다.
인증 파일을 참조하도록 웹앱의
pom.xml
파일을 수정합니다.코드 편집기를 사용하여
pom.xml
파일을 엽니다.cd ~/MyWebApp code pom.xml
azure-webapp-maven-plugin
의<configuration>
섹션을 찾습니다.<region>
요소를 포함하는 줄 뒤에 다음 XML을 추가합니다.<auth> <type>service_principal</type> <serverId>azure-auth</serverId> </auth>
azure-webapp-maven-plugin
섹션은 이제 다음 예제와 비슷해야 합니다.<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-webapp-maven-plugin</artifactId> <version>2.13.0</version> <configuration> <schemaVersion>V2</schemaVersion> <resourceGroup>maven-publish</resourceGroup> <appName>MyWebApp-1234567890</appName> <pricingTier>S1</pricingTier> <region>centralus</region> <auth> <type>service_principal</type> <serverId>azure-auth</serverId> </auth> <runtime> <os>Linux</os> <javaVersion>Java 17</javaVersion> <webContainer>Tomcat 10.0</webContainer> </runtime> <deployment> <resources> <resource> <directory>${project.basedir}/target</directory> <includes> <include>*.war</include> </includes> </resource> </resources> </deployment> </configuration> </plugin>
Ctrl+S를 입력하여 변경 내용을 저장합니다.
Ctrl+Q를 입력하여 코드 편집기를 종료합니다.
Maven을 사용하여 웹앱을 빌드하고 Azure App Service에 배포합니다.
mvn azure-webapp:deploy
Maven에 일련의 빌드 메시지가 표시됩니다. 마지막 메시지는 Azure에 성공적으로 배포되었음을 나타냅니다.
[INFO] -------------------< com.microsoft.example:MyWebApp >------------------- [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- azure-webapp-maven-plugin:2.13.0:deploy (default-cli) @ MyWebApp --- [INFO] Auth type: SERVICE_PRINCIPAL [INFO] Username: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa [INFO] There is only one subscription '...' in your account, will use it automatically. [INFO] Subscription: ... [INFO] Failed to get version of your artifact, skip artifact compatibility test [INFO] Trying to deploy external resources to MyWebApp-1714654093047... [INFO] Successfully deployed the resources to MyWebApp-1714654093047 [INFO] Trying to deploy artifact to MyWebApp-1714654093047... [INFO] Deploying (/home/cephas/MyWebApp/target/MyWebApp.war)[war] ... [INFO] Application url: https://mywebapp-1714654093047.azurewebsites.net [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 53.611 s [INFO] Finished at: 2024-05-02T13:53:31Z [INFO] ------------------------------------------------------------------------
여기서 응답의
Auth type: SERVICE_PRINCIPAL
줄은 웹앱을 Azure로 게시하는 데 서비스 주체 자격 증명이 사용되었음을 나타냅니다.