11.15. CLI 스크립트를 사용하여 부팅 가능한 JAR에 대한 HTTP 인증 활성화


CLI 스크립트를 사용하여 부팅 가능한 JAR에 대한 HTTP 인증을 활성화할 수 있습니다. 이 스크립트는 서버에 보안 영역과 보안 도메인을 추가합니다.

사전 요구 사항

  • 9.minor.micro.Final-redhat-XXXXX 와 같은 최신 Maven 플러그인 버전을 확인했습니다. 여기서 9 는 주요 버전이며 마이너 버전은 마이크로 마이크로 버전이며 X 는 Red Hat 빌드 번호입니다. 예: 9.0.1.Final-redhat-00009.
  • Maven 프로젝트를 생성하고 HTTP 인증이 필요한 애플리케이션을 생성하기 위한 종속 항목을 추가했습니다. 부팅 가능한 JAR Maven 프로젝트 생성 을 참조하십시오.

    중요

    Maven 프로젝트를 설정할 때 Maven archetype 구성에 HTTP 인증 값을 지정해야 합니다. 예를 들면 다음과 같습니다.

    $ mvn archetype:generate \
    -DgroupId=com.example.auth \
    -DartifactId=authentication \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-webapp \
    -DinteractiveMode=false
    cd authentication
    참고

    절차에 표시된 예제에서는 다음 속성을 지정합니다.

    • ${bootable.jar.maven.plugin.version} 은(는) Maven 플러그인 버전에 해당합니다.

    프로젝트에 이러한 속성을 설정해야 합니다. 예를 들면 다음과 같습니다.

    <properties>
        <bootable.jar.maven.plugin.version>9.0.1.Final-redhat-00009</bootable.jar.maven.plugin.version>
    </properties>

