@Override public UserDetails loadUserByUsername(String uName) throws UsernameNotFoundException { YourEntity yourEntity = null; if (uName == null || uName.isEmpty()) { throw new PreAuthenticatedCredentialsNotFoundException("No User Email Address Supplied for Obtaining User, Ignoring!"); } LOGGER.info("Authenticating:[{}]", uName); yourEntity = identityProviderEntityManager.findYourEntityByEmail(uName); if (yourEntity == null) { LOGGER.warn("YourEntity Object Not Found based Upon Email:[{}]",uName); throw new UsernameNotFoundException("No User with email address '" + uName + "' could be found."); } LOGGER.info("YourEntity Object Found based Upon Email:[{}]",uName); return new YourMicroserviceUserDetails(yourEntity); }
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class, PreAuthenticatedCredentialsNotFoundException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) // 400 @ResponseBody public String resolveBadRequestExceptions() { return "error"; }
protected String extractUsername(HttpServletRequest request) { try { return (String)getPreAuthenticatedPrincipal(request); } catch (PreAuthenticatedCredentialsNotFoundException ex) { return null; } }
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String requestURI = request.getRequestURI(); if (requestURI.startsWith("/human/") && !requestURI.startsWith("/human/register")) { String authUsername = getAuthUsername(request); String uriUsername = getUriUsername(requestURI); if (!authUsername.equals(uriUsername)) { throw new PreAuthenticatedCredentialsNotFoundException("Unauthorized access."); } } filterChain.doFilter(request, response); }
private String getAuthUsername(HttpServletRequest request) { String authorizationString = request.getHeader("Authorization"); if (authorizationString == null) { throw new PreAuthenticatedCredentialsNotFoundException("Unauthorized access. Please provide preemptive HTTP Basic authorization credentials with every request."); } authorizationString = authorizationString.substring("Basic".length()).trim(); String credentials = new String(Base64.decode(authorizationString.getBytes()), Charset.forName("UTF-8")); return (credentials.split(":", 2))[0]; }
/** {@inheritDoc} */ @Override protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) { Object principal = request.getAttribute(m_principalRequestAttribute); if (principal == null) { throw new PreAuthenticatedCredentialsNotFoundException(m_principalRequestAttribute + " attribute not found in request."); } return principal; }
@Test public void request_that_match_a_machine_path_should_receive_403() throws IOException, ServletException { request.setServletPath("/nsi/v2/provider"); subject.commence(request, response, new PreAuthenticatedCredentialsNotFoundException("foo")); assertTrue(response.getStatus() == HttpServletResponse.SC_FORBIDDEN); }
@Test public void request_that_dont_match_a_machine_path_should_see_redirect_to_splashPath() throws IOException, ServletException{ request.setServletPath("/bod"); request.setPathInfo("/noc"); subject.commence(request, response, new PreAuthenticatedCredentialsNotFoundException("foo")); assertTrue(response.getStatus() == HttpServletResponse.SC_MOVED_TEMPORARILY); }
@Override protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) { MetkaAuthenticationDetails details = buildDetails(request); if(!StringUtils.hasText(details.getSessionId())) { throw new PreAuthenticatedCredentialsNotFoundException("Shibboleth session id not found."); } String userName = details.getUserName(); if(!StringUtils.hasText(userName)) { throw new PreAuthenticatedCredentialsNotFoundException("No user name for shibboleth session."); } return userName; }
@DELETE @Path("security-401") public void throwAuthenticationException() { throw new PreAuthenticatedCredentialsNotFoundException("message"); }
@Test(expected = PreAuthenticatedCredentialsNotFoundException.class) public void testPrincipalHeaderMissing() { final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); filter.setExceptionIfHeaderMissing(true); Assert.assertNull(filter.getPreAuthenticatedPrincipal(request)); }
@Test public void toResponse() { final PreAuthenticatedCredentialsNotFoundException exception = new PreAuthenticatedCredentialsNotFoundException("message-error"); check(mock(new AuthenticationExceptionMapper()).toResponse(exception), 401, "{\"code\":\"security\",\"message\":\"message-error\",\"parameters\":null,\"cause\":null}"); }