我想使用TJWS通过jboss测试我的Java Web应用程序。 我使用ContainerRequestFilter的实现,该实现允许我指定允许访问应用程序中方法的角色列表(通过使用RolesAllowed批注)。
我不知道如何将过滤器“插入”我的小型服务器。我试图将其添加到提供程序列表中,但不起作用...
在服务器代码下方:
public abstract class BaseServiceTest extends JpaTest {
private static final Integer PORT = 12345;
protected TJWSEmbeddedJaxrsServer server;
private static User adminUser;
private static User basicUser;
private static User racUser;
private String LOGIN_URL ="/users/login";
protected static String adminAuthentication;
protected static String basicAuthentication;
protected static String racAuthentication;
private UserDao userDao;
private String bindAddress = "localhost";
@Override
public void setUp() throws InitializationError {
super.setUp();
userDao = new UserDao();
server = new TJWSEmbeddedJaxrsServer();
server.setPort(PORT);
RestAssured.port = PORT;
server.setBindAddress(bindAddress);
server.setRootResourcePath("/");
for (Object endpoint : getEndpoints()) {
server.getDeployment().getResources().add(endpoint);
}
SecurityRequestFilter srFilter = new SecurityRequestFilter();
try {
FieldUtils.writeField(srFilter, "userDao", userDao, true);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
server.getDeployment().getProviders().add(srFilter);
server.start();
// other stuff...
}
以及过滤器的代码:
@Provider
@Priority(Priorities.AUTHENTICATION)
public class SecurityRequestFilter implements ContainerRequestFilter {
@Context
UriInfo uriInfo;
@Inject
UserDao userDao;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
//some filter stuff
...
}
尽管我的英语不太好,但我还是希望我能指出观点。