2017-10-31 1 views
0

휴식 응용 프로그램에 대한 인터셉터를 등록하려고합니다. 이 인터셉터의 purpouse는 요청이 유효한지 여부를 확인하기 위해 요청 안에 토큰을 가져 오는 것입니다. 내가 이것을 달성하기 위해 사용자 정의 태그를 생성 한@Provider 리소스가 나머지 응용 프로그램에 등록되지 않았습니다.

는 :

@Provider 
@Secured 
public class AuthenticationFilter implements ContainerRequestFilter{ 

private static final Logger LOGGER = Logger.getLogger(AuthenticationFilter.class); 

UserDAO userDAO = (UserDAO) SpringApplicationContext.getBean("userDAO"); 

@Override 
public void filter(ContainerRequestContext requestContext) throws IOException { 

    // Get the HTTP Authorization header from the request 
    String authorizationHeader = 
     requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); 

    // Check if the HTTP Authorization header is present and formatted correctly 
    if (authorizationHeader == null || !authorizationHeader.startsWith("BSC")) { 
     if (authorizationHeader== null){ 
      LOGGER.error("No authorization header"); 
     } else{ 
      LOGGER.error("Authorization header: " + authorizationHeader); 
     } 
     throw new NotAuthorizedException("Authorization header must be provided"); 
    } 

    // Extract the token from the HTTP Authorization header 
    String token = authorizationHeader.substring("BSC".length()); 
    // Validate the token 
    boolean ok = validateToken(token); 

    if (!ok){ 
     LOGGER.error("Not authorized, passed token: " + token); 
     throw new NotAuthorizedException("Not authorized"); 
    } 


} 

private boolean validateToken(String token){ 
    boolean ok = userDAO.validateToken(token); 
    if (ok){ 
     userDAO.updateToken(token); 
    }else{ 
     userDAO.deleteToken(token); 
    } 

    return ok; 
} 

}

@NameBinding 
@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
public @interface Secured { 

} 

@Secured 태그와 방법의 모든 인터셉터 throught를 통과해야합니다.

나는 인터셉터 및 확장하는 클래스의 나머지 서비스를 등록 신청 :

그럼 내 web.xml에 내가이 클래스 등록
public class RestApplication extends Application{ 
private Set<Object> singletons = new HashSet<Object>(); 

public RestApplication() { 
    singletons.add(new RestService()); 
    singletons.add(new AuthenticationFilter()); 
} 

@Override 
public Set<Object> getSingletons() { 
    return singletons; 
} 
} 

:

<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>Manufacturing</display-name> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>jersey-serlvet</servlet-name> 
    <servlet-class> 
     com.sun.jersey.spi.container.servlet.ServletContainer 
    </servlet-class> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>com.everis.manufacturing.application.RestApplication</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>jersey-serlvet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

을 하지만 작동하지 않는 것처럼 보입니다. @Secured 태그가있는 서비스를 호출하지만 요격기를 호출하지 않습니다.

미리 감사드립니다.

답변

0

응용 프로그램 대신 ResourceConfig을 확장하도록 RestApplication 클래스를 변경해보십시오.

+0

저지에서 유일한 저지는 jersey-server입니다. ResourceConfig를 확장하려고 시도하면 com.sun.jersey.api.core.ResourceConfig가 발견됩니다. – A1t0r

+0

패키지 이름은 버전마다 다를 수 있습니다. 응용 프로그램을 확장하는 경우 사용할 수 있습니다. 그렇지 않다면 라이브러리 버전으로 질문을 업데이트 할 수 있습니까? – jay

관련 문제