4

Web 共通のセキュリティ制約を使用して API に認証を追加していますが、CORS フィルターが壊れているようです。私は以前、フィルターのみで動作し、アプリサーバーレベルの認証はありませんでした。

基本的な考え方は、/rest/account エンドポイントを除くすべてのリクエストで認証を要求することです。これは、これらが最初のログインを処理するものであり、パブリックにアクセスできる必要があるためです。

Chrome と Postman の両方でテストすると、ログイン時に POST 要求の一部として OPTIONS 呼び出しが行われると、405 Method not allowed 応答が返されます。

以下に関連するすべてを提供できれば幸いですが、他に何か必要な場合はお知らせください。前もって感謝します!

私の CORS フィルター

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

私のセキュリティ上の制約

<security-constraint>
    <display-name>WebsiteUsers</display-name>
    <web-resource-collection>
        <web-resource-name>WebsiteAPI</web-resource-name>
        <description/>
        <url-pattern>/rest/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description>Standard User authentication</description>
        <role-name>Users</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Public</web-resource-name>
        <description>Matches a few special endpoints</description>
        <url-pattern>/rest/account/*</url-pattern>
    </web-resource-collection>
    <!-- No auth-constraint means everybody has access! -->
</security-constraint>
4

1 に答える 1

3

<http-method-omission>OPTIONS</http-method-omission>protected に追加してみてください<web-resource-collection>。これにより、OPTIONS要求が CORS フィルターを通過できるようになります (適切なAccess-Control-Allow-Methods応答ヘッダーが返されます)。

https://docs.oracle.com/cd/E19798-01/821-1841/bncbk/index.htmlを参照してください。

あるいは (サーブレット仕様 < 3.0 をサポートする必要がある場合)、代わりに認証が必要なすべてのメソッド ( 、 、 など - を除く) を定義することGETPOSTできます。DELETEPUTOPTIONS<http-method>

于 2016-01-15T15:05:38.460 に答える