HTTP-аутентификация в рестлете, аутентификация дочернего URL - PullRequest
1 голос
/ 11 февраля 2012

Я использую механизм аутентификации HTTP Digest на стороне сервера, а клиент - firefox.

Это код на стороне сервера

Application application = new Vehicle();

component.getDefaultHost().attachDefault(application);
component.getDefaultHost().attach("/home",new Home());

DigestAuthenticator guard = new DigestAuthenticator(null, "TestRealm","mySecretServerKey");
Instantiates a Verifier of identifier/secret couples based on a  simple Map.
MapVerifier mapVerifier = new MapVerifier();

Загрузка одной статической пары логин / секрет.

mapVerifier.getLocalSecrets().put("login", "secret".toCharArray());
guard.setWrappedVerifier(mapVerifier);

Охрана рестлета

guard.setNext(application);
component.getDefaultHost().attachDefault(guard);  
component.start();

В домашнем классе

Router router = new Router(getContext());
router.attach("/People", People.class);
router.attach("/categories/",Categories.class);

return router;

если я запрашиваю http://localhost:8182/ Проверка подлинности Http работает, но http://localhost:8182/home/categories/ не запрашивает http authentication, если сначала мы попробуем /home/categories/ вместо http://localhost:8182/, это выдаст результат без какого-либо механизма аутентификации. Как это решить?

1 Ответ

0 голосов
/ 13 февраля 2012

Вы прикрепляете охрану только к маршруту по умолчанию, поэтому маршруты не совпадают ни с какими другими маршрутами.См. Javadoc для attachDefault:

 * Attaches a Resource class to this router as the default target to invoke
 * when no route matches. It actually sets a default route that scores all
 * calls to 1.0.

Ваши другие маршруты не являются маршрутами по умолчанию и поэтому они не охраняются

router.attach("/People", People.class);
router.attach("/categories/",Categories.class);

Вы должны связать охрану между каждым маршрутом, который вы хотитезащитить вот так:

DigestAuthenticator peopleGuard = new DigestAuthenticator(null, "TestRealm","mySecretServerKey");
peopleGuard.setNext(People.class);
router.attach("/People", peopleGuard);
...