@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { try{ String header = request.getHeader("Authorization"); if (header == null || !header.startsWith("Basic ")) { chain.doFilter(request, response); return; } String[] decodedHeader = extractAndDecodeHeader(header, request); //Validate against client lockout policy String clientId = decodedHeader[0]; //Validate against client secret expiration in the zone configured client secret policy Timestamp lastModified = (Timestamp) clientDetailsService.loadClientByClientId(clientId).getAdditionalInformation().get(ClientConstants.LAST_MODIFIED); } catch(BadCredentialsException e) { super.getAuthenticationEntryPoint().commence(request, response, e); return; } catch(ClientRegistrationException e) { logger.debug(e.getMessage()); } //call parent class to authenticate super.doFilterInternal(request, response, chain); }
@Override public ClientDetails loadClientByClientId( String clientId ) throws ClientRegistrationException { ClientDetails clientDetails = clientDetails( oAuth2ClientService.getOAuth2ClientByClientId( clientId ) ); if ( clientDetails == null ) { throw new ClientRegistrationException( "Invalid client_id" ); } return clientDetails; }
@Override public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException { ClientDetail clientDetails = clientDetailsRepository.findByClientId(clientId); if (null == clientDetails) { throw new ClientRegistrationException("Client not found with id '" + clientId + "'"); } return getClientFromMongoDBClientDetails(clientDetails); }
ConsumerRecordVO consumer = this.getConsumerDAO().getConsumer(clientId); if (null == consumer) { throw new ClientRegistrationException("Client with id '" + clientId + "' does not exists"); throw new ClientRegistrationException("Client '" + clientId + "' is expired"); } catch (Exception t) { logger.error("Error extracting consumer record by key {}", clientId, t); throw new ClientRegistrationException("Error extracting consumer record by key " + clientId, t);
@Override public UserDetails loadUserByUsername( String username ) throws UsernameNotFoundException { try { return super.loadUserByUsername( username ); } catch ( ClientRegistrationException ex ) { throw new UsernameNotFoundException( ex.getMessage(), ex ); } } }
throw new ClientRegistrationException("该应用不存在!");
public void testLoadClientByInvalidClientId() { try { this.oauthConsumerManager.loadClientByClientId("invalid"); fail(); } catch (ClientRegistrationException t) { assertEquals("Client with id 'invalid' does not exists", t.getMessage()); } catch (Throwable t) { throw t; } }
public void testFailLoadClientByClientId() throws Throwable { ConsumerRecordVO consumer = this.createConsumer("key_3", "secret_3", true); try { assertNull(this.oauthConsumerManager.getConsumerRecord(consumer.getKey())); oauthConsumerManager.addConsumer(consumer); ConsumerRecordVO extractedConsumer = oauthConsumerManager.getConsumerRecord(consumer.getKey()); assertNotNull(extractedConsumer); this.oauthConsumerManager.loadClientByClientId("key_3"); fail(); } catch (ClientRegistrationException t) { assertEquals("Client 'key_3' is expired", t.getMessage()); } catch (Throwable t) { throw t; } finally { oauthConsumerManager.deleteConsumer(consumer.getKey()); assertNull(this.oauthConsumerManager.getConsumerRecord(consumer.getKey())); } }