2016-05-02 12 views
0

Ich muss ein benutzerdefiniertes Auth-Modul ähnlich wie HTTPBasicServerAuthModule erstellen, die nicht zur Eingabe von Benutzername und Kennwort für die Authentifizierung aufgefordert werden. Denn wir haben ein JWT-Token, das für uns die Authentifizierung übernimmt, und wir brauchen keine weitere Authentifizierungsebene. meine benutzerdefinierte Auth Modul ist folgende:So entfernen Sie die Authentifizierung von HTTPServerAuthModule in Jboss EAP

public class CustomJaspiAuthModule extends WebServerAuthModule 
{ 
    protected Context context; 
    protected boolean cache = false; 
    public static final byte[] AUTHENTICATE_BYTES = { 
     (byte) 'W', 
     (byte) 'W', 
     (byte) 'W', 
     (byte) '-', 
     (byte) 'A', 
     (byte) 'u', 
     (byte) 't', 
     (byte) 'h', 
     (byte) 'e', 
     (byte) 'n', 
     (byte) 't', 
     (byte) 'i', 
     (byte) 'c', 
     (byte) 'a', 
     (byte) 't', 
     (byte) 'e' 
    }; 
    protected String delegatingLoginContextName = null; 

    public CustomJaspiAuthModule() { } 

    public CustomJaspiAuthModule(String delegatingLoginContextName) { 
     super(); 
     this.delegatingLoginContextName = delegatingLoginContextName; 
    } 

    @Override 
    public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException { 
     // do nothing, just return SUCCESS. 
     return AuthStatus.SUCCESS; 
    } 

    @Override 
    public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException { 
     Request request = (Request) messageInfo.getRequestMessage(); 
     Response response = (Response) messageInfo.getResponseMessage(); 

     Principal principal; 
     context = request.getContext(); 
     LoginConfig config = context.getLoginConfig(); 

     // validate any credentials already included with this request. 
     String username = null; 
     String password = null; 

     MessageBytes authorization = request.getCoyoteRequest().getMimeHeaders().getValue("authorization"); 

     if (authorization != null) { 
      authorization.toBytes(); 
      ByteChunk authorizationBC = authorization.getByteChunk(); 

      if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { 
       authorizationBC.setOffset(authorizationBC.getOffset() + 6); 
       CharChunk authorizationCC = authorization.getCharChunk(); 
       Base64.decode(authorizationBC, authorizationCC); 

       // get username and password from the authorization char chunk. 
       int colon = authorizationCC.indexOf(':'); 
       if (colon < 0) { 
        username = authorizationCC.toString(); 
       } else { 
        char[] buf = authorizationCC.getBuffer(); 
        username = new String(buf, 0, colon); 
        password = new String(buf, colon + 1, authorizationCC.getEnd() - colon - 1); 
       } 

       authorizationBC.setOffset(authorizationBC.getOffset() - 6); 
      } 

      principal = context.getRealm().authenticate(username, password); 
      if (principal != null) { 
       registerWithCallbackHandler(principal, username, password); 

       return AuthStatus.SUCCESS; 
      } 
     } 

     // send an "unauthorized" response and an appropriate challenge. 
     MessageBytes authenticate = response.getCoyoteResponse().getMimeHeaders(). 
       addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length); 

     CharChunk authenticateCC = authenticate.getCharChunk(); 
     try { 
      authenticateCC.append("Basic realm=\""); 
      if (config.getRealmName() == null) { 
       authenticateCC.append(request.getServerName()); 
       authenticateCC.append(':'); 
       authenticateCC.append(Integer.toString(request.getServerPort())); 
      } else { 
       authenticateCC.append(config.getRealmName()); 
      } 
      authenticateCC.append('\"'); 
      authenticate.toChars(); 

      response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 
     } catch (IOException e) { 
      // Ignore IOException here (client disconnect) 
     } 

     return AuthStatus.FAILURE; 
    } 

} 

Ich glaube, dass der folgende Teil des Codes ist der Grund, warum wir die Aufforderung erhalten Benutzername und Passwort-Warnung für die Authentifizierung einzugeben.

MessageBytes authenticate = response.getCoyoteResponse().getMimeHeaders(). 
       addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length); 

     CharChunk authenticateCC = authenticate.getCharChunk(); 
     try { 
      authenticateCC.append("Basic realm=\""); 
      if (config.getRealmName() == null) { 
       authenticateCC.append(request.getServerName()); 
       authenticateCC.append(':'); 
       authenticateCC.append(Integer.toString(request.getServerPort())); 
      } else { 
       authenticateCC.append(config.getRealmName()); 
      } 
      authenticateCC.append('\"'); 
      authenticate.toChars(); 

      response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 
     } catch (IOException e) { 
      // Ignore IOException here (client disconnect) 
     } 

Aber wenn ich diesen Block entfernen, ich bin eine gute Antwort zu erhalten zurück (200 Antwort), aber die Anfrage und Antwort, die ich erhalte bin wieder leer. Bitte schlagen Sie vor, wie ich diese Authentifizierungswarnung entfernen kann.

Antwort

0

Ich schrieb eine benutzerdefinierte Auth-Modul wie folgt aus und mein Zweck diente:

public class CustomJaspiAuthModule extends WebServerAuthModule 
{ 
    private static final String CLASSNAME = "CustomJaspiAuthModule"; 
    private static Logger  logger  = Logger.getLogger(CustomJaspiAuthModule.class.getName()); 

    protected Context context; 
    protected String delegatingLoginContextName = null; 

    public CustomJaspiAuthModule() { } 

    public CustomJaspiAuthModule(String delegatingLoginContextName) 
    { 
     super(); 
     this.delegatingLoginContextName = delegatingLoginContextName; 
    } 

    @Override 
    public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException 
    { 
     // do nothing, just return SUCCESS. 
     return AuthStatus.SUCCESS; 
    } 

    @Override 
    public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException 
    { 
     final String METHOD_NAME = "validateRequest"; 
     logger.logp(Level.INFO, CLASSNAME, METHOD_NAME, "**IN AUTHENTICATION MODULE**CustomJaspiAuthModule.validateRequest()"); 

     Request request = (Request) messageInfo.getRequestMessage(); 
     Response response = (Response) messageInfo.getResponseMessage(); 

     boolean authenticated = false; 
     context = request.getContext(); 
     Principal principal = context.getRealm().authenticate("*", "*"); 

     Callback[] callbacks = new Callback[] { 
       new CallerPrincipalCallback(clientSubject, (Principal) null) }; 

     if (principal != null) 
     { 
      callbacks = new Callback[] { 
        new CallerPrincipalCallback(clientSubject, principal) }; 
      authenticated = true; 
     } 

     if (authenticated) 
     { 
      try 
      { 
       callbackHandler.handle(callbacks); 
      } 
      catch (final Exception e) 
      { 
       throw (AuthException) new AuthException().initCause(e); 
      } 
      return AuthStatus.SUCCESS; 
     } 

     return AuthStatus.FAILURE; 
    } 
} 
Verwandte Themen