프로세스

  1. pom.xml 파일의 & lt;build > 요소에 다음 내용을 추가합니다. 예를 들면 다음과 같습니다.

    <plugins>
    	<plugin>
    		<groupId>org.wildfly.plugins</groupId>
    		<artifactId>wildfly-jar-maven-plugin</artifactId>
    		<version>${bootable.jar.maven.plugin.version}</version>
    		<configuration>
    			<channels>
    				<channel>
    					<manifest>
    						<groupId>org.jboss.eap.channels</groupId>
    						<artifactId>eap-8.0</artifactId>
    					</manifest>
    				</channel>
    				<channel>
    					<manifest>
    						<groupId>org.jboss.eap.channels</groupId>
    						<artifactId>eap-xp-5.0</artifactId>
    					</manifest>
    				</channel>
    			</channels>
    			<feature-pack-location>org.jboss.eap.xp:wildfly-galleon-pack</feature-pack-location>
    			<layers>
    				<layer>datasources-web-server</layer>
    			</layers>
    		</configuration>
    		<executions>
    			<execution>
    				<goals>
    					<goal>package</goal>
    				</goals>
    			</execution>
    		</executions>
    	</plugin>
    </plugins>

    이 예제에서는 elytron 하위 시스템을 포함하는 datasources-web-server Galleon 계층을 포함하는 방법을 보여줍니다.

  2. src/main/webapp/WEB-INF 디렉터리에서 web.xml 파일을 업데이트합니다. 예를 들면 다음과 같습니다.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app version="4.0"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
    
        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>Example Realm</realm-name>
        </login-config>
    
    </web-app>
  3. Java 파일을 저장할 디렉터리를 만듭니다.

    $ mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/

    여기서 APPLICATION_ROOT 는 Maven 프로젝트의 루트 디렉터리입니다.

  4. 다음 내용으로 Java 파일 TestServlet.java 를 만들고 파일을 APPLICATION_ROOT/src/main/java/com/example/authentication/ 디렉터리에 저장합니다.

    package com.example.authentication;
    
    import jakarta.servlet.annotation.HttpMethodConstraint;
    import jakarta.servlet.annotation.ServletSecurity;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet(urlPatterns = "/hello")
    @ServletSecurity(httpMethodConstraints = { @HttpMethodConstraint(value = "GET", rolesAllowed = { "Users" }) })
    public class TestServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            PrintWriter writer = resp.getWriter();
            writer.println("Hello " + req.getUserPrincipal().getName());
            writer.close();
        }
    
    }
  5. authentication.cli 와 같은 CLI 스크립트를 생성하고 APPLICATION_ROOT/scripts 디렉터리와 같은 부팅 가능한 JAR의 액세스 가능한 디렉터리에 저장합니다. 이 스크립트는 다음 명령을 포함해야 합니다.

    /subsystem=elytron/properties-realm=bootable-realm:add(users-properties={relative-to=jboss.server.config.dir, path=bootable-users.properties, plain-text=true}, groups-properties={relative-to=jboss.server.config.dir, path=bootable-groups.properties})
    /subsystem=elytron/security-domain=BootableDomain:add(default-realm=bootable-realm, permission-mapper=default-permission-mapper, realms=[{realm=bootable-realm, role-decoder=groups-to-roles}])
    
    /subsystem=undertow/application-security-domain=other:write-attribute(name=security-domain, value=BootableDomain)
  6. 플러그인 <configuration> 요소에 다음 구성 추출을 추가합니다.

    <cli-sessions>
        <cli-session>
            <script-files>
                <script>scripts/authentication.cli</script>
            </script-files>
        </cli-session>
    </cli-sessions>

    이 예에서는 기본 undertow 보안 도메인을 서버에 정의된 보안 도메인으로 구성하는 authentication.cli CLI 스크립트를 보여줍니다.

    참고

    패키징 시간 대신 런타임 시 CLI 스크립트를 실행하는 옵션이 있습니다. 이렇게 하려면 이 단계를 건너뛰고 10 단계로 진행합니다.

  7. Maven 프로젝트의 루트 디렉터리에는 JBoss EAP JAR Maven 플러그인이 부팅 가능한 JAR에 추가하는 속성 파일을 저장할 디렉터리를 생성합니다.

    $ mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/

    여기서 APPLICATION_ROOT 는 애플리케이션의 pom.xml 구성 파일이 포함된 디렉터리입니다.

    이 디렉터리는 bootable-users.propertiesbootable-groups.properties 파일과 같은 파일을 저장합니다.

    bootable-users.properties 파일에는 다음 내용이 포함되어 있습니다.

    testuser=bootable_password

    bootable-groups.properties 파일에는 다음 내용이 포함되어 있습니다.

    testuser=Users
  8. 기존 < configuration> 요소에 다음 extra-content-content-dirs 요소를 추가합니다.

    <extra-server-content-dirs>
                <extra-content>extra-content</extra-content>
    </extra-server-content-dirs>

    extra-content 디렉터리에는 속성 파일이 포함되어 있습니다.

  9. 애플리케이션을 부팅 가능한 JAR로 패키징합니다.

    $ mvn package
  10. 애플리케이션을 시작합니다.

    mvn wildfly-jar:run

    6단계를 건너뛰고 빌드 중에 CLI 스크립트를 실행하지 않도록 선택한 경우 다음 명령을 사용하여 애플리케이션을 시작합니다.

    mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cli
  11. 서블릿을 호출하지만 인증 정보를 지정하지 마십시오.

    curl -v http://localhost:8080/hello

    예상 출력:

    HTTP/1.1 401 Unauthorized
    ...
    WWW-Authenticate: Basic realm="Example Realm"
  12. 서버를 호출하고 인증 정보를 지정합니다. 예를 들면 다음과 같습니다.

    $ curl -v -u testuser:bootable_password http://localhost:8080/hello

    부팅 가능한 JAR에 HTTP 인증이 활성화되었음을 나타내는 HTTP 200 상태가 반환됩니다. 예를 들면 다음과 같습니다.

    HTTP/1.1 200 OK
    ....
    Hello testuser
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 문서 정보

Legal Notice

Theme

© 2026 Red Hat
맨 위로 이동