IT社畜犬くわっちょのはてな

青森の片隅で働く、フルスタックエンジニアに憧れる器用貧乏なIT社畜犬の遠吠え

Tomcatだけで特定の画面だけBASIC認証を外したい時

正直、普通にHTTPサーバー側で制御しろという話なのだが......

web.xmlにこんな感じに security-constraint を2つ用意した上で必要なものをBASIC認証のレルムから外す事でできる。

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>no auth</web-resource-name>
            <url-pattern>/sample.html</url-pattern>
            <url-pattern>/hoge/*</url-pattern>
        </web-resource-collection>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>User Basic Auth</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>samplerole</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>User Basic Auth</realm-name>
    </login-config>

    <security-role>
        <role-name>samplerole</role-name>
    </security-role>

特定の画面だけ認証をかけるだけでなく、逆も一応できる。