रीस्टलेट 2.0 में मूल प्रमाणीकरण करने के लिए (मुझे लगता है कि आप 2.012 का उपयोग कर रहे हैं क्योंकि आप ServerResource
का उल्लेख करते हैं), आपको ChallengeAuthenticator
का उपयोग करने की आवश्यकता है। यदि यह optional = true
के साथ कॉन्फ़िगर किया गया है तो प्रमाणीकरण केवल तभी अनुरोध किया जाएगा यदि आप ChallengeAuthenticator.challenge()
का आह्वान करते हैं।
आप एक authenticate()
विधि के साथ अपने आवेदन बना सकते हैं, और इस फोन जब भी आप सुरक्षित किया जा करने के लिए एक संसाधन के लिए उपयोग की जरूरत है:
आवेदन:
package example;
import org.restlet.*;
import org.restlet.data.ChallengeScheme;
import org.restlet.routing.Router;
import org.restlet.security.*;
public class ExampleApp extends Application {
private ChallengeAuthenticator authenticatior;
private ChallengeAuthenticator createAuthenticator() {
Context context = getContext();
boolean optional = true;
ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
String realm = "Example site";
// MapVerifier isn't very secure; see docs for alternatives
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put("user", "password".toCharArray());
ChallengeAuthenticator auth = new ChallengeAuthenticator(context, optional, challengeScheme, realm, verifier) {
@Override
protected boolean authenticate(Request request, Response response) {
if (request.getChallengeResponse() == null) {
return false;
} else {
return super.authenticate(request, response);
}
}
};
return auth;
}
@Override
public Restlet createInboundRoot() {
this.authenticatior = createAuthenticator();
Router router = new Router();
router.attach("/user", UserResource.class);
authenticatior.setNext(router);
return authenticatior;
}
public boolean authenticate(Request request, Response response) {
if (!request.getClientInfo().isAuthenticated()) {
authenticatior.challenge(response, false);
return false;
}
return true;
}
}
संसाधन:
package example;
import org.restlet.data.MediaType;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;
public class UserResource extends ServerResource {
@Override
public Representation get() {
ExampleApp app = (ExampleApp) getApplication();
if (!app.authenticate(getRequest(), getResponse())) {
// Not authenticated
return new EmptyRepresentation();
}
// Generate list of users
// ...
}
@Override
public Representation post(Representation entity) {
// Handle post
// ...
}
}
आपके उत्तर के लिए सबसे पहले धन्यवाद, यह आशाजनक लग रहा है। हालांकि, मुझे आपके कोड को काम करने में कुछ समस्याएं हैं। उदाहरण के लिए, क्लाइंटइन्फो के लिए कोई 'getSubject() 'विधि नहीं है (मैं 2.0m7 का उपयोग कर रहा हूं)। साथ ही, मुझे यकीन नहीं है कि आपकी 'प्रमाणीकृत()' विधि सही है या नहीं? –
मैं पहले स्नैपशॉट का उपयोग कर रहा था; मैंने 2.0m7 के साथ काम करने के लिए उदाहरण अपडेट किए हैं। – Sam
धन्यवाद, फिर, अब कोड संकलित और POST हमेशा उपलब्ध है। दुर्भाग्य से, कभी नहीं मिलता है। कोई फर्क नहीं पड़ता कि मैं कोई गलत, गलत या सही मूलभूत प्रमाण-पत्र प्रदान करता हूं, मुझे हमेशा 401 मिलते हैं। –