2010-11-08 17 views
9

मैं अपने बाकी जड़ संसाधन सुरक्षित करने के लिएJAX-रुपये के साथ आराम प्रमाणीकरण कैसे करना है

@Path("/employee") 
public class EmployeeResource { 

    @GET 
    @Produces("text/html") 
    public String get(
     @QueryParam("name") String empname, 
     @QueryParam("sn") String sn) { 

     // Return a data back. 
    } 
} 

पर कुछ संकेत मैं बुनियादी authetication और OAuth के बारे में पोस्ट की पढ़ा है देख रहा हूँ, मैं अवधारणा को पता है, लेकिन मैं कर रहा हूँ कोड में इसे कार्यान्वित करने के तरीकों की तलाश में।

धन्यवाद

उत्तर

6

एक इंटरसेप्टर घोषित:

<bean id="securityInterceptor" class="AuthenticatorInterceptor"> 
<property name="users"> 
    <map> 
<entry key="someuser" value="somepassword"/> 
    </map> 
</property> 

फिर इसका इस्तेमाल:

<jaxrs:server address="/"> 
     <jaxrs:inInterceptors> 
      <ref bean="securityInterceptor"/> 
     </jaxrs:inInterceptors> 
     (etc) 

फिर अपने AuthenticationInterceptor, की तर्ज पर न्यूनतम, मैं तुम्हें जोड़ने की जरूरत है लगता है :

import java.util.Map; 

import org.apache.cxf.message.Message; 
import org.apache.cxf.phase.PhaseInterceptor; 
import org.apache.cxf.phase.AbstractPhaseInterceptor; 
import org.apache.cxf.phase.Phase; 
import org.apache.cxf.configuration.security.AuthorizationPolicy; 
import org.apache.cxf.interceptor.Interceptor; 

import org.springframework.beans.factory.annotation.Required; 

public class AuthenticatorInterceptor extends AbstractPhaseInterceptor<Message> { 

    private Map<String,String> users; 

    @Required 
    public void setUsers(Map<String, String> users) { 
     this.users = users; 
    } 

    public AuthenticatorInterceptor() { 
     super(Phase.RECEIVE); 
    } 

    public void handleMessage(Message message) { 

     AuthorizationPolicy policy = message.get(AuthorizationPolicy.class); 

    if (policy == null) { 
     System.out.println("User attempted to log in with no credentials"); 
     throw new RuntimeException("Denied"); 
     } 

    String expectedPassword = users.get(policy.getUserName()); 
    if (expectedPassword == null || !expectedPassword.equals(policy.getPassword())) { 
     throw new RuntimeException("Denied"); 
    } 
    } 

} 

स्वीकार्य प्रमाण-पत्रों को अधिक सुविधाजनक तरीके से परिभाषित करना पाठक के लिए एक अभ्यास के रूप में छोड़ा गया है।

+0

अरे धन्यवाद मैं यह कोशिश करूंगा :) – BinCode

2

तरह से मैं जानता हूँ कि है अपने वेब ऐप्लिकेशन की web.xml में जोड़ने के लिए।

<!-- Specifies what and how to protect *part* of a webapp --> 
<security-constraint> 

    <!-- WHAT TO PROTECT --> 
    <web-resource-collection> 
     <web-resource-name>employee-related-urls</web-resource-name> 
     <!-- You might need to list other patterns too with more of these --> 
     <url-pattern>/employee/*</url-pattern> 
    </web-resource-collection> 

    <!-- WHO IS ALLOWED IN --> 
    <auth-constraint> 
     <!-- I assume something sensible here! --> 
     <role-name>employee</role-name> 
    </auth-constraint> 

    <!-- HOW TO PROTECT THE REQUESTS AND RESPONSES --> 
    <user-data-constraint> 
     <!-- Force HTTPS (or equivalent, in a formal sense) --> 
     <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
    </user-data-constraint> 
</security-constraint> 

<!-- HOW TO WORK OUT WHO IS ASKING --> 
<login-config> 
    <!-- This is how to specify BASIC HTTP auth; look up docs for OAuth yourself --> 
    <auth-method>BASIC</auth-method> 
    <!-- Omit the next element to use the container's default --> 
    <realm-name>site</realm-name> 
</login-config> 
+0

धन्यवाद डोनल, मैं इसे देख लूंगा। और वापस जाओ। – BinCode