繁体   English   中英

如何使用JAX-RS进行Rest身份验证

[英]How to do Rest Authentication with 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.
    }
}

我已阅读有关基本认证和OAuth的帖子,我知道这个概念,但我正在寻找如何在代码中实现它的方法。

谢谢

声明一个拦截器:

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

}

以更方便的方式定义可接受的凭证留给读者练习。

我知道的方法是添加到您的webapp的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM