2010-11-08 27 views
9

Tôi đang tìm kiếm một số gợi ý về cách để bảo đảm nguồn gốc còn lại của tôiLàm thế nào để làm Nghỉ ngơi Authentication với JAX-RS

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

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

     // Return a data back. 
    } 
} 

Tôi đã đọc bài của về authetication cơ bản và OAuth, tôi biết khái niệm nhưng tôi tìm cách để thực hiện nó trong mã.

Cảm ơn

Trả lời

6

Khai báo một đánh chặn:

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

Sau đó sử dụng nó:

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

Sau đó AuthenticationInterceptor của bạn, dọc theo dòng :

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"); 
    } 
    } 

} 

Xác định thông tin xác thực có thể chấp nhận theo cách thuận tiện hơn được để lại dưới dạng bài tập cho người đọc.

+0

hey cảm ơn tôi sẽ cố gắng này :) – BinCode

2

Cách tôi biết là thêm vào webapp của bạn web.xml. Tối thiểu, tôi nghĩ rằng bạn cần phải thêm:

<!-- 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

Cảm ơn Donal, tôi sẽ xem xét nó. và quay lại. – BinCode

Các vấn đề liên quan