我是使用jax-rs的bulid rest api。爲了處理身份驗證,我使用了Interceptor。當驗證失敗我回到WebApplicationException這樣的:jaxrs - 攔截器返回狀態時出錯500
try
{
Authentication authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(policy.getUserName(), policy.getPassword()));
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
}
catch (AuthenticationServiceException | BadCredentialsException e)
{
throw new WebApplicationException(Response.Status.UNAUTHORIZED); //TODO set response status
}
但startus返回500,而不是401
當我扔在服務寄回我設定的狀態WebApplicationExceptions但在攔截它並沒有奏效。如何從攔截器返回401?
JAXRS:服務器配置:
<jaxrs:server id="restService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="serviceBean"/>
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<ref bean="securityInterceptor"/>
</jaxrs:inInterceptors>
</jaxrs:server>
<bean id="serviceBean" class="some_package.CustomerService"/>
<bean id="securityInterceptor" class="some_package.AuthenticatorInterceptor"/>
我不確定,但可能認證你應該使用過濾器,而不是攔截器。 – mkrakhin 2014-09-01 11:02:33
@mkrakhin我嘗試過濾器,但我有版本1.1.1 jsr311-api它沒有奏效,我無法升級到最新版本。 – 2014-09-04 09:11:20