2017-04-04 114 views
2

我在WildFly 10上有一個JAX-RS應用程序,它將通過一個簡單的基本身份驗證來保護。如何將401響應的內容更改爲單個格式?

它的工作原理,到目前爲止,但如果驗證失敗,該服務器

<html> 
    <head> 
    <title>Error</title> 
    </head> 
    <body>Unauthorized</body> 
</html> 
這不是我所期望的響應

響應。我更喜歡定製的(json)響應。

如何做到這一點?

我做了什麼至今:

  1. 我配置我的服務器配置一個簡單的UserRolesLoginModule新Wildfly安全域(這是足以在我的情況):

    <security-domain name="MySecurityDomain" cache-type="default"> 
        <authentication> 
        <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required"> 
         <module-option name="usersProperties" value="${jboss.server.config.dir}/users.properties"/> 
         <module-option name="rolesProperties" value="${jboss.server.config.dir}/roles.properties"/>        
         <module-option name="hashAlgorithm" value="MD5"/> 
         <module-option name="hashEncoding" value="base64"/> 
         <module-option name="hashCharset" value="UTF-8"/> 
         <module-option name="unauthenticatedIdentity" value="UnauthenticatedAccess"/> 
        </login-module>          
        </authentication> 
    </security-domain> 
    
  2. 我註釋了應用中的所有服務:

     @SecurityDomain("MySecurityDomain") 
        @RolesAllowed({ "RoleFromPropertyFile", "AnotherRoleFromPropertyFile" }) 
    
  3. 我創建了一個的jboss-web.xml文件與內容

    <jboss-web> 
         <security-domain>MySecurityDomain</security-domain> 
        </jboss-web> 
    
  4. 我有一個web.xml,我嘗試了很多不同的東西沒有任何成功... :-(
    當前的內容:

    <security-constraint> 
         <display-name>Deny all HTTP methods except GET and POST</display-name> 
         <web-resource-collection> 
         <web-resource-name>NextTest</web-resource-name> 
         <url-pattern>/mypattern/*</url-pattern> 
         <http-method-omission>GET</http-method-omission> 
         <http-method-omission>POST</http-method-omission> 
        </web-resource-collection>  
        </security-constraint> 
        <login-config> 
        <auth-method>BASIC</auth-method> 
        <realm-name>MySecurityRealm</realm-name> 
        </login-config> 
        <security-role> 
        <description>Access to all application parts</description> 
        <role-name>all</role-name> 
    </security-role>  
    <!-- and some more roles --> 
    
  5. 我還實施了ExceptionMapper<EJBAccessException>來生成我自己的回覆。但是,只有當我刪除web.xml的所有內容時才能找到此映射器。

我的猜測是,承辦人正在進行授權並處理對未授權訪問的響應。如果我刪除web.xml中的安全配置,則會訪問EJB,但不會評估BasicAuth標頭。在這種情況下,所有請求均被拒絕。

我可能我會避免寫一個Servlet並使用ExceptionMapper來代替。

任何想法我錯過了什麼?

回答

0

我做了一個小實驗,一些代碼和,雖然它並不漂亮,你可以嘗試這樣的:

import java.io.IOException; 

import javax.ws.rs.container.ContainerRequestContext; 
import javax.ws.rs.container.ContainerResponseContext; 
import javax.ws.rs.container.ContainerResponseFilter; 
import javax.ws.rs.ext.Provider; 

@Provider 
public class AuthBodyResponseFilter implements ContainerResponseFilter { 

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

     if((responseContext.getStatus() == 401) && 
      (responseContext.getEntity() instanceof String)) 
      responseContext.setEntity("no services for you!"); 
    } 
} 

我測試了一下,似乎工作。當然,挑戰是在哪裏還有一個帶有String響應體的401?我不得不測試更多,看看這是否涵蓋了一切。

0

這是我如何做到這一點:

@POST 
@Consumes("application/json") 
@Produces("application/json") 
public Response create(Entity entity) { 
    try { 
     Entity created = service().create(entity); 
     return Response.created(location(created)).entity(created).build(); 
    } catch (ServiceException e) { 
     return Response.status(e.getStatus()).entity(e).build(); 
    } 
} 

通知返回類型,Response。這允許您自定義響應,包括設置標題等。這也意味着您必須編寫更多的接線代碼。

我在這裏使用的自定義ServiceException已經有了它的狀態並用它來設置響應代碼。然後我傳遞異常本身,它將作爲JSON返回。

相關問題