jersey、jetty 埋め込み、および BASIC 認証を使用する SecurityContext を使用して単純な Web アプリを作成しようとしています。reaml は、/tmp/realm.properties のプロパティ ファイルからユーザーのデータを取得します。なんらかの理由でプロパティ ファイルが読み取られていないようです。Linux カーネルで監査ルールを使用したところ、どのプロセスでもプロパティ ファイルにアクセスできないことがわかりました。多くのチュートリアルと公式ドキュメント ( https://wiki.eclipse.org/Jetty/Tutorial/Realms ) を読みましたが、何かが機能していないようです。
/test/free > セキュリティ コンテキストなし (残りの呼び出しで到達できます)
/test/test > セキュリティ コンテキストあり (HTTP ERROR 401 Unauthorized が表示されます)
curl http://127.0.0.1:8080/test/test -u foo:bar
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 401 Unauthorized</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /test/test. Reason:
<pre> Unauthorized</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>
ここに私のコードがあります:
package org.java.jettyServer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.mortbay.jetty.UserRealm;
import org.mortbay.jetty.security.HashUserRealm;
public class JettyEmbedded {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
// Handler for multiple web apps
HandlerCollection handlers = new HandlerCollection();
// Creating the first web application context
WebAppContext webapp1 = new WebAppContext();
webapp1.setResourceBase("src/main/webapp");
webapp1.setContextPath("/");
handlers.addHandler(webapp1);
// Adding the handlers to the server
server.setHandler(handlers);
// Starting the Server
server.start();
System.out.println("Started!");
server.join();
}
}
資力:
package org.java.resources;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@Path("test")
public class EntryPoint {
@GET
@Path("test")
@Produces(MediaType.TEXT_PLAIN)
public String deleteRating(@Context SecurityContext context ) {
String username = context.getUserPrincipal().getName();
System.out.println(username);
return "hello\n";
}
@GET
@Path("free")
@Produces(MediaType.TEXT_PLAIN)
public String freeResource(){
return "free access, no pass\n";
}
}
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Service</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.java.resources</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure</web-resource-name>
<url-pattern>/test/test</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<description>has to be a USER</description>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>UserRealms</realm-name>
</login-config>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
jetty-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">UserRealms</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/> /tmp/realm.properties</Set>
<Set name="refreshInterval">0</Set>
</New>
</Set>
</Get>
</Configure>
realm.properties
user: 123456,USERS
foo: bar,user
前もって感謝します