List of usage examples for org.springframework.security.core AuthenticationException getMessage
public String getMessage()
From source file:net.d53.syman.web.controller.PatientController.java
@RequestMapping(value = "/api/auth", method = RequestMethod.POST) @ResponseBody//from w w w .j a v a 2s.com public String APIauthenticate(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response) { String token = null; UsernamePasswordAuthenticationToken authenticationRequest = new UsernamePasswordAuthenticationToken( username, password); authenticationRequest.setDetails(APIAuthenticationToken.API_TOKEN_IDENTIFIER); try { APIAuthenticationToken res = (APIAuthenticationToken) authenticationManager .authenticate(authenticationRequest); LOGGER.info(ToStringBuilder.reflectionToString(res)); if (res != null) { token = res.getCredentials().toString(); LOGGER.info("Generated token " + token); SecurityContext context = SecurityContextHolder.getContext(); context.setAuthentication(res); this.securityContextRepository.saveContext(context, request, response); } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } catch (AuthenticationException e) { LOGGER.info("Authentication error: " + e.getMessage()); SecurityContextHolder.clearContext(); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } return token; }
From source file:com.ebay.pulsar.analytics.security.spring.PlainTextBasicAuthenticationEntryPoint.java
@Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\""); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); PrintWriter writer = response.getWriter(); writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage()); }
From source file:com.erudika.para.security.SimpleAuthenticationEntryPoint.java
@Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { if (isPreflight(request)) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); } else if (isRestRequest(request)) { RestUtils.returnStatusResponse(response, HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); } else {//w w w .j a v a 2s . co m super.commence(request, response, authException); } }
From source file:cz.zcu.kiv.eegdatabase.wui.app.session.EEGDataBaseSession.java
@Override public boolean authenticate(String username, String password) { if (password.equalsIgnoreCase(SOCIAL_PASSWD)) { this.setLoggedUser(facade.getPerson(username)); this.createShoppingCart(); this.createExperimentLicenseMap(); reloadPurchasedItemCache();//from w ww.java 2s.c o m return true; } boolean authenticated = false; try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(username, password)); SecurityContextHolder.getContext().setAuthentication(authentication); authenticated = authentication.isAuthenticated(); this.setLoggedUser(facade.getPerson(username)); reloadPurchasedItemCache(); this.createShoppingCart(); this.createExperimentLicenseMap(); } catch (AuthenticationException e) { error((String.format("User '%s' failed to login. Reason: %s", username, e.getMessage()))); authenticated = false; } if (getLoggedUser() != null && getLoggedUser().isLock()) { this.setLoggedUser(null); SecurityContextHolder.clearContext(); this.shoppingCart = null; error(ResourceUtils.getString("text.user.lock.login", username)); return false; } return authenticated; }
From source file:net.kamhon.ieagle.security.AuthenticationUtil.java
public UserDetails authenticate(HttpServletRequest request, HttpServletResponse response, String username, String password) {//from www . ja v a 2s . c o m Authentication authResult; try { // onPreAuthentication(request, response); authResult = attemptAuthentication(request, response, username, password); } catch (AuthenticationException failed) { try { // Authentication failed unsuccessfulAuthentication(request, response, failed); } catch (Exception ex) { throw new ValidatorException(failed.getMessage()); } if (failed instanceof BadCredentialsException) throw new InvalidCredentialsException("Invalid username or password"); else throw new ValidatorException(failed.getMessage()); } /*catch (IOException ex) { throw new DataException(ex); }*/ try { successfulAuthentication(request, response, authResult); } catch (IOException e) { throw new DataException(e); } catch (ServletException e) { throw new DataException(e); } return userDetailsService.findUserDetailsByUsername(username); }
From source file:com.gst.infrastructure.security.service.CustomAuthenticationFailureHandler.java
/** * Performs the redirect or forward to the {@code defaultFailureUrl} if set, * otherwise returns a 401 error code./*from ww w . j a v a2s .c o m*/ * <p> * If redirecting or forwarding, {@code saveException} will be called to * cache the exception for use in the target view. */ @Override public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException exception) throws IOException, ServletException { if (this.defaultFailureUrl == null) { this.logger.debug("No failure URL set, sending 401 Unauthorized error"); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + exception.getMessage()); } else { saveException(request, exception); if (this.forwardToDestination) { this.logger.debug("Forwarding to " + this.defaultFailureUrl); request.getRequestDispatcher(this.defaultFailureUrl).forward(request, response); } else { this.logger.debug("Redirecting to " + this.defaultFailureUrl); final String oauthToken = request.getParameter("oauth_token"); request.setAttribute("oauth_token", oauthToken); final String url = this.defaultFailureUrl + "?oauth_token=" + oauthToken; this.redirectStrategy.sendRedirect(request, response, url); } } }
From source file:org.ngrinder.security.SvnHttpBasicEntryPoint.java
@Override public void commence(HttpServletRequest request, HttpServletResponse response, // LB AuthenticationException authException) throws IOException, ServletException { // Get the first part of url path and use it as a realm. String pathInfo = request.getPathInfo(); String[] split = StringUtils.split(pathInfo, '/'); response.addHeader("WWW-Authenticate", "Basic realm=\"" + StringUtils.defaultIfBlank(split[0], "admin") + "\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); }
From source file:org.xaloon.wicket.security.spring.SpringSecurityFacade.java
private AuthenticationToken authenticateInternal(AbstractAuthenticationToken authenticationRequestToken) { boolean authenticated = false; String name = authenticationRequestToken.getName(); String errorMessage = null;/*from w w w . j a v a 2s.c o m*/ try { Authentication authentication = authenticationManager.authenticate(authenticationRequestToken); authenticated = authentication.isAuthenticated(); if (authenticated && authentication.getDetails() == null) { // Try to load user details. Copy information into new token UsernamePasswordAuthenticationToken authenticationWithDetails = new UsernamePasswordAuthenticationToken( authentication.getPrincipal(), authentication.getCredentials(), authentication.getAuthorities()); authenticationWithDetails.setDetails(userDao.getUserByUsername(authentication.getName())); authentication = authenticationWithDetails; } SecurityContextHolder.getContext().setAuthentication(authentication); name = authentication.getName(); } catch (AuthenticationException e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("User " + name + " failed to login. Reason: ", e); } authenticated = false; errorMessage = e.getMessage(); } if (authenticated) { return new AuthenticationToken(name, new ArrayList<AuthenticationAttribute>()); } return new AuthenticationToken(name, errorMessage); }