保护网络球衣1.19休息

时间:2018-10-01 13:13:05

标签: java jersey

我无法为我的项目创建过滤器,因此每个人都可以使用它们,所有这些我都找到了jersey 2.0的示例,但是当我尝试在我的项目中使用时根本无法使用。我无法弄清楚如何使用jersey 1.19拦截URL上的REST服务。我已经在这个项目上工作了大约一个月,所以我拒绝升级到jersey 2.0,除非这很容易。该服务现在在云上。 https://pets2018.herokuapp.com/controllers.jsp

package com.service.controllers;

import java.util.List;
import java.util.StringTokenizer;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import com.service.exceptions.WebApplicationException;
import com.sun.jersey.core.util.Base64;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

@Provider
public class AuthController implements ContainerRequestFilter  {

private static final String AUTHORIZATION_HEADER_KEY = "Authorization";
private static final String AUTHORIZATION_HEADER_PREFIX = "Basic ";
private static final String CONTROLLERS_PREFIX = "test";

@Override
public ContainerRequest filter(ContainerRequest requestContext) {

if(requestContext.getPath().contains(CONTROLLERS_PREFIX)) {
    System.out.println(requestContext.getBaseUri().getPath());
    List<String> authHeader = requestContext.getRequestHeader(AUTHORIZATION_HEADER_KEY);
    if(authHeader != null && authHeader.size() > 0) {
        String authToken = authHeader.get(0);
        authToken = authToken.replaceFirst(AUTHORIZATION_HEADER_PREFIX, "");
        String decodedString = Base64.base64Decode(authToken);
        StringTokenizer tokenizer = new StringTokenizer(decodedString, ":");
        String username = tokenizer.nextToken();
        String password = tokenizer.nextToken();

        if("username".equals(username) && "password".equals(password)) {

        }
    }else {
        Response unauthorizedStatus = Response.status(Response.Status.UNAUTHORIZED).entity("No puede ingresar a este servicio").build();
        try {
            throw new WebApplicationException(unauthorizedStatus);
        } catch (WebApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}else {
System.out.println("No se encuentra la palabra indicada en el URI requerido");
}
    return null;
}

}

0 个答案:

没有答案