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>-
프로세스
pom.xml파일의 <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-serverGalleon 계층을 포함하는 방법을 보여줍니다.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>Java 파일을 저장할 디렉터리를 만듭니다.
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/authentication/여기서
APPLICATION_ROOT는 Maven 프로젝트의 루트 디렉터리입니다.다음 내용으로 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(); } }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)플러그인 <configuration> 요소에 다음
구성 추출을추가합니다.<cli-sessions> <cli-session> <script-files> <script>scripts/authentication.cli</script> </script-files> </cli-session> </cli-sessions>이 예에서는 기본
undertow보안 도메인을 서버에 정의된 보안 도메인으로 구성하는authentication.cliCLI 스크립트를 보여줍니다.참고패키징 시간 대신 런타임 시 CLI 스크립트를 실행하는 옵션이 있습니다. 이렇게 하려면 이 단계를 건너뛰고 10 단계로 진행합니다.
Maven 프로젝트의 루트 디렉터리에는 JBoss EAP JAR Maven 플러그인이 부팅 가능한 JAR에 추가하는 속성 파일을 저장할 디렉터리를 생성합니다.
$ mkdir -p APPLICATION_ROOT/extra-content/standalone/configuration/여기서
APPLICATION_ROOT는 애플리케이션의pom.xml구성 파일이 포함된 디렉터리입니다.이 디렉터리는
bootable-users.properties및bootable-groups.properties파일과 같은 파일을 저장합니다.bootable-users.properties파일에는 다음 내용이 포함되어 있습니다.testuser=bootable_passwordbootable-groups.properties파일에는 다음 내용이 포함되어 있습니다.testuser=Users기존 <
configuration> 요소에 다음추가합니다.extra-content-content-dirs요소를<extra-server-content-dirs> <extra-content>extra-content</extra-content> </extra-server-content-dirs>extra-content디렉터리에는 속성 파일이 포함되어 있습니다.애플리케이션을 부팅 가능한 JAR로 패키징합니다.
$ mvn package애플리케이션을 시작합니다.
mvn wildfly-jar:run6단계를 건너뛰고 빌드 중에 CLI 스크립트를 실행하지 않도록 선택한 경우 다음 명령을 사용하여 애플리케이션을 시작합니다.
mvn wildfly-jar:run -Dwildfly.bootable.arguments=--cli-script=scripts/authentication.cli서블릿을 호출하지만 인증 정보를 지정하지 마십시오.
curl -v http://localhost:8080/hello예상 출력:
HTTP/1.1 401 Unauthorized ... WWW-Authenticate: Basic realm="Example Realm"서버를 호출하고 인증 정보를 지정합니다. 예를 들면 다음과 같습니다.
$ curl -v -u testuser:bootable_password http://localhost:8080/hello부팅 가능한 JAR에 HTTP 인증이 활성화되었음을 나타내는 HTTP 200 상태가 반환됩니다. 예를 들면 다음과 같습니다.
HTTP/1.1 200 OK .... Hello testuser